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-2013, 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 | |
---|
42 | using namespace std; |
---|
43 | |
---|
44 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
45 | |
---|
46 | //sequence level |
---|
47 | TEncRCSeq::TEncRCSeq() |
---|
48 | { |
---|
49 | m_totalFrames = 0; |
---|
50 | m_targetRate = 0; |
---|
51 | m_frameRate = 0; |
---|
52 | m_targetBits = 0; |
---|
53 | m_GOPSize = 0; |
---|
54 | m_picWidth = 0; |
---|
55 | m_picHeight = 0; |
---|
56 | m_LCUWidth = 0; |
---|
57 | m_LCUHeight = 0; |
---|
58 | m_numberOfLevel = 0; |
---|
59 | m_numberOfLCU = 0; |
---|
60 | m_averageBits = 0; |
---|
61 | m_bitsRatio = NULL; |
---|
62 | m_GOPID2Level = NULL; |
---|
63 | m_picPara = NULL; |
---|
64 | m_LCUPara = NULL; |
---|
65 | m_numberOfPixel = 0; |
---|
66 | m_framesLeft = 0; |
---|
67 | m_bitsLeft = 0; |
---|
68 | m_useLCUSeparateModel = false; |
---|
69 | } |
---|
70 | |
---|
71 | TEncRCSeq::~TEncRCSeq() |
---|
72 | { |
---|
73 | destroy(); |
---|
74 | } |
---|
75 | |
---|
76 | Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel ) |
---|
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 |
---|
104 | { |
---|
105 | m_alphaUpdate = 0.1; |
---|
106 | m_betaUpdate = 0.05; |
---|
107 | } |
---|
108 | m_averageBits = (Int)(m_targetBits / totalFrames); |
---|
109 | Int picWidthInBU = ( m_picWidth % m_LCUWidth ) == 0 ? m_picWidth / m_LCUWidth : m_picWidth / m_LCUWidth + 1; |
---|
110 | Int picHeightInBU = ( m_picHeight % m_LCUHeight ) == 0 ? m_picHeight / m_LCUHeight : m_picHeight / m_LCUHeight + 1; |
---|
111 | m_numberOfLCU = picWidthInBU * picHeightInBU; |
---|
112 | |
---|
113 | m_bitsRatio = new Int[m_GOPSize]; |
---|
114 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
115 | { |
---|
116 | m_bitsRatio[i] = 1; |
---|
117 | } |
---|
118 | |
---|
119 | m_GOPID2Level = new Int[m_GOPSize]; |
---|
120 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
121 | { |
---|
122 | m_GOPID2Level[i] = 1; |
---|
123 | } |
---|
124 | |
---|
125 | m_picPara = new TRCParameter[m_numberOfLevel]; |
---|
126 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
127 | { |
---|
128 | m_picPara[i].m_alpha = 0.0; |
---|
129 | m_picPara[i].m_beta = 0.0; |
---|
130 | } |
---|
131 | |
---|
132 | if ( m_useLCUSeparateModel ) |
---|
133 | { |
---|
134 | m_LCUPara = new TRCParameter*[m_numberOfLevel]; |
---|
135 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
136 | { |
---|
137 | m_LCUPara[i] = new TRCParameter[m_numberOfLCU]; |
---|
138 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
139 | { |
---|
140 | m_LCUPara[i][j].m_alpha = 0.0; |
---|
141 | m_LCUPara[i][j].m_beta = 0.0; |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | m_framesLeft = m_totalFrames; |
---|
147 | m_bitsLeft = m_targetBits; |
---|
148 | } |
---|
149 | |
---|
150 | Void TEncRCSeq::destroy() |
---|
151 | { |
---|
152 | if (m_bitsRatio != NULL) |
---|
153 | { |
---|
154 | delete[] m_bitsRatio; |
---|
155 | m_bitsRatio = NULL; |
---|
156 | } |
---|
157 | |
---|
158 | if ( m_GOPID2Level != NULL ) |
---|
159 | { |
---|
160 | delete[] m_GOPID2Level; |
---|
161 | m_GOPID2Level = NULL; |
---|
162 | } |
---|
163 | |
---|
164 | if ( m_picPara != NULL ) |
---|
165 | { |
---|
166 | delete[] m_picPara; |
---|
167 | m_picPara = NULL; |
---|
168 | } |
---|
169 | |
---|
170 | if ( m_LCUPara != NULL ) |
---|
171 | { |
---|
172 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
173 | { |
---|
174 | delete[] m_LCUPara[i]; |
---|
175 | } |
---|
176 | delete[] m_LCUPara; |
---|
177 | m_LCUPara = NULL; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | Void TEncRCSeq::initBitsRatio( Int bitsRatio[]) |
---|
182 | { |
---|
183 | for (Int i=0; i<m_GOPSize; i++) |
---|
184 | { |
---|
185 | m_bitsRatio[i] = bitsRatio[i]; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | Void TEncRCSeq::initGOPID2Level( Int GOPID2Level[] ) |
---|
190 | { |
---|
191 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
192 | { |
---|
193 | m_GOPID2Level[i] = GOPID2Level[i]; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | Void TEncRCSeq::initPicPara( TRCParameter* picPara ) |
---|
198 | { |
---|
199 | assert( m_picPara != NULL ); |
---|
200 | |
---|
201 | if ( picPara == NULL ) |
---|
202 | { |
---|
203 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
204 | { |
---|
205 | m_picPara[i].m_alpha = 3.2003; |
---|
206 | m_picPara[i].m_beta = -1.367; |
---|
207 | } |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
212 | { |
---|
213 | m_picPara[i] = picPara[i]; |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | Void TEncRCSeq::initLCUPara( TRCParameter** LCUPara ) |
---|
219 | { |
---|
220 | if ( m_LCUPara == NULL ) |
---|
221 | { |
---|
222 | return; |
---|
223 | } |
---|
224 | if ( LCUPara == NULL ) |
---|
225 | { |
---|
226 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
227 | { |
---|
228 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
229 | { |
---|
230 | m_LCUPara[i][j].m_alpha = 3.2003; |
---|
231 | m_LCUPara[i][j].m_beta = -1.367; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | else |
---|
236 | { |
---|
237 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
238 | { |
---|
239 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
240 | { |
---|
241 | m_LCUPara[i][j] = LCUPara[i][j]; |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | Void TEncRCSeq::updateAfterPic ( Int bits ) |
---|
248 | { |
---|
249 | m_bitsLeft -= bits; |
---|
250 | m_framesLeft--; |
---|
251 | } |
---|
252 | |
---|
253 | Int TEncRCSeq::getRefineBitsForIntra( Int orgBits ) |
---|
254 | { |
---|
255 | Double bpp = ( (Double)orgBits ) / m_picHeight / m_picHeight; |
---|
256 | if ( bpp > 0.2 ) |
---|
257 | { |
---|
258 | return orgBits * 5; |
---|
259 | } |
---|
260 | if ( bpp > 0.1 ) |
---|
261 | { |
---|
262 | return orgBits * 7; |
---|
263 | } |
---|
264 | return orgBits * 10; |
---|
265 | } |
---|
266 | |
---|
267 | //GOP level |
---|
268 | TEncRCGOP::TEncRCGOP() |
---|
269 | { |
---|
270 | m_encRCSeq = NULL; |
---|
271 | m_picTargetBitInGOP = NULL; |
---|
272 | m_numPic = 0; |
---|
273 | m_targetBits = 0; |
---|
274 | m_picLeft = 0; |
---|
275 | m_bitsLeft = 0; |
---|
276 | } |
---|
277 | |
---|
278 | TEncRCGOP::~TEncRCGOP() |
---|
279 | { |
---|
280 | destroy(); |
---|
281 | } |
---|
282 | |
---|
283 | Void TEncRCGOP::create( TEncRCSeq* encRCSeq, Int numPic ) |
---|
284 | { |
---|
285 | destroy(); |
---|
286 | Int targetBits = xEstGOPTargetBits( encRCSeq, numPic ); |
---|
287 | |
---|
288 | m_picTargetBitInGOP = new Int[numPic]; |
---|
289 | Int i; |
---|
290 | Int totalPicRatio = 0; |
---|
291 | Int currPicRatio = 0; |
---|
292 | for ( i=0; i<numPic; i++ ) |
---|
293 | { |
---|
294 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
295 | } |
---|
296 | for ( i=0; i<numPic; i++ ) |
---|
297 | { |
---|
298 | currPicRatio = encRCSeq->getBitRatio( i ); |
---|
299 | m_picTargetBitInGOP[i] = targetBits * currPicRatio / totalPicRatio; |
---|
300 | } |
---|
301 | |
---|
302 | m_encRCSeq = encRCSeq; |
---|
303 | m_numPic = numPic; |
---|
304 | m_targetBits = targetBits; |
---|
305 | m_picLeft = m_numPic; |
---|
306 | m_bitsLeft = m_targetBits; |
---|
307 | } |
---|
308 | |
---|
309 | Void TEncRCGOP::destroy() |
---|
310 | { |
---|
311 | m_encRCSeq = NULL; |
---|
312 | if ( m_picTargetBitInGOP != NULL ) |
---|
313 | { |
---|
314 | delete[] m_picTargetBitInGOP; |
---|
315 | m_picTargetBitInGOP = NULL; |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | Void TEncRCGOP::updateAfterPicture( Int bitsCost ) |
---|
320 | { |
---|
321 | m_bitsLeft -= bitsCost; |
---|
322 | m_picLeft--; |
---|
323 | } |
---|
324 | |
---|
325 | Int TEncRCGOP::xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize ) |
---|
326 | { |
---|
327 | Int realInfluencePicture = min( g_RCSmoothWindowSize, encRCSeq->getFramesLeft() ); |
---|
328 | Int averageTargetBitsPerPic = (Int)( encRCSeq->getTargetBits() / encRCSeq->getTotalFrames() ); |
---|
329 | Int currentTargetBitsPerPic = (Int)( ( encRCSeq->getBitsLeft() - averageTargetBitsPerPic * (encRCSeq->getFramesLeft() - realInfluencePicture) ) / realInfluencePicture ); |
---|
330 | Int targetBits = currentTargetBitsPerPic * GOPSize; |
---|
331 | |
---|
332 | if ( targetBits < 200 ) |
---|
333 | { |
---|
334 | targetBits = 200; // at least allocate 200 bits for one GOP |
---|
335 | } |
---|
336 | |
---|
337 | return targetBits; |
---|
338 | } |
---|
339 | |
---|
340 | //picture level |
---|
341 | TEncRCPic::TEncRCPic() |
---|
342 | { |
---|
343 | m_encRCSeq = NULL; |
---|
344 | m_encRCGOP = NULL; |
---|
345 | |
---|
346 | m_frameLevel = 0; |
---|
347 | m_numberOfPixel = 0; |
---|
348 | m_numberOfLCU = 0; |
---|
349 | m_targetBits = 0; |
---|
350 | m_estHeaderBits = 0; |
---|
351 | m_estPicQP = 0; |
---|
352 | m_estPicLambda = 0.0; |
---|
353 | |
---|
354 | m_LCULeft = 0; |
---|
355 | m_bitsLeft = 0; |
---|
356 | m_pixelsLeft = 0; |
---|
357 | |
---|
358 | m_LCUs = NULL; |
---|
359 | m_lastPicture = NULL; |
---|
360 | m_picActualHeaderBits = 0; |
---|
361 | m_totalMAD = 0.0; |
---|
362 | m_picActualBits = 0; |
---|
363 | m_picQP = 0; |
---|
364 | m_picLambda = 0.0; |
---|
365 | } |
---|
366 | |
---|
367 | TEncRCPic::~TEncRCPic() |
---|
368 | { |
---|
369 | destroy(); |
---|
370 | } |
---|
371 | |
---|
372 | Int TEncRCPic::xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ) |
---|
373 | { |
---|
374 | Int targetBits = 0; |
---|
375 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
---|
376 | |
---|
377 | Int i; |
---|
378 | Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft(); |
---|
379 | Int currPicRatio = encRCSeq->getBitRatio( currPicPosition ); |
---|
380 | Int totalPicRatio = 0; |
---|
381 | for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ ) |
---|
382 | { |
---|
383 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
384 | } |
---|
385 | |
---|
386 | targetBits = Int( GOPbitsLeft * currPicRatio / totalPicRatio ); |
---|
387 | |
---|
388 | if ( targetBits < 100 ) |
---|
389 | { |
---|
390 | targetBits = 100; // at least allocate 100 bits for one picture |
---|
391 | } |
---|
392 | |
---|
393 | if ( m_encRCSeq->getFramesLeft() > 16 ) |
---|
394 | { |
---|
395 | targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) ); |
---|
396 | } |
---|
397 | |
---|
398 | return targetBits; |
---|
399 | } |
---|
400 | |
---|
401 | Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ) |
---|
402 | { |
---|
403 | Int numPreviousPics = 0; |
---|
404 | Int totalPreviousBits = 0; |
---|
405 | |
---|
406 | list<TEncRCPic*>::iterator it; |
---|
407 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
408 | { |
---|
409 | if ( (*it)->getFrameLevel() == frameLevel ) |
---|
410 | { |
---|
411 | totalPreviousBits += (*it)->getPicActualHeaderBits(); |
---|
412 | numPreviousPics++; |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | Int estHeaderBits = 0; |
---|
417 | if ( numPreviousPics > 0 ) |
---|
418 | { |
---|
419 | estHeaderBits = totalPreviousBits / numPreviousPics; |
---|
420 | } |
---|
421 | |
---|
422 | return estHeaderBits; |
---|
423 | } |
---|
424 | |
---|
425 | Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ) |
---|
426 | { |
---|
427 | if ( listPreviousPictures.size() > g_RCMaxPicListSize ) |
---|
428 | { |
---|
429 | TEncRCPic* p = listPreviousPictures.front(); |
---|
430 | listPreviousPictures.pop_front(); |
---|
431 | p->destroy(); |
---|
432 | delete p; |
---|
433 | } |
---|
434 | |
---|
435 | listPreviousPictures.push_back( this ); |
---|
436 | } |
---|
437 | |
---|
438 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ) |
---|
439 | { |
---|
440 | destroy(); |
---|
441 | m_encRCSeq = encRCSeq; |
---|
442 | m_encRCGOP = encRCGOP; |
---|
443 | |
---|
444 | Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP ); |
---|
445 | Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel ); |
---|
446 | |
---|
447 | if ( targetBits < estHeaderBits + 100 ) |
---|
448 | { |
---|
449 | targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data |
---|
450 | } |
---|
451 | |
---|
452 | m_frameLevel = frameLevel; |
---|
453 | m_numberOfPixel = encRCSeq->getNumPixel(); |
---|
454 | m_numberOfLCU = encRCSeq->getNumberOfLCU(); |
---|
455 | m_estPicLambda = 100.0; |
---|
456 | m_targetBits = targetBits; |
---|
457 | m_estHeaderBits = estHeaderBits; |
---|
458 | m_bitsLeft = m_targetBits; |
---|
459 | Int picWidth = encRCSeq->getPicWidth(); |
---|
460 | Int picHeight = encRCSeq->getPicHeight(); |
---|
461 | Int LCUWidth = encRCSeq->getLCUWidth(); |
---|
462 | Int LCUHeight = encRCSeq->getLCUHeight(); |
---|
463 | Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1; |
---|
464 | Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1; |
---|
465 | |
---|
466 | m_LCULeft = m_numberOfLCU; |
---|
467 | m_bitsLeft -= m_estHeaderBits; |
---|
468 | m_pixelsLeft = m_numberOfPixel; |
---|
469 | |
---|
470 | m_LCUs = new TRCLCU[m_numberOfLCU]; |
---|
471 | Int i, j; |
---|
472 | Int LCUIdx; |
---|
473 | for ( i=0; i<picWidthInLCU; i++ ) |
---|
474 | { |
---|
475 | for ( j=0; j<picHeightInLCU; j++ ) |
---|
476 | { |
---|
477 | LCUIdx = j*picWidthInLCU + i; |
---|
478 | m_LCUs[LCUIdx].m_actualBits = 0; |
---|
479 | m_LCUs[LCUIdx].m_QP = 0; |
---|
480 | m_LCUs[LCUIdx].m_lambda = 0.0; |
---|
481 | m_LCUs[LCUIdx].m_targetBits = 0; |
---|
482 | m_LCUs[LCUIdx].m_MAD = 0.0; |
---|
483 | Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); |
---|
484 | Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); |
---|
485 | m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight; |
---|
486 | } |
---|
487 | } |
---|
488 | m_picActualHeaderBits = 0; |
---|
489 | m_totalMAD = 0.0; |
---|
490 | m_picActualBits = 0; |
---|
491 | m_picQP = 0; |
---|
492 | m_picLambda = 0.0; |
---|
493 | |
---|
494 | m_lastPicture = NULL; |
---|
495 | list<TEncRCPic*>::reverse_iterator it; |
---|
496 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
---|
497 | { |
---|
498 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
499 | { |
---|
500 | m_lastPicture = (*it); |
---|
501 | break; |
---|
502 | } |
---|
503 | } |
---|
504 | } |
---|
505 | |
---|
506 | Void TEncRCPic::destroy() |
---|
507 | { |
---|
508 | if( m_LCUs != NULL ) |
---|
509 | { |
---|
510 | delete[] m_LCUs; |
---|
511 | m_LCUs = NULL; |
---|
512 | } |
---|
513 | m_encRCSeq = NULL; |
---|
514 | m_encRCGOP = NULL; |
---|
515 | } |
---|
516 | |
---|
517 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures ) |
---|
518 | { |
---|
519 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
520 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
521 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
522 | Double estLambda = alpha * pow( bpp, beta ); |
---|
523 | Double lastLevelLambda = -1.0; |
---|
524 | Double lastPicLambda = -1.0; |
---|
525 | Double lastValidLambda = -1.0; |
---|
526 | list<TEncRCPic*>::iterator it; |
---|
527 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
528 | { |
---|
529 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
530 | { |
---|
531 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
532 | } |
---|
533 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
534 | |
---|
535 | if ( lastPicLambda > 0.0 ) |
---|
536 | { |
---|
537 | lastValidLambda = lastPicLambda; |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | if ( lastLevelLambda > 0.0 ) |
---|
542 | { |
---|
543 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
544 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
545 | } |
---|
546 | |
---|
547 | if ( lastPicLambda > 0.0 ) |
---|
548 | { |
---|
549 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
550 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
551 | } |
---|
552 | else if ( lastValidLambda > 0.0 ) |
---|
553 | { |
---|
554 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
555 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
556 | } |
---|
557 | else |
---|
558 | { |
---|
559 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
560 | } |
---|
561 | |
---|
562 | if ( estLambda < 0.1 ) |
---|
563 | { |
---|
564 | estLambda = 0.1; |
---|
565 | } |
---|
566 | |
---|
567 | m_estPicLambda = estLambda; |
---|
568 | return estLambda; |
---|
569 | } |
---|
570 | |
---|
571 | Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures ) |
---|
572 | { |
---|
573 | Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
574 | |
---|
575 | Int lastLevelQP = g_RCInvalidQPValue; |
---|
576 | Int lastPicQP = g_RCInvalidQPValue; |
---|
577 | Int lastValidQP = g_RCInvalidQPValue; |
---|
578 | list<TEncRCPic*>::iterator it; |
---|
579 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
580 | { |
---|
581 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
582 | { |
---|
583 | lastLevelQP = (*it)->getPicActualQP(); |
---|
584 | } |
---|
585 | lastPicQP = (*it)->getPicActualQP(); |
---|
586 | if ( lastPicQP > g_RCInvalidQPValue ) |
---|
587 | { |
---|
588 | lastValidQP = lastPicQP; |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | if ( lastLevelQP > g_RCInvalidQPValue ) |
---|
593 | { |
---|
594 | QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP ); |
---|
595 | } |
---|
596 | |
---|
597 | if( lastPicQP > g_RCInvalidQPValue ) |
---|
598 | { |
---|
599 | QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP ); |
---|
600 | } |
---|
601 | else if( lastValidQP > g_RCInvalidQPValue ) |
---|
602 | { |
---|
603 | QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP ); |
---|
604 | } |
---|
605 | |
---|
606 | return QP; |
---|
607 | } |
---|
608 | |
---|
609 | Double TEncRCPic::getLCUTargetBpp() |
---|
610 | { |
---|
611 | Int LCUIdx = getLCUCoded(); |
---|
612 | Double bpp = -1.0; |
---|
613 | Int avgBits = 0; |
---|
614 | Double totalMAD = -1.0; |
---|
615 | Double MAD = -1.0; |
---|
616 | |
---|
617 | if ( m_lastPicture == NULL ) |
---|
618 | { |
---|
619 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
620 | } |
---|
621 | else |
---|
622 | { |
---|
623 | MAD = m_lastPicture->getLCU(LCUIdx).m_MAD; |
---|
624 | totalMAD = m_lastPicture->getTotalMAD(); |
---|
625 | for ( Int i=0; i<LCUIdx; i++ ) |
---|
626 | { |
---|
627 | totalMAD -= m_lastPicture->getLCU(i).m_MAD; |
---|
628 | } |
---|
629 | |
---|
630 | if ( totalMAD > 0.1 ) |
---|
631 | { |
---|
632 | avgBits = Int( m_bitsLeft * MAD / totalMAD ); |
---|
633 | } |
---|
634 | else |
---|
635 | { |
---|
636 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | #if L0033_RC_BUGFIX |
---|
641 | if ( avgBits < 1 ) |
---|
642 | { |
---|
643 | avgBits = 1; |
---|
644 | } |
---|
645 | #else |
---|
646 | if ( avgBits < 5 ) |
---|
647 | { |
---|
648 | avgBits = 5; |
---|
649 | } |
---|
650 | #endif |
---|
651 | |
---|
652 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
653 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
654 | |
---|
655 | return bpp; |
---|
656 | } |
---|
657 | |
---|
658 | Double TEncRCPic::getLCUEstLambda( Double bpp ) |
---|
659 | { |
---|
660 | Int LCUIdx = getLCUCoded(); |
---|
661 | Double alpha; |
---|
662 | Double beta; |
---|
663 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
664 | { |
---|
665 | alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
666 | beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
667 | } |
---|
668 | else |
---|
669 | { |
---|
670 | alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
671 | beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
672 | } |
---|
673 | |
---|
674 | Double estLambda = alpha * pow( bpp, beta ); |
---|
675 | //for Lambda clip, picture level clip |
---|
676 | Double clipPicLambda = m_estPicLambda; |
---|
677 | |
---|
678 | //for Lambda clip, LCU level clip |
---|
679 | Double clipNeighbourLambda = -1.0; |
---|
680 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
---|
681 | { |
---|
682 | if ( m_LCUs[i].m_lambda > 0 ) |
---|
683 | { |
---|
684 | clipNeighbourLambda = m_LCUs[i].m_lambda; |
---|
685 | break; |
---|
686 | } |
---|
687 | } |
---|
688 | |
---|
689 | if ( clipNeighbourLambda > 0.0 ) |
---|
690 | { |
---|
691 | estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda ); |
---|
692 | } |
---|
693 | |
---|
694 | if ( clipPicLambda > 0.0 ) |
---|
695 | { |
---|
696 | estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda ); |
---|
697 | } |
---|
698 | else |
---|
699 | { |
---|
700 | estLambda = Clip3( 10.0, 1000.0, estLambda ); |
---|
701 | } |
---|
702 | |
---|
703 | if ( estLambda < 0.1 ) |
---|
704 | { |
---|
705 | estLambda = 0.1; |
---|
706 | } |
---|
707 | |
---|
708 | return estLambda; |
---|
709 | } |
---|
710 | |
---|
711 | Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP ) |
---|
712 | { |
---|
713 | Int LCUIdx = getLCUCoded(); |
---|
714 | Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
715 | |
---|
716 | //for Lambda clip, LCU level clip |
---|
717 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
718 | #if L0033_RC_BUGFIX |
---|
719 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
---|
720 | #else |
---|
721 | for ( int i=LCUIdx; i>=0; i-- ) |
---|
722 | #endif |
---|
723 | { |
---|
724 | if ( (getLCU(i)).m_QP > g_RCInvalidQPValue ) |
---|
725 | { |
---|
726 | clipNeighbourQP = getLCU(i).m_QP; |
---|
727 | break; |
---|
728 | } |
---|
729 | } |
---|
730 | |
---|
731 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
732 | { |
---|
733 | estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP ); |
---|
734 | } |
---|
735 | |
---|
736 | estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP ); |
---|
737 | |
---|
738 | return estQP; |
---|
739 | } |
---|
740 | |
---|
741 | Void TEncRCPic::updateAfterLCU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter ) |
---|
742 | { |
---|
743 | m_LCUs[LCUIdx].m_actualBits = bits; |
---|
744 | m_LCUs[LCUIdx].m_QP = QP; |
---|
745 | m_LCUs[LCUIdx].m_lambda = lambda; |
---|
746 | |
---|
747 | m_LCULeft--; |
---|
748 | m_bitsLeft -= bits; |
---|
749 | m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel; |
---|
750 | |
---|
751 | if ( !updateLCUParameter ) |
---|
752 | { |
---|
753 | return; |
---|
754 | } |
---|
755 | |
---|
756 | if ( !m_encRCSeq->getUseLCUSeparateModel() ) |
---|
757 | { |
---|
758 | return; |
---|
759 | } |
---|
760 | |
---|
761 | Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
762 | Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
763 | |
---|
764 | Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits; |
---|
765 | Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel; |
---|
766 | Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels; |
---|
767 | Double calLambda = alpha * pow( bpp, beta ); |
---|
768 | Double inputLambda = m_LCUs[LCUIdx].m_lambda; |
---|
769 | |
---|
770 | if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 ) |
---|
771 | { |
---|
772 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
773 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
774 | |
---|
775 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
776 | beta = Clip3( -3.0, -0.1, beta ); |
---|
777 | |
---|
778 | TRCParameter rcPara; |
---|
779 | rcPara.m_alpha = alpha; |
---|
780 | rcPara.m_beta = beta; |
---|
781 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
782 | |
---|
783 | return; |
---|
784 | } |
---|
785 | |
---|
786 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
787 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
788 | double lnbpp = log( bpp ); |
---|
789 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
---|
790 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
791 | |
---|
792 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
793 | beta = Clip3( -3.0, -0.1, beta ); |
---|
794 | TRCParameter rcPara; |
---|
795 | rcPara.m_alpha = alpha; |
---|
796 | rcPara.m_beta = beta; |
---|
797 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
798 | |
---|
799 | } |
---|
800 | |
---|
801 | Double TEncRCPic::getEffectivePercentage() |
---|
802 | { |
---|
803 | Int effectivePiexels = 0; |
---|
804 | Int totalPixels = 0; |
---|
805 | |
---|
806 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
807 | { |
---|
808 | totalPixels += m_LCUs[i].m_numberOfPixel; |
---|
809 | if ( m_LCUs[i].m_QP > 0 ) |
---|
810 | { |
---|
811 | effectivePiexels += m_LCUs[i].m_numberOfPixel; |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | Double effectivePixelPercentage = (Double)effectivePiexels/(Double)totalPixels; |
---|
816 | return effectivePixelPercentage; |
---|
817 | } |
---|
818 | |
---|
819 | Double TEncRCPic::calAverageQP() |
---|
820 | { |
---|
821 | Int totalQPs = 0; |
---|
822 | Int numTotalLCUs = 0; |
---|
823 | |
---|
824 | Int i; |
---|
825 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
826 | { |
---|
827 | if ( m_LCUs[i].m_QP > 0 ) |
---|
828 | { |
---|
829 | totalQPs += m_LCUs[i].m_QP; |
---|
830 | numTotalLCUs++; |
---|
831 | } |
---|
832 | } |
---|
833 | |
---|
834 | Double avgQP = 0.0; |
---|
835 | if ( numTotalLCUs == 0 ) |
---|
836 | { |
---|
837 | avgQP = g_RCInvalidQPValue; |
---|
838 | } |
---|
839 | else |
---|
840 | { |
---|
841 | avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs); |
---|
842 | } |
---|
843 | return avgQP; |
---|
844 | } |
---|
845 | |
---|
846 | Double TEncRCPic::calAverageLambda() |
---|
847 | { |
---|
848 | Double totalLambdas = 0.0; |
---|
849 | Int numTotalLCUs = 0; |
---|
850 | |
---|
851 | Int i; |
---|
852 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
853 | { |
---|
854 | if ( m_LCUs[i].m_lambda > 0.01 ) |
---|
855 | { |
---|
856 | totalLambdas += log( m_LCUs[i].m_lambda ); |
---|
857 | numTotalLCUs++; |
---|
858 | } |
---|
859 | } |
---|
860 | |
---|
861 | Double avgLambda; |
---|
862 | if( numTotalLCUs == 0 ) |
---|
863 | { |
---|
864 | avgLambda = -1.0; |
---|
865 | } |
---|
866 | else |
---|
867 | { |
---|
868 | avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs ); |
---|
869 | } |
---|
870 | return avgLambda; |
---|
871 | } |
---|
872 | |
---|
873 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage ) |
---|
874 | { |
---|
875 | m_picActualHeaderBits = actualHeaderBits; |
---|
876 | m_picActualBits = actualTotalBits; |
---|
877 | if ( averageQP > 0.0 ) |
---|
878 | { |
---|
879 | m_picQP = Int( averageQP + 0.5 ); |
---|
880 | } |
---|
881 | else |
---|
882 | { |
---|
883 | m_picQP = g_RCInvalidQPValue; |
---|
884 | } |
---|
885 | m_picLambda = averageLambda; |
---|
886 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
887 | { |
---|
888 | m_totalMAD += m_LCUs[i].m_MAD; |
---|
889 | } |
---|
890 | |
---|
891 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
892 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
893 | |
---|
894 | // update parameters |
---|
895 | Double picActualBits = ( Double )m_picActualBits; |
---|
896 | Double picActualBpp = picActualBits/(Double)m_numberOfPixel; |
---|
897 | Double calLambda = alpha * pow( picActualBpp, beta ); |
---|
898 | Double inputLambda = m_picLambda; |
---|
899 | |
---|
900 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 ) |
---|
901 | { |
---|
902 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
903 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
904 | |
---|
905 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
906 | beta = Clip3( -3.0, -0.1, beta ); |
---|
907 | TRCParameter rcPara; |
---|
908 | rcPara.m_alpha = alpha; |
---|
909 | rcPara.m_beta = beta; |
---|
910 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
911 | |
---|
912 | return; |
---|
913 | } |
---|
914 | |
---|
915 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
916 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
917 | double lnbpp = log( picActualBpp ); |
---|
918 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
---|
919 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
920 | |
---|
921 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
922 | beta = Clip3( -3.0, -0.1, beta ); |
---|
923 | |
---|
924 | TRCParameter rcPara; |
---|
925 | rcPara.m_alpha = alpha; |
---|
926 | rcPara.m_beta = beta; |
---|
927 | |
---|
928 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
929 | } |
---|
930 | |
---|
931 | TEncRateCtrl::TEncRateCtrl() |
---|
932 | { |
---|
933 | m_encRCSeq = NULL; |
---|
934 | m_encRCGOP = NULL; |
---|
935 | m_encRCPic = NULL; |
---|
936 | } |
---|
937 | |
---|
938 | TEncRateCtrl::~TEncRateCtrl() |
---|
939 | { |
---|
940 | destroy(); |
---|
941 | } |
---|
942 | |
---|
943 | Void TEncRateCtrl::destroy() |
---|
944 | { |
---|
945 | if ( m_encRCSeq != NULL ) |
---|
946 | { |
---|
947 | delete m_encRCSeq; |
---|
948 | m_encRCSeq = NULL; |
---|
949 | } |
---|
950 | if ( m_encRCGOP != NULL ) |
---|
951 | { |
---|
952 | delete m_encRCGOP; |
---|
953 | m_encRCGOP = NULL; |
---|
954 | } |
---|
955 | while ( m_listRCPictures.size() > 0 ) |
---|
956 | { |
---|
957 | TEncRCPic* p = m_listRCPictures.front(); |
---|
958 | m_listRCPictures.pop_front(); |
---|
959 | delete p; |
---|
960 | } |
---|
961 | } |
---|
962 | |
---|
963 | Void TEncRateCtrl::init( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Bool keepHierBits, Bool useLCUSeparateModel, GOPEntry GOPList[MAX_GOP] ) |
---|
964 | { |
---|
965 | destroy(); |
---|
966 | |
---|
967 | Bool isLowdelay = true; |
---|
968 | for ( Int i=0; i<GOPSize-1; i++ ) |
---|
969 | { |
---|
970 | if ( GOPList[i].m_POC > GOPList[i+1].m_POC ) |
---|
971 | { |
---|
972 | isLowdelay = false; |
---|
973 | break; |
---|
974 | } |
---|
975 | } |
---|
976 | |
---|
977 | Int numberOfLevel = 1; |
---|
978 | if ( keepHierBits ) |
---|
979 | { |
---|
980 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
981 | } |
---|
982 | if ( !isLowdelay && GOPSize == 8 ) |
---|
983 | { |
---|
984 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
985 | } |
---|
986 | numberOfLevel++; // intra picture |
---|
987 | numberOfLevel++; // non-reference picture |
---|
988 | |
---|
989 | |
---|
990 | Int* bitsRatio; |
---|
991 | bitsRatio = new Int[ GOPSize ]; |
---|
992 | for ( Int i=0; i<GOPSize; i++ ) |
---|
993 | { |
---|
994 | bitsRatio[i] = 10; |
---|
995 | if ( !GOPList[i].m_refPic ) |
---|
996 | { |
---|
997 | bitsRatio[i] = 2; |
---|
998 | } |
---|
999 | } |
---|
1000 | if ( keepHierBits ) |
---|
1001 | { |
---|
1002 | Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); |
---|
1003 | if ( GOPSize == 4 && isLowdelay ) |
---|
1004 | { |
---|
1005 | if ( bpp > 0.2 ) |
---|
1006 | { |
---|
1007 | bitsRatio[0] = 2; |
---|
1008 | bitsRatio[1] = 3; |
---|
1009 | bitsRatio[2] = 2; |
---|
1010 | bitsRatio[3] = 6; |
---|
1011 | } |
---|
1012 | else if( bpp > 0.1 ) |
---|
1013 | { |
---|
1014 | bitsRatio[0] = 2; |
---|
1015 | bitsRatio[1] = 3; |
---|
1016 | bitsRatio[2] = 2; |
---|
1017 | bitsRatio[3] = 10; |
---|
1018 | } |
---|
1019 | else if ( bpp > 0.05 ) |
---|
1020 | { |
---|
1021 | bitsRatio[0] = 2; |
---|
1022 | bitsRatio[1] = 3; |
---|
1023 | bitsRatio[2] = 2; |
---|
1024 | bitsRatio[3] = 12; |
---|
1025 | } |
---|
1026 | else |
---|
1027 | { |
---|
1028 | bitsRatio[0] = 2; |
---|
1029 | bitsRatio[1] = 3; |
---|
1030 | bitsRatio[2] = 2; |
---|
1031 | bitsRatio[3] = 14; |
---|
1032 | } |
---|
1033 | } |
---|
1034 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
1035 | { |
---|
1036 | if ( bpp > 0.2 ) |
---|
1037 | { |
---|
1038 | bitsRatio[0] = 15; |
---|
1039 | bitsRatio[1] = 5; |
---|
1040 | bitsRatio[2] = 4; |
---|
1041 | bitsRatio[3] = 1; |
---|
1042 | bitsRatio[4] = 1; |
---|
1043 | bitsRatio[5] = 4; |
---|
1044 | bitsRatio[6] = 1; |
---|
1045 | bitsRatio[7] = 1; |
---|
1046 | } |
---|
1047 | else if ( bpp > 0.1 ) |
---|
1048 | { |
---|
1049 | bitsRatio[0] = 20; |
---|
1050 | bitsRatio[1] = 6; |
---|
1051 | bitsRatio[2] = 4; |
---|
1052 | bitsRatio[3] = 1; |
---|
1053 | bitsRatio[4] = 1; |
---|
1054 | bitsRatio[5] = 4; |
---|
1055 | bitsRatio[6] = 1; |
---|
1056 | bitsRatio[7] = 1; |
---|
1057 | } |
---|
1058 | else if ( bpp > 0.05 ) |
---|
1059 | { |
---|
1060 | bitsRatio[0] = 25; |
---|
1061 | bitsRatio[1] = 7; |
---|
1062 | bitsRatio[2] = 4; |
---|
1063 | bitsRatio[3] = 1; |
---|
1064 | bitsRatio[4] = 1; |
---|
1065 | bitsRatio[5] = 4; |
---|
1066 | bitsRatio[6] = 1; |
---|
1067 | bitsRatio[7] = 1; |
---|
1068 | } |
---|
1069 | else |
---|
1070 | { |
---|
1071 | bitsRatio[0] = 30; |
---|
1072 | bitsRatio[1] = 8; |
---|
1073 | bitsRatio[2] = 4; |
---|
1074 | bitsRatio[3] = 1; |
---|
1075 | bitsRatio[4] = 1; |
---|
1076 | bitsRatio[5] = 4; |
---|
1077 | bitsRatio[6] = 1; |
---|
1078 | bitsRatio[7] = 1; |
---|
1079 | } |
---|
1080 | } |
---|
1081 | else |
---|
1082 | { |
---|
1083 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." ); |
---|
1084 | } |
---|
1085 | } |
---|
1086 | |
---|
1087 | Int* GOPID2Level = new int[ GOPSize ]; |
---|
1088 | for ( int i=0; i<GOPSize; i++ ) |
---|
1089 | { |
---|
1090 | GOPID2Level[i] = 1; |
---|
1091 | if ( !GOPList[i].m_refPic ) |
---|
1092 | { |
---|
1093 | GOPID2Level[i] = 2; |
---|
1094 | } |
---|
1095 | } |
---|
1096 | if ( keepHierBits ) |
---|
1097 | { |
---|
1098 | if ( GOPSize == 4 && isLowdelay ) |
---|
1099 | { |
---|
1100 | GOPID2Level[0] = 3; |
---|
1101 | GOPID2Level[1] = 2; |
---|
1102 | GOPID2Level[2] = 3; |
---|
1103 | GOPID2Level[3] = 1; |
---|
1104 | } |
---|
1105 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
1106 | { |
---|
1107 | GOPID2Level[0] = 1; |
---|
1108 | GOPID2Level[1] = 2; |
---|
1109 | GOPID2Level[2] = 3; |
---|
1110 | GOPID2Level[3] = 4; |
---|
1111 | GOPID2Level[4] = 4; |
---|
1112 | GOPID2Level[5] = 3; |
---|
1113 | GOPID2Level[6] = 4; |
---|
1114 | GOPID2Level[7] = 4; |
---|
1115 | } |
---|
1116 | } |
---|
1117 | |
---|
1118 | if ( !isLowdelay && GOPSize == 8 ) |
---|
1119 | { |
---|
1120 | GOPID2Level[0] = 1; |
---|
1121 | GOPID2Level[1] = 2; |
---|
1122 | GOPID2Level[2] = 3; |
---|
1123 | GOPID2Level[3] = 4; |
---|
1124 | GOPID2Level[4] = 4; |
---|
1125 | GOPID2Level[5] = 3; |
---|
1126 | GOPID2Level[6] = 4; |
---|
1127 | GOPID2Level[7] = 4; |
---|
1128 | } |
---|
1129 | |
---|
1130 | m_encRCSeq = new TEncRCSeq; |
---|
1131 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel ); |
---|
1132 | m_encRCSeq->initBitsRatio( bitsRatio ); |
---|
1133 | m_encRCSeq->initGOPID2Level( GOPID2Level ); |
---|
1134 | m_encRCSeq->initPicPara(); |
---|
1135 | if ( useLCUSeparateModel ) |
---|
1136 | { |
---|
1137 | m_encRCSeq->initLCUPara(); |
---|
1138 | } |
---|
1139 | |
---|
1140 | delete[] bitsRatio; |
---|
1141 | delete[] GOPID2Level; |
---|
1142 | } |
---|
1143 | |
---|
1144 | Void TEncRateCtrl::initRCPic( Int frameLevel ) |
---|
1145 | { |
---|
1146 | m_encRCPic = new TEncRCPic; |
---|
1147 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures ); |
---|
1148 | } |
---|
1149 | |
---|
1150 | Void TEncRateCtrl::initRCGOP( Int numberOfPictures ) |
---|
1151 | { |
---|
1152 | m_encRCGOP = new TEncRCGOP; |
---|
1153 | m_encRCGOP->create( m_encRCSeq, numberOfPictures ); |
---|
1154 | } |
---|
1155 | |
---|
1156 | Void TEncRateCtrl::destroyRCGOP() |
---|
1157 | { |
---|
1158 | delete m_encRCGOP; |
---|
1159 | m_encRCGOP = NULL; |
---|
1160 | } |
---|
1161 | |
---|
1162 | #else |
---|
1163 | |
---|
1164 | #define ADJUSTMENT_FACTOR 0.60 |
---|
1165 | #define HIGH_QSTEP_THRESHOLD 9.5238 |
---|
1166 | #define HIGH_QSTEP_ALPHA 4.9371 |
---|
1167 | #define HIGH_QSTEP_BETA 0.0922 |
---|
1168 | #define LOW_QSTEP_ALPHA 16.7429 |
---|
1169 | #define LOW_QSTEP_BETA -1.1494 |
---|
1170 | |
---|
1171 | #define MAD_PRED_Y1 1.0 |
---|
1172 | #define MAD_PRED_Y2 0.0 |
---|
1173 | |
---|
1174 | enum MAD_HISOTRY { |
---|
1175 | MAD_PPPrevious = 0, |
---|
1176 | MAD_PPrevious = 1, |
---|
1177 | MAD_Previous = 2 |
---|
1178 | }; |
---|
1179 | |
---|
1180 | Void MADLinearModel::initMADLinearModel() |
---|
1181 | { |
---|
1182 | m_activeOn = false; |
---|
1183 | m_paramY1 = 1.0; |
---|
1184 | m_paramY2 = 0.0; |
---|
1185 | m_costMADs[0] = m_costMADs[1] = m_costMADs[2] = 0.0; |
---|
1186 | } |
---|
1187 | |
---|
1188 | Double MADLinearModel::getMAD() |
---|
1189 | { |
---|
1190 | Double costPredMAD = m_paramY1 * m_costMADs[MAD_Previous] + m_paramY2; |
---|
1191 | |
---|
1192 | if(costPredMAD < 0) |
---|
1193 | { |
---|
1194 | costPredMAD = m_costMADs[MAD_Previous]; |
---|
1195 | m_paramY1 = MAD_PRED_Y1; |
---|
1196 | m_paramY2 = MAD_PRED_Y2; |
---|
1197 | } |
---|
1198 | return costPredMAD; |
---|
1199 | } |
---|
1200 | |
---|
1201 | Void MADLinearModel::updateMADLiearModel() |
---|
1202 | { |
---|
1203 | Double dNewY1 = ((m_costMADs[MAD_Previous] - m_costMADs[MAD_PPrevious]) / (m_costMADs[MAD_PPrevious] - m_costMADs[MAD_PPPrevious])); |
---|
1204 | Double dNewY2 = (m_costMADs[MAD_Previous] - (dNewY1*m_costMADs[MAD_PPrevious])); |
---|
1205 | |
---|
1206 | m_paramY1 = 0.70+0.20*m_paramY1+ 0.10*dNewY1; |
---|
1207 | m_paramY2 = 0.20*m_paramY2+ 0.10*dNewY2; |
---|
1208 | } |
---|
1209 | |
---|
1210 | Void MADLinearModel::updateMADHistory(Double dMAD) |
---|
1211 | { |
---|
1212 | m_costMADs[MAD_PPPrevious] = m_costMADs[MAD_PPrevious]; |
---|
1213 | m_costMADs[MAD_PPrevious ] = m_costMADs[MAD_Previous ]; |
---|
1214 | m_costMADs[MAD_Previous ] = dMAD; |
---|
1215 | m_activeOn = (m_costMADs[MAD_Previous ] && m_costMADs[MAD_PPrevious ] && m_costMADs[MAD_PPPrevious]); |
---|
1216 | } |
---|
1217 | |
---|
1218 | |
---|
1219 | Void PixelBaseURQQuadraticModel::initPixelBaseQuadraticModel() |
---|
1220 | { |
---|
1221 | m_paramHighX1 = HIGH_QSTEP_ALPHA; |
---|
1222 | m_paramHighX2 = HIGH_QSTEP_BETA; |
---|
1223 | m_paramLowX1 = LOW_QSTEP_ALPHA; |
---|
1224 | m_paramLowX2 = LOW_QSTEP_BETA; |
---|
1225 | } |
---|
1226 | |
---|
1227 | Int PixelBaseURQQuadraticModel::getQP(Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD) |
---|
1228 | { |
---|
1229 | Double qStep; |
---|
1230 | Double bppPerMAD = (Double)(targetBits/(numberOfPixels*costPredMAD)); |
---|
1231 | |
---|
1232 | if(xConvertQP2QStep(qp) >= HIGH_QSTEP_THRESHOLD) |
---|
1233 | { |
---|
1234 | qStep = 1/( sqrt((bppPerMAD/m_paramHighX1)+((m_paramHighX2*m_paramHighX2)/(4*m_paramHighX1*m_paramHighX1))) - (m_paramHighX2/(2*m_paramHighX1))); |
---|
1235 | } |
---|
1236 | else |
---|
1237 | { |
---|
1238 | qStep = 1/( sqrt((bppPerMAD/m_paramLowX1)+((m_paramLowX2*m_paramLowX2)/(4*m_paramLowX1*m_paramLowX1))) - (m_paramLowX2/(2*m_paramLowX1))); |
---|
1239 | } |
---|
1240 | |
---|
1241 | return xConvertQStep2QP(qStep); |
---|
1242 | } |
---|
1243 | |
---|
1244 | Void PixelBaseURQQuadraticModel::updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD) |
---|
1245 | { |
---|
1246 | Double qStep = xConvertQP2QStep(qp); |
---|
1247 | Double invqStep = (1/qStep); |
---|
1248 | Double paramNewX1, paramNewX2; |
---|
1249 | |
---|
1250 | if(qStep >= HIGH_QSTEP_THRESHOLD) |
---|
1251 | { |
---|
1252 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(23.3772*invqStep*invqStep))/((1-200*invqStep)*invqStep)); |
---|
1253 | paramNewX1 = (23.3772-200*paramNewX2); |
---|
1254 | m_paramHighX1 = 0.70*HIGH_QSTEP_ALPHA + 0.20 * m_paramHighX1 + 0.10 * paramNewX1; |
---|
1255 | m_paramHighX2 = 0.70*HIGH_QSTEP_BETA + 0.20 * m_paramHighX2 + 0.10 * paramNewX2; |
---|
1256 | } |
---|
1257 | else |
---|
1258 | { |
---|
1259 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(5.8091*invqStep*invqStep))/((1-9.5455*invqStep)*invqStep)); |
---|
1260 | paramNewX1 = (5.8091-9.5455*paramNewX2); |
---|
1261 | m_paramLowX1 = 0.90*LOW_QSTEP_ALPHA + 0.09 * m_paramLowX1 + 0.01 * paramNewX1; |
---|
1262 | m_paramLowX2 = 0.90*LOW_QSTEP_BETA + 0.09 * m_paramLowX2 + 0.01 * paramNewX2; |
---|
1263 | } |
---|
1264 | } |
---|
1265 | |
---|
1266 | Bool PixelBaseURQQuadraticModel::checkUpdateAvailable(Int qpReference ) |
---|
1267 | { |
---|
1268 | Double qStep = xConvertQP2QStep(qpReference); |
---|
1269 | |
---|
1270 | if (qStep > xConvertQP2QStep(MAX_QP) |
---|
1271 | ||qStep < xConvertQP2QStep(MIN_QP) ) |
---|
1272 | { |
---|
1273 | return false; |
---|
1274 | } |
---|
1275 | |
---|
1276 | return true; |
---|
1277 | } |
---|
1278 | |
---|
1279 | Double PixelBaseURQQuadraticModel::xConvertQP2QStep(Int qp ) |
---|
1280 | { |
---|
1281 | Int i; |
---|
1282 | Double qStep; |
---|
1283 | static const Double mapQP2QSTEP[6] = { 0.625, 0.703, 0.797, 0.891, 1.000, 1.125 }; |
---|
1284 | |
---|
1285 | qStep = mapQP2QSTEP[qp % 6]; |
---|
1286 | for( i=0; i<(qp/6); i++) |
---|
1287 | { |
---|
1288 | qStep *= 2; |
---|
1289 | } |
---|
1290 | |
---|
1291 | return qStep; |
---|
1292 | } |
---|
1293 | |
---|
1294 | Int PixelBaseURQQuadraticModel::xConvertQStep2QP(Double qStep ) |
---|
1295 | { |
---|
1296 | Int per = 0, rem = 0; |
---|
1297 | |
---|
1298 | if( qStep < xConvertQP2QStep(MIN_QP)) |
---|
1299 | { |
---|
1300 | return MIN_QP; |
---|
1301 | } |
---|
1302 | else if (qStep > xConvertQP2QStep(MAX_QP) ) |
---|
1303 | { |
---|
1304 | return MAX_QP; |
---|
1305 | } |
---|
1306 | |
---|
1307 | while( qStep > xConvertQP2QStep(5) ) |
---|
1308 | { |
---|
1309 | qStep /= 2.0; |
---|
1310 | per++; |
---|
1311 | } |
---|
1312 | |
---|
1313 | if (qStep <= 0.625) |
---|
1314 | { |
---|
1315 | rem = 0; |
---|
1316 | } |
---|
1317 | else if (qStep <= 0.703) |
---|
1318 | { |
---|
1319 | rem = 1; |
---|
1320 | } |
---|
1321 | else if (qStep <= 0.797) |
---|
1322 | { |
---|
1323 | rem = 2; |
---|
1324 | } |
---|
1325 | else if (qStep <= 0.891) |
---|
1326 | { |
---|
1327 | rem = 3; |
---|
1328 | } |
---|
1329 | else if (qStep <= 1.000) |
---|
1330 | { |
---|
1331 | rem = 4; |
---|
1332 | } |
---|
1333 | else |
---|
1334 | { |
---|
1335 | rem = 5; |
---|
1336 | } |
---|
1337 | return (per * 6 + rem); |
---|
1338 | } |
---|
1339 | |
---|
1340 | |
---|
1341 | Void TEncRateCtrl::create(Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight) |
---|
1342 | { |
---|
1343 | Int leftInHeight, leftInWidth; |
---|
1344 | |
---|
1345 | m_sourceWidthInLCU = (sourceWidth / maxCUWidth ) + (( sourceWidth % maxCUWidth ) ? 1 : 0); |
---|
1346 | m_sourceHeightInLCU = (sourceHeight / maxCUHeight) + (( sourceHeight % maxCUHeight) ? 1 : 0); |
---|
1347 | m_isLowdelay = (sizeIntraPeriod == -1) ? true : false; |
---|
1348 | m_prevBitrate = ( targetKbps << 10 ); // in units of 1,024 bps |
---|
1349 | m_currBitrate = ( targetKbps << 10 ); |
---|
1350 | m_frameRate = frameRate; |
---|
1351 | m_refFrameNum = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1); |
---|
1352 | m_nonRefFrameNum = sizeGOP-m_refFrameNum; |
---|
1353 | m_sizeGOP = sizeGOP; |
---|
1354 | m_numOfPixels = ((sourceWidth*sourceHeight*3)>>1); |
---|
1355 | m_indexGOP = 0; |
---|
1356 | m_indexFrame = 0; |
---|
1357 | m_indexLCU = 0; |
---|
1358 | m_indexUnit = 0; |
---|
1359 | m_indexRefFrame = 0; |
---|
1360 | m_indexNonRefFrame = 0; |
---|
1361 | m_occupancyVB = 0; |
---|
1362 | m_initialOVB = 0; |
---|
1363 | m_targetBufLevel = 0; |
---|
1364 | m_initialTBL = 0; |
---|
1365 | m_occupancyVBInFrame = 0; |
---|
1366 | m_remainingBitsInGOP = (m_currBitrate*sizeGOP/m_frameRate); |
---|
1367 | m_remainingBitsInFrame = 0; |
---|
1368 | m_numUnitInFrame = m_sourceWidthInLCU*m_sourceHeightInLCU; |
---|
1369 | m_cMADLinearModel. initMADLinearModel(); |
---|
1370 | m_cPixelURQQuadraticModel.initPixelBaseQuadraticModel(); |
---|
1371 | |
---|
1372 | m_costRefAvgWeighting = 0.0; |
---|
1373 | m_costNonRefAvgWeighting = 0.0; |
---|
1374 | m_costAvgbpp = 0.0; |
---|
1375 | m_activeUnitLevelOn = false; |
---|
1376 | |
---|
1377 | m_pcFrameData = new FrameData [sizeGOP+1]; initFrameData(qp); |
---|
1378 | m_pcLCUData = new LCUData [m_numUnitInFrame]; initUnitData (qp); |
---|
1379 | |
---|
1380 | for(Int i = 0, addressUnit = 0; i < m_sourceHeightInLCU*maxCUHeight; i += maxCUHeight) |
---|
1381 | { |
---|
1382 | leftInHeight = sourceHeight - i; |
---|
1383 | leftInHeight = min(leftInHeight, maxCUHeight); |
---|
1384 | for(Int j = 0; j < m_sourceWidthInLCU*maxCUWidth; j += maxCUWidth, addressUnit++) |
---|
1385 | { |
---|
1386 | leftInWidth = sourceWidth - j; |
---|
1387 | leftInWidth = min(leftInWidth, maxCUWidth); |
---|
1388 | m_pcLCUData[addressUnit].m_widthInPixel = leftInWidth; |
---|
1389 | m_pcLCUData[addressUnit].m_heightInPixel= leftInHeight; |
---|
1390 | m_pcLCUData[addressUnit].m_pixels = ((leftInHeight*leftInWidth*3)>>1); |
---|
1391 | } |
---|
1392 | } |
---|
1393 | } |
---|
1394 | |
---|
1395 | Void TEncRateCtrl::destroy() |
---|
1396 | { |
---|
1397 | if(m_pcFrameData) |
---|
1398 | { |
---|
1399 | delete [] m_pcFrameData; |
---|
1400 | m_pcFrameData = NULL; |
---|
1401 | } |
---|
1402 | if(m_pcLCUData) |
---|
1403 | { |
---|
1404 | delete [] m_pcLCUData; |
---|
1405 | m_pcLCUData = NULL; |
---|
1406 | } |
---|
1407 | } |
---|
1408 | |
---|
1409 | Void TEncRateCtrl::initFrameData (Int qp) |
---|
1410 | { |
---|
1411 | for(Int i = 0 ; i <= m_sizeGOP; i++) |
---|
1412 | { |
---|
1413 | m_pcFrameData[i].m_isReferenced = false; |
---|
1414 | m_pcFrameData[i].m_costMAD = 0.0; |
---|
1415 | m_pcFrameData[i].m_bits = 0; |
---|
1416 | m_pcFrameData[i].m_qp = qp; |
---|
1417 | } |
---|
1418 | } |
---|
1419 | |
---|
1420 | Void TEncRateCtrl::initUnitData (Int qp) |
---|
1421 | { |
---|
1422 | for(Int i = 1 ; i < m_numUnitInFrame; i++) |
---|
1423 | { |
---|
1424 | m_pcLCUData[i].m_qp = qp; |
---|
1425 | m_pcLCUData[i].m_bits = 0; |
---|
1426 | m_pcLCUData[i].m_pixels = 0; |
---|
1427 | m_pcLCUData[i].m_widthInPixel = 0; |
---|
1428 | m_pcLCUData[i].m_heightInPixel = 0; |
---|
1429 | m_pcLCUData[i].m_costMAD = 0.0; |
---|
1430 | } |
---|
1431 | } |
---|
1432 | |
---|
1433 | Int TEncRateCtrl::getFrameQP(Bool isReferenced, Int POC) |
---|
1434 | { |
---|
1435 | Int numofReferenced = 0; |
---|
1436 | Int finalQP = 0; |
---|
1437 | FrameData* pcFrameData; |
---|
1438 | |
---|
1439 | m_indexPOCInGOP = (POC%m_sizeGOP) == 0 ? m_sizeGOP : (POC%m_sizeGOP); |
---|
1440 | pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
---|
1441 | |
---|
1442 | if(m_indexFrame != 0) |
---|
1443 | { |
---|
1444 | if(isReferenced) |
---|
1445 | { |
---|
1446 | Double gamma = m_isLowdelay ? 0.5 : 0.25; |
---|
1447 | Double beta = m_isLowdelay ? 0.9 : 0.6; |
---|
1448 | Int numRemainingRefFrames = m_refFrameNum - m_indexRefFrame; |
---|
1449 | Int numRemainingNRefFrames = m_nonRefFrameNum - m_indexNonRefFrame; |
---|
1450 | |
---|
1451 | Double targetBitsOccupancy = (m_currBitrate/(Double)m_frameRate) + gamma*(m_targetBufLevel-m_occupancyVB - (m_initialOVB/(Double)m_frameRate)); |
---|
1452 | Double targetBitsLeftBudget = ((m_costRefAvgWeighting*m_remainingBitsInGOP)/((m_costRefAvgWeighting*numRemainingRefFrames)+(m_costNonRefAvgWeighting*numRemainingNRefFrames))); |
---|
1453 | |
---|
1454 | m_targetBits = (Int)(beta * targetBitsLeftBudget + (1-beta) * targetBitsOccupancy); |
---|
1455 | |
---|
1456 | if(m_targetBits <= 0 || m_remainingBitsInGOP <= 0) |
---|
1457 | { |
---|
1458 | finalQP = m_pcFrameData[m_indexPrevPOCInGOP].m_qp + 2; |
---|
1459 | } |
---|
1460 | else |
---|
1461 | { |
---|
1462 | Double costPredMAD = m_cMADLinearModel.getMAD(); |
---|
1463 | Int qpLowerBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp-2; |
---|
1464 | Int qpUpperBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp+2; |
---|
1465 | finalQP = m_cPixelURQQuadraticModel.getQP(m_pcFrameData[m_indexPrevPOCInGOP].m_qp, m_targetBits, m_numOfPixels, costPredMAD); |
---|
1466 | finalQP = max(qpLowerBound, min(qpUpperBound, finalQP)); |
---|
1467 | m_activeUnitLevelOn = true; |
---|
1468 | m_remainingBitsInFrame = m_targetBits; |
---|
1469 | m_costAvgbpp = (m_targetBits/(Double)m_numOfPixels); |
---|
1470 | } |
---|
1471 | |
---|
1472 | m_indexRefFrame++; |
---|
1473 | } |
---|
1474 | else |
---|
1475 | { |
---|
1476 | Int bwdQP = m_pcFrameData[m_indexPOCInGOP-1].m_qp; |
---|
1477 | Int fwdQP = m_pcFrameData[m_indexPOCInGOP+1].m_qp; |
---|
1478 | |
---|
1479 | if( (fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP-1].m_qp |
---|
1480 | ||(fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP+1].m_qp) |
---|
1481 | { |
---|
1482 | finalQP = (fwdQP+bwdQP); |
---|
1483 | } |
---|
1484 | else if(bwdQP != fwdQP) |
---|
1485 | { |
---|
1486 | finalQP = ((bwdQP+fwdQP+2)>>1); |
---|
1487 | } |
---|
1488 | else |
---|
1489 | { |
---|
1490 | finalQP = bwdQP+2; |
---|
1491 | } |
---|
1492 | m_indexNonRefFrame++; |
---|
1493 | } |
---|
1494 | } |
---|
1495 | else |
---|
1496 | { |
---|
1497 | Int lastQPminus2 = m_pcFrameData[0].m_qp - 2; |
---|
1498 | Int lastQPplus2 = m_pcFrameData[0].m_qp + 2; |
---|
1499 | |
---|
1500 | for(Int idx = 1; idx <= m_sizeGOP; idx++) |
---|
1501 | { |
---|
1502 | if(m_pcFrameData[idx].m_isReferenced) |
---|
1503 | { |
---|
1504 | finalQP += m_pcFrameData[idx].m_qp; |
---|
1505 | numofReferenced++; |
---|
1506 | } |
---|
1507 | } |
---|
1508 | |
---|
1509 | finalQP = (numofReferenced == 0) ? m_pcFrameData[0].m_qp : ((finalQP + (1<<(numofReferenced>>1)))/numofReferenced); |
---|
1510 | finalQP = max( lastQPminus2, min( lastQPplus2, finalQP)); |
---|
1511 | |
---|
1512 | Double costAvgFrameBits = m_remainingBitsInGOP/(Double)m_sizeGOP; |
---|
1513 | Int bufLevel = m_occupancyVB + m_initialOVB; |
---|
1514 | |
---|
1515 | if(abs(bufLevel) > costAvgFrameBits) |
---|
1516 | { |
---|
1517 | if(bufLevel < 0) |
---|
1518 | { |
---|
1519 | finalQP -= 2; |
---|
1520 | } |
---|
1521 | else |
---|
1522 | { |
---|
1523 | finalQP += 2; |
---|
1524 | } |
---|
1525 | } |
---|
1526 | m_indexRefFrame++; |
---|
1527 | } |
---|
1528 | finalQP = max(MIN_QP, min(MAX_QP, finalQP)); |
---|
1529 | |
---|
1530 | for(Int indexLCU = 0 ; indexLCU < m_numUnitInFrame; indexLCU++) |
---|
1531 | { |
---|
1532 | m_pcLCUData[indexLCU].m_qp = finalQP; |
---|
1533 | } |
---|
1534 | |
---|
1535 | pcFrameData->m_isReferenced = isReferenced; |
---|
1536 | pcFrameData->m_qp = finalQP; |
---|
1537 | |
---|
1538 | return finalQP; |
---|
1539 | } |
---|
1540 | |
---|
1541 | Bool TEncRateCtrl::calculateUnitQP () |
---|
1542 | { |
---|
1543 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
---|
1544 | { |
---|
1545 | return false; |
---|
1546 | } |
---|
1547 | Int upperQPBound, lowerQPBound, finalQP; |
---|
1548 | Int colQP = m_pcLCUData[m_indexLCU].m_qp; |
---|
1549 | Double colMAD = m_pcLCUData[m_indexLCU].m_costMAD; |
---|
1550 | Double budgetInUnit = m_pcLCUData[m_indexLCU].m_pixels*m_costAvgbpp; |
---|
1551 | |
---|
1552 | |
---|
1553 | Int targetBitsOccupancy = (Int)(budgetInUnit - (m_occupancyVBInFrame/(m_numUnitInFrame-m_indexUnit))); |
---|
1554 | Int targetBitsLeftBudget= (Int)((m_remainingBitsInFrame*m_pcLCUData[m_indexLCU].m_pixels)/(Double)(m_numOfPixels-m_codedPixels)); |
---|
1555 | Int targetBits = (targetBitsLeftBudget>>1) + (targetBitsOccupancy>>1); |
---|
1556 | |
---|
1557 | |
---|
1558 | if( m_indexLCU >= m_sourceWidthInLCU) |
---|
1559 | { |
---|
1560 | upperQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) + MAX_DELTA_QP; |
---|
1561 | lowerQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) - MAX_DELTA_QP; |
---|
1562 | } |
---|
1563 | else |
---|
1564 | { |
---|
1565 | upperQPBound = m_pcLCUData[m_indexLCU-1].m_qp + MAX_DELTA_QP; |
---|
1566 | lowerQPBound = m_pcLCUData[m_indexLCU-1].m_qp - MAX_DELTA_QP; |
---|
1567 | } |
---|
1568 | |
---|
1569 | if(targetBits < 0) |
---|
1570 | { |
---|
1571 | finalQP = m_pcLCUData[m_indexLCU-1].m_qp + 1; |
---|
1572 | } |
---|
1573 | else |
---|
1574 | { |
---|
1575 | finalQP = m_cPixelURQQuadraticModel.getQP(colQP, targetBits, m_pcLCUData[m_indexLCU].m_pixels, colMAD); |
---|
1576 | } |
---|
1577 | |
---|
1578 | finalQP = max(lowerQPBound, min(upperQPBound, finalQP)); |
---|
1579 | m_pcLCUData[m_indexLCU].m_qp = max(MIN_QP, min(MAX_QP, finalQP)); |
---|
1580 | |
---|
1581 | return true; |
---|
1582 | } |
---|
1583 | |
---|
1584 | Void TEncRateCtrl::updateRCGOPStatus() |
---|
1585 | { |
---|
1586 | m_remainingBitsInGOP = ((m_currBitrate/m_frameRate)*m_sizeGOP) - m_occupancyVB; |
---|
1587 | |
---|
1588 | FrameData cFrameData = m_pcFrameData[m_sizeGOP]; |
---|
1589 | initFrameData(); |
---|
1590 | |
---|
1591 | m_pcFrameData[0] = cFrameData; |
---|
1592 | m_indexGOP++; |
---|
1593 | m_indexFrame = 0; |
---|
1594 | m_indexRefFrame = 0; |
---|
1595 | m_indexNonRefFrame = 0; |
---|
1596 | } |
---|
1597 | |
---|
1598 | Void TEncRateCtrl::updataRCFrameStatus(Int frameBits, SliceType eSliceType) |
---|
1599 | { |
---|
1600 | FrameData* pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
---|
1601 | Int occupancyBits; |
---|
1602 | Double adjustmentBits; |
---|
1603 | |
---|
1604 | m_remainingBitsInGOP = m_remainingBitsInGOP + ( ((m_currBitrate-m_prevBitrate)/m_frameRate)*(m_sizeGOP-m_indexFrame) ) - frameBits; |
---|
1605 | occupancyBits = (Int)((Double)frameBits - (m_currBitrate/(Double)m_frameRate)); |
---|
1606 | |
---|
1607 | if( (occupancyBits < 0) && (m_initialOVB > 0) ) |
---|
1608 | { |
---|
1609 | adjustmentBits = xAdjustmentBits(occupancyBits, m_initialOVB ); |
---|
1610 | |
---|
1611 | if(m_initialOVB < 0) |
---|
1612 | { |
---|
1613 | adjustmentBits = m_initialOVB; |
---|
1614 | occupancyBits += (Int)adjustmentBits; |
---|
1615 | m_initialOVB = 0; |
---|
1616 | } |
---|
1617 | } |
---|
1618 | else if( (occupancyBits > 0) && (m_initialOVB < 0) ) |
---|
1619 | { |
---|
1620 | adjustmentBits = xAdjustmentBits(m_initialOVB, occupancyBits ); |
---|
1621 | |
---|
1622 | if(occupancyBits < 0) |
---|
1623 | { |
---|
1624 | adjustmentBits = occupancyBits; |
---|
1625 | m_initialOVB += (Int)adjustmentBits; |
---|
1626 | occupancyBits = 0; |
---|
1627 | } |
---|
1628 | } |
---|
1629 | |
---|
1630 | if(m_indexGOP == 0) |
---|
1631 | { |
---|
1632 | m_initialOVB = occupancyBits; |
---|
1633 | } |
---|
1634 | else |
---|
1635 | { |
---|
1636 | m_occupancyVB= m_occupancyVB + occupancyBits; |
---|
1637 | } |
---|
1638 | |
---|
1639 | if(pcFrameData->m_isReferenced) |
---|
1640 | { |
---|
1641 | m_costRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costRefAvgWeighting)/8.0); |
---|
1642 | |
---|
1643 | if(m_indexFrame == 0) |
---|
1644 | { |
---|
1645 | m_initialTBL = m_targetBufLevel = (frameBits - (m_currBitrate/m_frameRate)); |
---|
1646 | } |
---|
1647 | else |
---|
1648 | { |
---|
1649 | Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1; |
---|
1650 | m_targetBufLevel = m_targetBufLevel |
---|
1651 | - (m_initialTBL/(m_refFrameNum-1)) |
---|
1652 | + (Int)((m_costRefAvgWeighting*(distance+1)*m_currBitrate)/(m_frameRate*(m_costRefAvgWeighting+(m_costNonRefAvgWeighting*distance)))) |
---|
1653 | - (m_currBitrate/m_frameRate); |
---|
1654 | } |
---|
1655 | |
---|
1656 | if(m_cMADLinearModel.IsUpdateAvailable()) |
---|
1657 | { |
---|
1658 | m_cMADLinearModel.updateMADLiearModel(); |
---|
1659 | } |
---|
1660 | |
---|
1661 | if(eSliceType != I_SLICE && |
---|
1662 | m_cPixelURQQuadraticModel.checkUpdateAvailable(pcFrameData->m_qp)) |
---|
1663 | { |
---|
1664 | m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(pcFrameData->m_qp, pcFrameData->m_bits, m_numOfPixels, pcFrameData->m_costMAD); |
---|
1665 | } |
---|
1666 | } |
---|
1667 | else |
---|
1668 | { |
---|
1669 | m_costNonRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costNonRefAvgWeighting)/8.0); |
---|
1670 | } |
---|
1671 | |
---|
1672 | m_indexFrame++; |
---|
1673 | m_indexLCU = 0; |
---|
1674 | m_indexUnit = 0; |
---|
1675 | m_occupancyVBInFrame = 0; |
---|
1676 | m_remainingBitsInFrame = 0; |
---|
1677 | m_codedPixels = 0; |
---|
1678 | m_activeUnitLevelOn = false; |
---|
1679 | m_costAvgbpp = 0.0; |
---|
1680 | } |
---|
1681 | Void TEncRateCtrl::updataRCUnitStatus () |
---|
1682 | { |
---|
1683 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
---|
1684 | { |
---|
1685 | return; |
---|
1686 | } |
---|
1687 | |
---|
1688 | m_codedPixels += m_pcLCUData[m_indexLCU-1].m_pixels; |
---|
1689 | m_remainingBitsInFrame = m_remainingBitsInFrame - m_pcLCUData[m_indexLCU-1].m_bits; |
---|
1690 | m_occupancyVBInFrame = (Int)(m_occupancyVBInFrame + m_pcLCUData[m_indexLCU-1].m_bits - m_pcLCUData[m_indexLCU-1].m_pixels*m_costAvgbpp); |
---|
1691 | |
---|
1692 | if( m_cPixelURQQuadraticModel.checkUpdateAvailable(m_pcLCUData[m_indexLCU-1].m_qp) ) |
---|
1693 | { |
---|
1694 | m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(m_pcLCUData[m_indexLCU-1].m_qp, m_pcLCUData[m_indexLCU-1].m_bits, m_pcLCUData[m_indexLCU-1].m_pixels, m_pcLCUData[m_indexLCU-1].m_costMAD); |
---|
1695 | } |
---|
1696 | |
---|
1697 | m_indexUnit++; |
---|
1698 | } |
---|
1699 | |
---|
1700 | Void TEncRateCtrl::updateFrameData(UInt64 actualFrameBits) |
---|
1701 | { |
---|
1702 | Double costMAD = 0.0; |
---|
1703 | |
---|
1704 | for(Int i = 0; i < m_numUnitInFrame; i++) |
---|
1705 | { |
---|
1706 | costMAD += m_pcLCUData[i].m_costMAD; |
---|
1707 | } |
---|
1708 | |
---|
1709 | m_pcFrameData[m_indexPOCInGOP].m_costMAD = (costMAD/(Double)m_numUnitInFrame); |
---|
1710 | m_pcFrameData[m_indexPOCInGOP].m_bits = (Int)actualFrameBits; |
---|
1711 | |
---|
1712 | if(m_pcFrameData[m_indexPOCInGOP].m_isReferenced) |
---|
1713 | { |
---|
1714 | m_indexPrevPOCInGOP = m_indexPOCInGOP; |
---|
1715 | m_cMADLinearModel.updateMADHistory(m_pcFrameData[m_indexPOCInGOP].m_costMAD); |
---|
1716 | } |
---|
1717 | } |
---|
1718 | |
---|
1719 | Void TEncRateCtrl::updateLCUData(TComDataCU* pcCU, UInt64 actualLCUBits, Int qp) |
---|
1720 | { |
---|
1721 | Int x, y; |
---|
1722 | Double costMAD = 0.0; |
---|
1723 | |
---|
1724 | Pel* pOrg = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1725 | Pel* pRec = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1726 | Int stride = pcCU->getPic()->getStride(); |
---|
1727 | |
---|
1728 | Int width = m_pcLCUData[m_indexLCU].m_widthInPixel; |
---|
1729 | Int height = m_pcLCUData[m_indexLCU].m_heightInPixel; |
---|
1730 | |
---|
1731 | for( y = 0; y < height; y++ ) |
---|
1732 | { |
---|
1733 | for( x = 0; x < width; x++ ) |
---|
1734 | { |
---|
1735 | costMAD += abs( pOrg[x] - pRec[x] ); |
---|
1736 | } |
---|
1737 | pOrg += stride; |
---|
1738 | pRec += stride; |
---|
1739 | } |
---|
1740 | m_pcLCUData[m_indexLCU ].m_qp = qp; |
---|
1741 | m_pcLCUData[m_indexLCU ].m_costMAD = (costMAD /(Double)(width*height)); |
---|
1742 | m_pcLCUData[m_indexLCU++].m_bits = (Int)actualLCUBits; |
---|
1743 | } |
---|
1744 | |
---|
1745 | Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits) |
---|
1746 | { |
---|
1747 | Double adjustment = ADJUSTMENT_FACTOR*reductionBits; |
---|
1748 | reductionBits -= (Int)adjustment; |
---|
1749 | compensationBits += (Int)adjustment; |
---|
1750 | |
---|
1751 | return adjustment; |
---|
1752 | } |
---|
1753 | |
---|
1754 | #endif |
---|
1755 | |
---|