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-2016, 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 | |
---|
43 | using namespace std; |
---|
44 | |
---|
45 | //sequence level |
---|
46 | TEncRCSeq::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 | |
---|
72 | TEncRCSeq::~TEncRCSeq() |
---|
73 | { |
---|
74 | destroy(); |
---|
75 | } |
---|
76 | |
---|
77 | Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel, Int adaptiveBit ) |
---|
78 | { |
---|
79 | destroy(); |
---|
80 | m_totalFrames = totalFrames; |
---|
81 | m_targetRate = targetBitrate; |
---|
82 | m_frameRate = frameRate; |
---|
83 | m_GOPSize = GOPSize; |
---|
84 | m_picWidth = picWidth; |
---|
85 | m_picHeight = picHeight; |
---|
86 | m_LCUWidth = LCUWidth; |
---|
87 | m_LCUHeight = LCUHeight; |
---|
88 | m_numberOfLevel = numberOfLevel; |
---|
89 | m_useLCUSeparateModel = useLCUSeparateModel; |
---|
90 | |
---|
91 | m_numberOfPixel = m_picWidth * m_picHeight; |
---|
92 | m_targetBits = (Int64)m_totalFrames * (Int64)m_targetRate / (Int64)m_frameRate; |
---|
93 | m_seqTargetBpp = (Double)m_targetRate / (Double)m_frameRate / (Double)m_numberOfPixel; |
---|
94 | if ( m_seqTargetBpp < 0.03 ) |
---|
95 | { |
---|
96 | m_alphaUpdate = 0.01; |
---|
97 | m_betaUpdate = 0.005; |
---|
98 | } |
---|
99 | else if ( m_seqTargetBpp < 0.08 ) |
---|
100 | { |
---|
101 | m_alphaUpdate = 0.05; |
---|
102 | m_betaUpdate = 0.025; |
---|
103 | } |
---|
104 | else if ( m_seqTargetBpp < 0.2 ) |
---|
105 | { |
---|
106 | m_alphaUpdate = 0.1; |
---|
107 | m_betaUpdate = 0.05; |
---|
108 | } |
---|
109 | else if ( m_seqTargetBpp < 0.5 ) |
---|
110 | { |
---|
111 | m_alphaUpdate = 0.2; |
---|
112 | m_betaUpdate = 0.1; |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | m_alphaUpdate = 0.4; |
---|
117 | m_betaUpdate = 0.2; |
---|
118 | } |
---|
119 | |
---|
120 | m_averageBits = (Int)(m_targetBits / totalFrames); |
---|
121 | Int picWidthInBU = ( m_picWidth % m_LCUWidth ) == 0 ? m_picWidth / m_LCUWidth : m_picWidth / m_LCUWidth + 1; |
---|
122 | Int picHeightInBU = ( m_picHeight % m_LCUHeight ) == 0 ? m_picHeight / m_LCUHeight : m_picHeight / m_LCUHeight + 1; |
---|
123 | m_numberOfLCU = picWidthInBU * picHeightInBU; |
---|
124 | |
---|
125 | m_bitsRatio = new Int[m_GOPSize]; |
---|
126 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
127 | { |
---|
128 | m_bitsRatio[i] = 1; |
---|
129 | } |
---|
130 | |
---|
131 | m_GOPID2Level = new Int[m_GOPSize]; |
---|
132 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
133 | { |
---|
134 | m_GOPID2Level[i] = 1; |
---|
135 | } |
---|
136 | |
---|
137 | m_picPara = new TRCParameter[m_numberOfLevel]; |
---|
138 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
139 | { |
---|
140 | m_picPara[i].m_alpha = 0.0; |
---|
141 | m_picPara[i].m_beta = 0.0; |
---|
142 | } |
---|
143 | |
---|
144 | if ( m_useLCUSeparateModel ) |
---|
145 | { |
---|
146 | m_LCUPara = new TRCParameter*[m_numberOfLevel]; |
---|
147 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
148 | { |
---|
149 | m_LCUPara[i] = new TRCParameter[m_numberOfLCU]; |
---|
150 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
151 | { |
---|
152 | m_LCUPara[i][j].m_alpha = 0.0; |
---|
153 | m_LCUPara[i][j].m_beta = 0.0; |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | m_framesLeft = m_totalFrames; |
---|
159 | m_bitsLeft = m_targetBits; |
---|
160 | m_adaptiveBit = adaptiveBit; |
---|
161 | m_lastLambda = 0.0; |
---|
162 | } |
---|
163 | |
---|
164 | Void 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 | |
---|
195 | Void TEncRCSeq::initBitsRatio( Int bitsRatio[]) |
---|
196 | { |
---|
197 | for (Int i=0; i<m_GOPSize; i++) |
---|
198 | { |
---|
199 | m_bitsRatio[i] = bitsRatio[i]; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | Void TEncRCSeq::initGOPID2Level( Int GOPID2Level[] ) |
---|
204 | { |
---|
205 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
206 | { |
---|
207 | m_GOPID2Level[i] = GOPID2Level[i]; |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | Void 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 | |
---|
240 | Void 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 | |
---|
269 | Void TEncRCSeq::updateAfterPic ( Int bits ) |
---|
270 | { |
---|
271 | m_bitsLeft -= bits; |
---|
272 | m_framesLeft--; |
---|
273 | } |
---|
274 | |
---|
275 | Void TEncRCSeq::setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB ) |
---|
276 | { |
---|
277 | Int* bitsRatio = new Int[m_GOPSize]; |
---|
278 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
279 | { |
---|
280 | bitsRatio[i] = (Int)( equaCoeffA[i] * pow( basicLambda, equaCoeffB[i] ) * m_numberOfPixel ); |
---|
281 | } |
---|
282 | initBitsRatio( bitsRatio ); |
---|
283 | delete[] bitsRatio; |
---|
284 | } |
---|
285 | |
---|
286 | //GOP level |
---|
287 | TEncRCGOP::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 | |
---|
297 | TEncRCGOP::~TEncRCGOP() |
---|
298 | { |
---|
299 | destroy(); |
---|
300 | } |
---|
301 | |
---|
302 | Void TEncRCGOP::create( TEncRCSeq* encRCSeq, Int numPic ) |
---|
303 | { |
---|
304 | destroy(); |
---|
305 | Int targetBits = xEstGOPTargetBits( encRCSeq, numPic ); |
---|
306 | |
---|
307 | if ( encRCSeq->getAdaptiveBits() > 0 && encRCSeq->getLastLambda() > 0.1 ) |
---|
308 | { |
---|
309 | Double targetBpp = (Double)targetBits / encRCSeq->getNumPixel(); |
---|
310 | Double basicLambda = 0.0; |
---|
311 | Double* lambdaRatio = new Double[encRCSeq->getGOPSize()]; |
---|
312 | Double* equaCoeffA = new Double[encRCSeq->getGOPSize()]; |
---|
313 | Double* equaCoeffB = new Double[encRCSeq->getGOPSize()]; |
---|
314 | |
---|
315 | if ( encRCSeq->getAdaptiveBits() == 1 ) // for GOP size =4, low delay case |
---|
316 | { |
---|
317 | if ( encRCSeq->getLastLambda() < 120.0 ) |
---|
318 | { |
---|
319 | lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.5793; |
---|
320 | lambdaRatio[0] = 1.3 * lambdaRatio[1]; |
---|
321 | lambdaRatio[2] = 1.3 * lambdaRatio[1]; |
---|
322 | lambdaRatio[3] = 1.0; |
---|
323 | } |
---|
324 | else |
---|
325 | { |
---|
326 | lambdaRatio[0] = 5.0; |
---|
327 | lambdaRatio[1] = 4.0; |
---|
328 | lambdaRatio[2] = 5.0; |
---|
329 | lambdaRatio[3] = 1.0; |
---|
330 | } |
---|
331 | } |
---|
332 | else if ( encRCSeq->getAdaptiveBits() == 2 ) // for GOP size = 8, random access case |
---|
333 | { |
---|
334 | if ( encRCSeq->getLastLambda() < 90.0 ) |
---|
335 | { |
---|
336 | lambdaRatio[0] = 1.0; |
---|
337 | lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.7963; |
---|
338 | lambdaRatio[2] = 1.3 * lambdaRatio[1]; |
---|
339 | lambdaRatio[3] = 3.25 * lambdaRatio[1]; |
---|
340 | lambdaRatio[4] = 3.25 * lambdaRatio[1]; |
---|
341 | lambdaRatio[5] = 1.3 * lambdaRatio[1]; |
---|
342 | lambdaRatio[6] = 3.25 * lambdaRatio[1]; |
---|
343 | lambdaRatio[7] = 3.25 * lambdaRatio[1]; |
---|
344 | } |
---|
345 | else |
---|
346 | { |
---|
347 | lambdaRatio[0] = 1.0; |
---|
348 | lambdaRatio[1] = 4.0; |
---|
349 | lambdaRatio[2] = 5.0; |
---|
350 | lambdaRatio[3] = 12.3; |
---|
351 | lambdaRatio[4] = 12.3; |
---|
352 | lambdaRatio[5] = 5.0; |
---|
353 | lambdaRatio[6] = 12.3; |
---|
354 | lambdaRatio[7] = 12.3; |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | xCalEquaCoeff( encRCSeq, lambdaRatio, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
359 | basicLambda = xSolveEqua( targetBpp, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
360 | encRCSeq->setAllBitRatio( basicLambda, equaCoeffA, equaCoeffB ); |
---|
361 | |
---|
362 | delete []lambdaRatio; |
---|
363 | delete []equaCoeffA; |
---|
364 | delete []equaCoeffB; |
---|
365 | } |
---|
366 | |
---|
367 | m_picTargetBitInGOP = new Int[numPic]; |
---|
368 | Int i; |
---|
369 | Int totalPicRatio = 0; |
---|
370 | Int currPicRatio = 0; |
---|
371 | for ( i=0; i<numPic; i++ ) |
---|
372 | { |
---|
373 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
374 | } |
---|
375 | for ( i=0; i<numPic; i++ ) |
---|
376 | { |
---|
377 | currPicRatio = encRCSeq->getBitRatio( i ); |
---|
378 | m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio ); |
---|
379 | } |
---|
380 | |
---|
381 | m_encRCSeq = encRCSeq; |
---|
382 | m_numPic = numPic; |
---|
383 | m_targetBits = targetBits; |
---|
384 | m_picLeft = m_numPic; |
---|
385 | m_bitsLeft = m_targetBits; |
---|
386 | } |
---|
387 | |
---|
388 | Void TEncRCGOP::xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
389 | { |
---|
390 | for ( Int i=0; i<GOPSize; i++ ) |
---|
391 | { |
---|
392 | Int frameLevel = encRCSeq->getGOPID2Level(i); |
---|
393 | Double alpha = encRCSeq->getPicPara(frameLevel).m_alpha; |
---|
394 | Double beta = encRCSeq->getPicPara(frameLevel).m_beta; |
---|
395 | equaCoeffA[i] = pow( 1.0/alpha, 1.0/beta ) * pow( lambdaRatio[i], 1.0/beta ); |
---|
396 | equaCoeffB[i] = 1.0/beta; |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | Double TEncRCGOP::xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
401 | { |
---|
402 | Double solution = 100.0; |
---|
403 | Double minNumber = 0.1; |
---|
404 | Double maxNumber = 10000.0; |
---|
405 | for ( Int i=0; i<g_RCIterationNum; i++ ) |
---|
406 | { |
---|
407 | Double fx = 0.0; |
---|
408 | for ( Int j=0; j<GOPSize; j++ ) |
---|
409 | { |
---|
410 | fx += equaCoeffA[j] * pow( solution, equaCoeffB[j] ); |
---|
411 | } |
---|
412 | |
---|
413 | if ( fabs( fx - targetBpp ) < 0.000001 ) |
---|
414 | { |
---|
415 | break; |
---|
416 | } |
---|
417 | |
---|
418 | if ( fx > targetBpp ) |
---|
419 | { |
---|
420 | minNumber = solution; |
---|
421 | solution = ( solution + maxNumber ) / 2.0; |
---|
422 | } |
---|
423 | else |
---|
424 | { |
---|
425 | maxNumber = solution; |
---|
426 | solution = ( solution + minNumber ) / 2.0; |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | solution = Clip3( 0.1, 10000.0, solution ); |
---|
431 | return solution; |
---|
432 | } |
---|
433 | |
---|
434 | Void TEncRCGOP::destroy() |
---|
435 | { |
---|
436 | m_encRCSeq = NULL; |
---|
437 | if ( m_picTargetBitInGOP != NULL ) |
---|
438 | { |
---|
439 | delete[] m_picTargetBitInGOP; |
---|
440 | m_picTargetBitInGOP = NULL; |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | Void TEncRCGOP::updateAfterPicture( Int bitsCost ) |
---|
445 | { |
---|
446 | m_bitsLeft -= bitsCost; |
---|
447 | m_picLeft--; |
---|
448 | } |
---|
449 | |
---|
450 | Int 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 |
---|
466 | TEncRCPic::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 | #if KWU_RC_MADPRED_E0227 |
---|
485 | m_lastIVPicture = NULL; |
---|
486 | #endif |
---|
487 | |
---|
488 | m_picActualHeaderBits = 0; |
---|
489 | m_picActualBits = 0; |
---|
490 | m_picQP = 0; |
---|
491 | m_picLambda = 0.0; |
---|
492 | |
---|
493 | #if KWU_RC_MADPRED_E0227 |
---|
494 | m_IVtotalMAD = 0.0; |
---|
495 | #endif |
---|
496 | } |
---|
497 | |
---|
498 | TEncRCPic::~TEncRCPic() |
---|
499 | { |
---|
500 | destroy(); |
---|
501 | } |
---|
502 | |
---|
503 | Int TEncRCPic::xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ) |
---|
504 | { |
---|
505 | Int targetBits = 0; |
---|
506 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
---|
507 | |
---|
508 | Int i; |
---|
509 | Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft(); |
---|
510 | Int currPicRatio = encRCSeq->getBitRatio( currPicPosition ); |
---|
511 | Int totalPicRatio = 0; |
---|
512 | for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ ) |
---|
513 | { |
---|
514 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
515 | } |
---|
516 | |
---|
517 | targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio ); |
---|
518 | |
---|
519 | if ( targetBits < 100 ) |
---|
520 | { |
---|
521 | targetBits = 100; // at least allocate 100 bits for one picture |
---|
522 | } |
---|
523 | |
---|
524 | if ( m_encRCSeq->getFramesLeft() > 16 ) |
---|
525 | { |
---|
526 | targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) ); |
---|
527 | } |
---|
528 | |
---|
529 | return targetBits; |
---|
530 | } |
---|
531 | |
---|
532 | Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ) |
---|
533 | { |
---|
534 | Int numPreviousPics = 0; |
---|
535 | Int totalPreviousBits = 0; |
---|
536 | |
---|
537 | list<TEncRCPic*>::iterator it; |
---|
538 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
539 | { |
---|
540 | if ( (*it)->getFrameLevel() == frameLevel ) |
---|
541 | { |
---|
542 | totalPreviousBits += (*it)->getPicActualHeaderBits(); |
---|
543 | numPreviousPics++; |
---|
544 | } |
---|
545 | } |
---|
546 | |
---|
547 | Int estHeaderBits = 0; |
---|
548 | if ( numPreviousPics > 0 ) |
---|
549 | { |
---|
550 | estHeaderBits = totalPreviousBits / numPreviousPics; |
---|
551 | } |
---|
552 | |
---|
553 | return estHeaderBits; |
---|
554 | } |
---|
555 | |
---|
556 | #if V0078_ADAPTIVE_LOWER_BOUND |
---|
557 | Int TEncRCPic::xEstPicLowerBound(TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP) |
---|
558 | { |
---|
559 | Int lowerBound = 0; |
---|
560 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
---|
561 | |
---|
562 | const Int nextPicPosition = (encRCGOP->getNumPic() - encRCGOP->getPicLeft() + 1) % encRCGOP->getNumPic(); |
---|
563 | const Int nextPicRatio = encRCSeq->getBitRatio(nextPicPosition); |
---|
564 | |
---|
565 | Int totalPicRatio = 0; |
---|
566 | for (Int i = nextPicPosition; i < encRCGOP->getNumPic(); i++) |
---|
567 | { |
---|
568 | totalPicRatio += encRCSeq->getBitRatio(i); |
---|
569 | } |
---|
570 | |
---|
571 | if (nextPicPosition == 0) |
---|
572 | { |
---|
573 | GOPbitsLeft = encRCGOP->getTargetBits(); |
---|
574 | } |
---|
575 | else |
---|
576 | { |
---|
577 | GOPbitsLeft -= m_targetBits; |
---|
578 | } |
---|
579 | |
---|
580 | lowerBound = Int(((Double)GOPbitsLeft) * nextPicRatio / totalPicRatio); |
---|
581 | |
---|
582 | if (lowerBound < 100) |
---|
583 | { |
---|
584 | lowerBound = 100; // at least allocate 100 bits for one picture |
---|
585 | } |
---|
586 | |
---|
587 | if (m_encRCSeq->getFramesLeft() > 16) |
---|
588 | { |
---|
589 | lowerBound = Int(g_RCWeightPicRargetBitInBuffer * lowerBound + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP(nextPicPosition)); |
---|
590 | } |
---|
591 | |
---|
592 | return lowerBound; |
---|
593 | } |
---|
594 | #endif |
---|
595 | |
---|
596 | |
---|
597 | Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ) |
---|
598 | { |
---|
599 | if ( listPreviousPictures.size() > g_RCMaxPicListSize ) |
---|
600 | { |
---|
601 | TEncRCPic* p = listPreviousPictures.front(); |
---|
602 | listPreviousPictures.pop_front(); |
---|
603 | p->destroy(); |
---|
604 | delete p; |
---|
605 | } |
---|
606 | |
---|
607 | listPreviousPictures.push_back( this ); |
---|
608 | } |
---|
609 | |
---|
610 | #if KWU_RC_MADPRED_E0227 |
---|
611 | Void TEncRCPic::addToPictureLsitIV( list<TEncRCPic*>& listPreviousPictures ) |
---|
612 | { |
---|
613 | m_lastIVPicture = NULL; |
---|
614 | m_lastIVPicture = this; |
---|
615 | } |
---|
616 | |
---|
617 | Void TEncRCPic::setIVPic( TEncRCPic* BaseRCPic ) |
---|
618 | { |
---|
619 | m_lastIVPicture = BaseRCPic; |
---|
620 | } |
---|
621 | #endif |
---|
622 | |
---|
623 | #if KWU_RC_MADPRED_E0227 |
---|
624 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures, Int layerID ) |
---|
625 | #else |
---|
626 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ) |
---|
627 | #endif |
---|
628 | { |
---|
629 | destroy(); |
---|
630 | m_encRCSeq = encRCSeq; |
---|
631 | m_encRCGOP = encRCGOP; |
---|
632 | |
---|
633 | Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP ); |
---|
634 | Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel ); |
---|
635 | |
---|
636 | if ( targetBits < estHeaderBits + 100 ) |
---|
637 | { |
---|
638 | targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data |
---|
639 | } |
---|
640 | |
---|
641 | m_frameLevel = frameLevel; |
---|
642 | m_numberOfPixel = encRCSeq->getNumPixel(); |
---|
643 | m_numberOfLCU = encRCSeq->getNumberOfLCU(); |
---|
644 | m_estPicLambda = 100.0; |
---|
645 | m_targetBits = targetBits; |
---|
646 | m_estHeaderBits = estHeaderBits; |
---|
647 | m_bitsLeft = m_targetBits; |
---|
648 | Int picWidth = encRCSeq->getPicWidth(); |
---|
649 | Int picHeight = encRCSeq->getPicHeight(); |
---|
650 | Int LCUWidth = encRCSeq->getLCUWidth(); |
---|
651 | Int LCUHeight = encRCSeq->getLCUHeight(); |
---|
652 | Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1; |
---|
653 | Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1; |
---|
654 | #if V0078_ADAPTIVE_LOWER_BOUND |
---|
655 | m_lowerBound = xEstPicLowerBound( encRCSeq, encRCGOP ); |
---|
656 | #endif |
---|
657 | |
---|
658 | m_LCULeft = m_numberOfLCU; |
---|
659 | m_bitsLeft -= m_estHeaderBits; |
---|
660 | m_pixelsLeft = m_numberOfPixel; |
---|
661 | |
---|
662 | m_LCUs = new TRCLCU[m_numberOfLCU]; |
---|
663 | Int i, j; |
---|
664 | Int LCUIdx; |
---|
665 | for ( i=0; i<picWidthInLCU; i++ ) |
---|
666 | { |
---|
667 | for ( j=0; j<picHeightInLCU; j++ ) |
---|
668 | { |
---|
669 | LCUIdx = j*picWidthInLCU + i; |
---|
670 | m_LCUs[LCUIdx].m_actualBits = 0; |
---|
671 | m_LCUs[LCUIdx].m_QP = 0; |
---|
672 | m_LCUs[LCUIdx].m_lambda = 0.0; |
---|
673 | m_LCUs[LCUIdx].m_targetBits = 0; |
---|
674 | m_LCUs[LCUIdx].m_bitWeight = 1.0; |
---|
675 | Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); |
---|
676 | Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); |
---|
677 | m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight; |
---|
678 | |
---|
679 | #if KWU_RC_MADPRED_E0227 |
---|
680 | m_LCUs[LCUIdx].m_CUWidth = currWidth; |
---|
681 | m_LCUs[LCUIdx].m_CUHeight = currHeight; |
---|
682 | m_LCUs[LCUIdx].m_IVMAD = -1.0; |
---|
683 | #endif |
---|
684 | } |
---|
685 | } |
---|
686 | m_picActualHeaderBits = 0; |
---|
687 | m_picActualBits = 0; |
---|
688 | m_picQP = 0; |
---|
689 | m_picLambda = 0.0; |
---|
690 | |
---|
691 | |
---|
692 | #if KWU_RC_MADPRED_E0227 |
---|
693 | m_LayerID = layerID; |
---|
694 | m_lastIVPicture = NULL; |
---|
695 | m_IVtotalMAD = 0.0; |
---|
696 | #endif |
---|
697 | |
---|
698 | |
---|
699 | #if KWU_RC_MADPRED_E0227 |
---|
700 | list<TEncRCPic*>::reverse_iterator it; |
---|
701 | if( m_LayerID != 0) |
---|
702 | { |
---|
703 | m_lastIVPicture = NULL; |
---|
704 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
---|
705 | { |
---|
706 | if ( (*it)->getLayerID() == 0 ) |
---|
707 | { |
---|
708 | m_lastIVPicture = (*it); |
---|
709 | break; |
---|
710 | } |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | m_lastPicture = NULL; |
---|
715 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
---|
716 | { |
---|
717 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
718 | { |
---|
719 | m_lastPicture = (*it); |
---|
720 | break; |
---|
721 | } |
---|
722 | } |
---|
723 | #endif |
---|
724 | } |
---|
725 | |
---|
726 | Void TEncRCPic::destroy() |
---|
727 | { |
---|
728 | if( m_LCUs != NULL ) |
---|
729 | { |
---|
730 | delete[] m_LCUs; |
---|
731 | m_LCUs = NULL; |
---|
732 | } |
---|
733 | m_encRCSeq = NULL; |
---|
734 | m_encRCGOP = NULL; |
---|
735 | } |
---|
736 | |
---|
737 | |
---|
738 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) |
---|
739 | { |
---|
740 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
741 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
742 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
743 | Double estLambda; |
---|
744 | if (eSliceType == I_SLICE) |
---|
745 | { |
---|
746 | estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp); |
---|
747 | } |
---|
748 | else |
---|
749 | { |
---|
750 | estLambda = alpha * pow( bpp, beta ); |
---|
751 | } |
---|
752 | |
---|
753 | Double lastLevelLambda = -1.0; |
---|
754 | Double lastPicLambda = -1.0; |
---|
755 | Double lastValidLambda = -1.0; |
---|
756 | list<TEncRCPic*>::iterator it; |
---|
757 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
758 | { |
---|
759 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
760 | { |
---|
761 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
762 | } |
---|
763 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
764 | |
---|
765 | if ( lastPicLambda > 0.0 ) |
---|
766 | { |
---|
767 | lastValidLambda = lastPicLambda; |
---|
768 | } |
---|
769 | } |
---|
770 | |
---|
771 | if ( lastLevelLambda > 0.0 ) |
---|
772 | { |
---|
773 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
774 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
775 | } |
---|
776 | |
---|
777 | if ( lastPicLambda > 0.0 ) |
---|
778 | { |
---|
779 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
780 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
781 | } |
---|
782 | else if ( lastValidLambda > 0.0 ) |
---|
783 | { |
---|
784 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
785 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
786 | } |
---|
787 | else |
---|
788 | { |
---|
789 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
790 | } |
---|
791 | |
---|
792 | if ( estLambda < 0.1 ) |
---|
793 | { |
---|
794 | estLambda = 0.1; |
---|
795 | } |
---|
796 | |
---|
797 | m_estPicLambda = estLambda; |
---|
798 | |
---|
799 | Double totalWeight = 0.0; |
---|
800 | // initial BU bit allocation weight |
---|
801 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
802 | { |
---|
803 | Double alphaLCU, betaLCU; |
---|
804 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
805 | { |
---|
806 | alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
---|
807 | betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
---|
808 | } |
---|
809 | else |
---|
810 | { |
---|
811 | alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
812 | betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
813 | } |
---|
814 | |
---|
815 | m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); |
---|
816 | |
---|
817 | if ( m_LCUs[i].m_bitWeight < 0.01 ) |
---|
818 | { |
---|
819 | m_LCUs[i].m_bitWeight = 0.01; |
---|
820 | } |
---|
821 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
822 | } |
---|
823 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
824 | { |
---|
825 | Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight; |
---|
826 | m_LCUs[i].m_bitWeight = BUTargetBits; |
---|
827 | } |
---|
828 | |
---|
829 | return estLambda; |
---|
830 | } |
---|
831 | |
---|
832 | Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures ) |
---|
833 | { |
---|
834 | Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
835 | |
---|
836 | Int lastLevelQP = g_RCInvalidQPValue; |
---|
837 | Int lastPicQP = g_RCInvalidQPValue; |
---|
838 | Int lastValidQP = g_RCInvalidQPValue; |
---|
839 | list<TEncRCPic*>::iterator it; |
---|
840 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
841 | { |
---|
842 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
843 | { |
---|
844 | lastLevelQP = (*it)->getPicActualQP(); |
---|
845 | } |
---|
846 | lastPicQP = (*it)->getPicActualQP(); |
---|
847 | if ( lastPicQP > g_RCInvalidQPValue ) |
---|
848 | { |
---|
849 | lastValidQP = lastPicQP; |
---|
850 | } |
---|
851 | } |
---|
852 | |
---|
853 | if ( lastLevelQP > g_RCInvalidQPValue ) |
---|
854 | { |
---|
855 | QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP ); |
---|
856 | } |
---|
857 | |
---|
858 | if( lastPicQP > g_RCInvalidQPValue ) |
---|
859 | { |
---|
860 | QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP ); |
---|
861 | } |
---|
862 | else if( lastValidQP > g_RCInvalidQPValue ) |
---|
863 | { |
---|
864 | QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP ); |
---|
865 | } |
---|
866 | |
---|
867 | return QP; |
---|
868 | } |
---|
869 | |
---|
870 | |
---|
871 | #if KWU_RC_MADPRED_E0227 |
---|
872 | Double TEncRCPic::estimatePicLambdaIV( list<TEncRCPic*>& listPreviousPictures, Int CurPOC ) |
---|
873 | { |
---|
874 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
875 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
876 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
877 | Double estLambda = alpha * pow( bpp, beta ); |
---|
878 | Double lastLevelLambda = -1.0; |
---|
879 | Double lastPicLambda = -1.0; |
---|
880 | Double lastValidLambda = -1.0; |
---|
881 | list<TEncRCPic*>::iterator it; |
---|
882 | |
---|
883 | if(listPreviousPictures.size() == 0 || CurPOC%8 == 0) |
---|
884 | { |
---|
885 | lastLevelLambda = m_lastIVPicture->getPicActualLambda(); |
---|
886 | lastPicLambda = m_lastIVPicture->getPicActualLambda(); |
---|
887 | } |
---|
888 | else |
---|
889 | { |
---|
890 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
891 | { |
---|
892 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
893 | { |
---|
894 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
895 | } |
---|
896 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
897 | |
---|
898 | if ( lastPicLambda > 0.0 ) |
---|
899 | { |
---|
900 | lastValidLambda = lastPicLambda; |
---|
901 | } |
---|
902 | } |
---|
903 | } |
---|
904 | |
---|
905 | if ( lastLevelLambda > 0.0 ) |
---|
906 | { |
---|
907 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
908 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
909 | } |
---|
910 | |
---|
911 | if ( lastPicLambda > 0.0 ) |
---|
912 | { |
---|
913 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
914 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
915 | } |
---|
916 | else if ( lastValidLambda > 0.0 ) |
---|
917 | { |
---|
918 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
919 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
920 | } |
---|
921 | else |
---|
922 | { |
---|
923 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
924 | } |
---|
925 | |
---|
926 | if ( estLambda < 0.1 ) |
---|
927 | { |
---|
928 | estLambda = 0.1; |
---|
929 | } |
---|
930 | |
---|
931 | m_estPicLambda = estLambda; |
---|
932 | return estLambda; |
---|
933 | } |
---|
934 | #endif |
---|
935 | |
---|
936 | |
---|
937 | Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) |
---|
938 | { |
---|
939 | Int LCUIdx = getLCUCoded(); |
---|
940 | Double bpp = -1.0; |
---|
941 | Int avgBits = 0; |
---|
942 | |
---|
943 | if (eSliceType == I_SLICE) |
---|
944 | { |
---|
945 | Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1; |
---|
946 | Int bitrateWindow = min(4,noOfLCUsLeft); |
---|
947 | Double MAD = getLCU(LCUIdx).m_costIntra; |
---|
948 | |
---|
949 | if (m_remainingCostIntra > 0.1 ) |
---|
950 | { |
---|
951 | Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow; |
---|
952 | avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra ); |
---|
953 | } |
---|
954 | else |
---|
955 | { |
---|
956 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
957 | } |
---|
958 | m_remainingCostIntra -= MAD; |
---|
959 | } |
---|
960 | else |
---|
961 | { |
---|
962 | Double totalWeight = 0; |
---|
963 | for ( Int i=LCUIdx; i<m_numberOfLCU; i++ ) |
---|
964 | { |
---|
965 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
966 | } |
---|
967 | Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() ); |
---|
968 | avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 ); |
---|
969 | } |
---|
970 | |
---|
971 | if ( avgBits < 1 ) |
---|
972 | { |
---|
973 | avgBits = 1; |
---|
974 | } |
---|
975 | |
---|
976 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
977 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
978 | |
---|
979 | return bpp; |
---|
980 | } |
---|
981 | |
---|
982 | |
---|
983 | #if KWU_RC_MADPRED_E0227 |
---|
984 | Double TEncRCPic::getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction, Int* disparity ) |
---|
985 | { |
---|
986 | Int LCUIdx = getLCUCoded(); |
---|
987 | Double bpp = -1.0; |
---|
988 | Int avgBits = 0; |
---|
989 | #if !M0036_RC_IMPROVEMENT |
---|
990 | Double totalMAD = -1.0; |
---|
991 | Double MAD = -1.0; |
---|
992 | #endif |
---|
993 | |
---|
994 | Double totalMAD = -1.0; |
---|
995 | Double MAD = -1.0; |
---|
996 | |
---|
997 | Double IVMAD = -1.0; |
---|
998 | Double SAD = 0.0; |
---|
999 | Int x, y; |
---|
1000 | Int Sum = 0; |
---|
1001 | |
---|
1002 | { |
---|
1003 | Pel* pOrg = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1004 | Pel* pRec = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1005 | Pel* pDep = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1006 | Int iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride(); |
---|
1007 | |
---|
1008 | Int width = m_LCUs[ LCUIdx ].m_CUWidth; |
---|
1009 | Int height = m_LCUs[ LCUIdx ].m_CUHeight; |
---|
1010 | |
---|
1011 | for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8) |
---|
1012 | { |
---|
1013 | for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8) |
---|
1014 | { |
---|
1015 | Sum += pDep[x]; |
---|
1016 | } |
---|
1017 | pDep += iStride; |
---|
1018 | } |
---|
1019 | |
---|
1020 | Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8)); |
---|
1021 | |
---|
1022 | Double fL = focalLen * abs( basePos - curPos ); |
---|
1023 | Double z = abs( 1.0 / znear - 1.0 / zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / zfar); |
---|
1024 | *disparity = (Int)(direction*fL * z); |
---|
1025 | Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8); |
---|
1026 | |
---|
1027 | Int disp = *disparity; |
---|
1028 | Int posX, posY; |
---|
1029 | pcCU->getPosInPic(0, posX, posY); |
---|
1030 | if((posX + *disparity) < 0 || (posX + *disparity + width) >= pcCU->getSlice()->getSPS()->getMaxCUWidth()) |
---|
1031 | { |
---|
1032 | disp = 0; |
---|
1033 | } |
---|
1034 | |
---|
1035 | for( y = 0; y < height; y++ ) |
---|
1036 | { |
---|
1037 | for( x = 0; x < width; x++ ) |
---|
1038 | { |
---|
1039 | SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] |
---|
1040 | - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] )>>shift; |
---|
1041 | } |
---|
1042 | pOrg += iStride; |
---|
1043 | pRec += iStride; |
---|
1044 | } |
---|
1045 | IVMAD = SAD / (Double)(height * width); |
---|
1046 | IVMAD = IVMAD * IVMAD; |
---|
1047 | |
---|
1048 | m_LCUs[ LCUIdx ].m_IVMAD = IVMAD; |
---|
1049 | if(m_lastPicture) |
---|
1050 | { |
---|
1051 | m_LCUs[ LCUIdx ].m_MAD = m_lastPicture->getLCU(LCUIdx).m_MAD; |
---|
1052 | } |
---|
1053 | |
---|
1054 | MAD = m_LCUs[ LCUIdx ].m_IVMAD; |
---|
1055 | |
---|
1056 | if(m_lastPicture) |
---|
1057 | { |
---|
1058 | totalMAD = m_lastPicture->getTotalMAD(); // get total MAD of temporal frame |
---|
1059 | for ( Int i=0; i<LCUIdx; i++ ) |
---|
1060 | { |
---|
1061 | totalMAD -= m_lastPicture->getLCU(i).m_MAD; |
---|
1062 | } |
---|
1063 | } |
---|
1064 | else |
---|
1065 | { |
---|
1066 | totalMAD = m_lastIVPicture->getTotalMAD(); // get total MAD of inter-view frame |
---|
1067 | for ( Int i=0; i<LCUIdx; i++ ) |
---|
1068 | { |
---|
1069 | totalMAD -= m_lastIVPicture->getLCU(i).m_MAD; |
---|
1070 | } |
---|
1071 | } |
---|
1072 | |
---|
1073 | |
---|
1074 | if ( totalMAD > 0.1 ) |
---|
1075 | { |
---|
1076 | avgBits = Int( (m_bitsLeft * MAD) / totalMAD ); |
---|
1077 | } |
---|
1078 | else |
---|
1079 | { |
---|
1080 | avgBits = Int( (m_bitsLeft) / m_LCULeft ); |
---|
1081 | } |
---|
1082 | } |
---|
1083 | |
---|
1084 | if ( avgBits < 5 ) |
---|
1085 | { |
---|
1086 | avgBits = 5; |
---|
1087 | } |
---|
1088 | |
---|
1089 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
1090 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
1091 | |
---|
1092 | return bpp; |
---|
1093 | } |
---|
1094 | #endif |
---|
1095 | |
---|
1096 | |
---|
1097 | |
---|
1098 | |
---|
1099 | Double TEncRCPic::getLCUEstLambda( Double bpp ) |
---|
1100 | { |
---|
1101 | Int LCUIdx = getLCUCoded(); |
---|
1102 | Double alpha; |
---|
1103 | Double beta; |
---|
1104 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
1105 | { |
---|
1106 | alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
1107 | beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
1108 | } |
---|
1109 | else |
---|
1110 | { |
---|
1111 | alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
1112 | beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
1113 | } |
---|
1114 | |
---|
1115 | Double estLambda = alpha * pow( bpp, beta ); |
---|
1116 | //for Lambda clip, picture level clip |
---|
1117 | Double clipPicLambda = m_estPicLambda; |
---|
1118 | |
---|
1119 | //for Lambda clip, LCU level clip |
---|
1120 | Double clipNeighbourLambda = -1.0; |
---|
1121 | for ( Int i=LCUIdx - 1; i>=0; i-- ) |
---|
1122 | { |
---|
1123 | if ( m_LCUs[i].m_lambda > 0 ) |
---|
1124 | { |
---|
1125 | clipNeighbourLambda = m_LCUs[i].m_lambda; |
---|
1126 | break; |
---|
1127 | } |
---|
1128 | } |
---|
1129 | |
---|
1130 | if ( clipNeighbourLambda > 0.0 ) |
---|
1131 | { |
---|
1132 | estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda ); |
---|
1133 | } |
---|
1134 | |
---|
1135 | if ( clipPicLambda > 0.0 ) |
---|
1136 | { |
---|
1137 | estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda ); |
---|
1138 | } |
---|
1139 | else |
---|
1140 | { |
---|
1141 | estLambda = Clip3( 10.0, 1000.0, estLambda ); |
---|
1142 | } |
---|
1143 | |
---|
1144 | if ( estLambda < 0.1 ) |
---|
1145 | { |
---|
1146 | estLambda = 0.1; |
---|
1147 | } |
---|
1148 | |
---|
1149 | return estLambda; |
---|
1150 | } |
---|
1151 | |
---|
1152 | Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP ) |
---|
1153 | { |
---|
1154 | Int LCUIdx = getLCUCoded(); |
---|
1155 | Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
1156 | |
---|
1157 | //for Lambda clip, LCU level clip |
---|
1158 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
1159 | for ( Int i=LCUIdx - 1; i>=0; i-- ) |
---|
1160 | { |
---|
1161 | if ( (getLCU(i)).m_QP > g_RCInvalidQPValue ) |
---|
1162 | { |
---|
1163 | clipNeighbourQP = getLCU(i).m_QP; |
---|
1164 | break; |
---|
1165 | } |
---|
1166 | } |
---|
1167 | |
---|
1168 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
1169 | { |
---|
1170 | estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP ); |
---|
1171 | } |
---|
1172 | |
---|
1173 | estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP ); |
---|
1174 | |
---|
1175 | return estQP; |
---|
1176 | } |
---|
1177 | |
---|
1178 | Void TEncRCPic::updateAfterCTU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter ) |
---|
1179 | { |
---|
1180 | m_LCUs[LCUIdx].m_actualBits = bits; |
---|
1181 | m_LCUs[LCUIdx].m_QP = QP; |
---|
1182 | m_LCUs[LCUIdx].m_lambda = lambda; |
---|
1183 | |
---|
1184 | m_LCULeft--; |
---|
1185 | m_bitsLeft -= bits; |
---|
1186 | m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel; |
---|
1187 | |
---|
1188 | if ( !updateLCUParameter ) |
---|
1189 | { |
---|
1190 | return; |
---|
1191 | } |
---|
1192 | |
---|
1193 | if ( !m_encRCSeq->getUseLCUSeparateModel() ) |
---|
1194 | { |
---|
1195 | return; |
---|
1196 | } |
---|
1197 | |
---|
1198 | Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
1199 | Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
1200 | |
---|
1201 | Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits; |
---|
1202 | Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel; |
---|
1203 | Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels; |
---|
1204 | Double calLambda = alpha * pow( bpp, beta ); |
---|
1205 | Double inputLambda = m_LCUs[LCUIdx].m_lambda; |
---|
1206 | |
---|
1207 | if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 ) |
---|
1208 | { |
---|
1209 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
1210 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
1211 | |
---|
1212 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1213 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1214 | |
---|
1215 | TRCParameter rcPara; |
---|
1216 | rcPara.m_alpha = alpha; |
---|
1217 | rcPara.m_beta = beta; |
---|
1218 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
1219 | |
---|
1220 | return; |
---|
1221 | } |
---|
1222 | |
---|
1223 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
1224 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
1225 | Double lnbpp = log( bpp ); |
---|
1226 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
1227 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
1228 | |
---|
1229 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1230 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1231 | |
---|
1232 | TRCParameter rcPara; |
---|
1233 | rcPara.m_alpha = alpha; |
---|
1234 | rcPara.m_beta = beta; |
---|
1235 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
1236 | |
---|
1237 | } |
---|
1238 | |
---|
1239 | Double TEncRCPic::calAverageQP() |
---|
1240 | { |
---|
1241 | Int totalQPs = 0; |
---|
1242 | Int numTotalLCUs = 0; |
---|
1243 | |
---|
1244 | Int i; |
---|
1245 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
1246 | { |
---|
1247 | if ( m_LCUs[i].m_QP > 0 ) |
---|
1248 | { |
---|
1249 | totalQPs += m_LCUs[i].m_QP; |
---|
1250 | numTotalLCUs++; |
---|
1251 | } |
---|
1252 | } |
---|
1253 | |
---|
1254 | Double avgQP = 0.0; |
---|
1255 | if ( numTotalLCUs == 0 ) |
---|
1256 | { |
---|
1257 | avgQP = g_RCInvalidQPValue; |
---|
1258 | } |
---|
1259 | else |
---|
1260 | { |
---|
1261 | avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs); |
---|
1262 | } |
---|
1263 | return avgQP; |
---|
1264 | } |
---|
1265 | |
---|
1266 | Double TEncRCPic::calAverageLambda() |
---|
1267 | { |
---|
1268 | Double totalLambdas = 0.0; |
---|
1269 | Int numTotalLCUs = 0; |
---|
1270 | |
---|
1271 | Int i; |
---|
1272 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
1273 | { |
---|
1274 | if ( m_LCUs[i].m_lambda > 0.01 ) |
---|
1275 | { |
---|
1276 | totalLambdas += log( m_LCUs[i].m_lambda ); |
---|
1277 | numTotalLCUs++; |
---|
1278 | } |
---|
1279 | } |
---|
1280 | |
---|
1281 | Double avgLambda; |
---|
1282 | if( numTotalLCUs == 0 ) |
---|
1283 | { |
---|
1284 | avgLambda = -1.0; |
---|
1285 | } |
---|
1286 | else |
---|
1287 | { |
---|
1288 | avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs ); |
---|
1289 | } |
---|
1290 | return avgLambda; |
---|
1291 | } |
---|
1292 | |
---|
1293 | |
---|
1294 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) |
---|
1295 | { |
---|
1296 | m_picActualHeaderBits = actualHeaderBits; |
---|
1297 | m_picActualBits = actualTotalBits; |
---|
1298 | if ( averageQP > 0.0 ) |
---|
1299 | { |
---|
1300 | m_picQP = Int( averageQP + 0.5 ); |
---|
1301 | } |
---|
1302 | else |
---|
1303 | { |
---|
1304 | m_picQP = g_RCInvalidQPValue; |
---|
1305 | } |
---|
1306 | m_picLambda = averageLambda; |
---|
1307 | #if KWU_RC_MADPRED_E0227 |
---|
1308 | m_totalMAD = 0; |
---|
1309 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
1310 | { |
---|
1311 | m_totalMAD += m_LCUs[i].m_MAD; |
---|
1312 | } |
---|
1313 | #endif |
---|
1314 | |
---|
1315 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
1316 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
1317 | |
---|
1318 | if (eSliceType == I_SLICE) |
---|
1319 | { |
---|
1320 | updateAlphaBetaIntra(&alpha, &beta); |
---|
1321 | } |
---|
1322 | else |
---|
1323 | { |
---|
1324 | // update parameters |
---|
1325 | Double picActualBits = ( Double )m_picActualBits; |
---|
1326 | Double picActualBpp = picActualBits/(Double)m_numberOfPixel; |
---|
1327 | Double calLambda = alpha * pow( picActualBpp, beta ); |
---|
1328 | Double inputLambda = m_picLambda; |
---|
1329 | |
---|
1330 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) |
---|
1331 | { |
---|
1332 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
1333 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
1334 | |
---|
1335 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1336 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1337 | |
---|
1338 | TRCParameter rcPara; |
---|
1339 | rcPara.m_alpha = alpha; |
---|
1340 | rcPara.m_beta = beta; |
---|
1341 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
1342 | |
---|
1343 | return; |
---|
1344 | } |
---|
1345 | |
---|
1346 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
1347 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
1348 | Double lnbpp = log( picActualBpp ); |
---|
1349 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
1350 | |
---|
1351 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
1352 | |
---|
1353 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1354 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1355 | } |
---|
1356 | |
---|
1357 | TRCParameter rcPara; |
---|
1358 | rcPara.m_alpha = alpha; |
---|
1359 | rcPara.m_beta = beta; |
---|
1360 | |
---|
1361 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
1362 | |
---|
1363 | if ( m_frameLevel == 1 ) |
---|
1364 | { |
---|
1365 | Double currLambda = Clip3( 0.1, 10000.0, m_picLambda ); |
---|
1366 | Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda; |
---|
1367 | m_encRCSeq->setLastLambda( updateLastLambda ); |
---|
1368 | } |
---|
1369 | } |
---|
1370 | |
---|
1371 | Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) |
---|
1372 | { |
---|
1373 | Double alpha=0.25, beta=0.5582; |
---|
1374 | Int iIntraBits; |
---|
1375 | |
---|
1376 | if (orgBits*40 < m_numberOfPixel) |
---|
1377 | { |
---|
1378 | alpha=0.25; |
---|
1379 | } |
---|
1380 | else |
---|
1381 | { |
---|
1382 | alpha=0.30; |
---|
1383 | } |
---|
1384 | |
---|
1385 | iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5); |
---|
1386 | |
---|
1387 | return iIntraBits; |
---|
1388 | } |
---|
1389 | |
---|
1390 | Double TEncRCPic::calculateLambdaIntra(Double alpha, Double beta, Double MADPerPixel, Double bitsPerPixel) |
---|
1391 | { |
---|
1392 | return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) ); |
---|
1393 | } |
---|
1394 | |
---|
1395 | Void TEncRCPic::updateAlphaBetaIntra(Double *alpha, Double *beta) |
---|
1396 | { |
---|
1397 | Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1)); |
---|
1398 | Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits)); |
---|
1399 | |
---|
1400 | diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda); |
---|
1401 | *alpha = (*alpha) * exp(diffLambda); |
---|
1402 | *beta = (*beta) + diffLambda / lnbpp; |
---|
1403 | } |
---|
1404 | |
---|
1405 | |
---|
1406 | Void TEncRCPic::getLCUInitTargetBits() |
---|
1407 | { |
---|
1408 | Int iAvgBits = 0; |
---|
1409 | |
---|
1410 | m_remainingCostIntra = m_totalCostIntra; |
---|
1411 | for (Int i=m_numberOfLCU-1; i>=0; i--) |
---|
1412 | { |
---|
1413 | iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra); |
---|
1414 | getLCU(i).m_targetBitsLeft = iAvgBits; |
---|
1415 | } |
---|
1416 | } |
---|
1417 | |
---|
1418 | |
---|
1419 | Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP) |
---|
1420 | { |
---|
1421 | Int LCUIdx = getLCUCoded(); |
---|
1422 | |
---|
1423 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
1424 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
1425 | |
---|
1426 | Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel; |
---|
1427 | costPerPixel = pow(costPerPixel, BETA1); |
---|
1428 | Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp); |
---|
1429 | |
---|
1430 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
1431 | for (Int i=LCUIdx-1; i>=0; i--) |
---|
1432 | { |
---|
1433 | if ((getLCU(i)).m_QP > g_RCInvalidQPValue) |
---|
1434 | { |
---|
1435 | clipNeighbourQP = getLCU(i).m_QP; |
---|
1436 | break; |
---|
1437 | } |
---|
1438 | } |
---|
1439 | |
---|
1440 | Int minQP = clipPicQP - 2; |
---|
1441 | Int maxQP = clipPicQP + 2; |
---|
1442 | |
---|
1443 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
1444 | { |
---|
1445 | maxQP = min(clipNeighbourQP + 1, maxQP); |
---|
1446 | minQP = max(clipNeighbourQP - 1, minQP); |
---|
1447 | } |
---|
1448 | |
---|
1449 | Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005); |
---|
1450 | Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005); |
---|
1451 | |
---|
1452 | estLambda = Clip3(minLambda, maxLambda, estLambda); |
---|
1453 | |
---|
1454 | *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 ); |
---|
1455 | *estQP = Clip3(minQP, maxQP, *estQP); |
---|
1456 | |
---|
1457 | return estLambda; |
---|
1458 | } |
---|
1459 | |
---|
1460 | TEncRateCtrl::TEncRateCtrl() |
---|
1461 | { |
---|
1462 | m_encRCSeq = NULL; |
---|
1463 | m_encRCGOP = NULL; |
---|
1464 | m_encRCPic = NULL; |
---|
1465 | } |
---|
1466 | |
---|
1467 | TEncRateCtrl::~TEncRateCtrl() |
---|
1468 | { |
---|
1469 | destroy(); |
---|
1470 | } |
---|
1471 | |
---|
1472 | Void TEncRateCtrl::destroy() |
---|
1473 | { |
---|
1474 | if ( m_encRCSeq != NULL ) |
---|
1475 | { |
---|
1476 | delete m_encRCSeq; |
---|
1477 | m_encRCSeq = NULL; |
---|
1478 | } |
---|
1479 | if ( m_encRCGOP != NULL ) |
---|
1480 | { |
---|
1481 | delete m_encRCGOP; |
---|
1482 | m_encRCGOP = NULL; |
---|
1483 | } |
---|
1484 | while ( m_listRCPictures.size() > 0 ) |
---|
1485 | { |
---|
1486 | TEncRCPic* p = m_listRCPictures.front(); |
---|
1487 | m_listRCPictures.pop_front(); |
---|
1488 | delete p; |
---|
1489 | } |
---|
1490 | } |
---|
1491 | |
---|
1492 | #if KWU_RC_MADPRED_E0227 |
---|
1493 | 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], Int layerID ) |
---|
1494 | #else |
---|
1495 | Void TEncRateCtrl::init( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int keepHierBits, Bool useLCUSeparateModel, GOPEntry GOPList[MAX_GOP] ) |
---|
1496 | #endif |
---|
1497 | { |
---|
1498 | destroy(); |
---|
1499 | |
---|
1500 | Bool isLowdelay = true; |
---|
1501 | for ( Int i=0; i<GOPSize-1; i++ ) |
---|
1502 | { |
---|
1503 | if ( GOPList[i].m_POC > GOPList[i+1].m_POC ) |
---|
1504 | { |
---|
1505 | isLowdelay = false; |
---|
1506 | break; |
---|
1507 | } |
---|
1508 | } |
---|
1509 | |
---|
1510 | Int numberOfLevel = 1; |
---|
1511 | Int adaptiveBit = 0; |
---|
1512 | if ( keepHierBits > 0 ) |
---|
1513 | { |
---|
1514 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
1515 | } |
---|
1516 | if ( !isLowdelay && GOPSize == 8 ) |
---|
1517 | { |
---|
1518 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
1519 | } |
---|
1520 | numberOfLevel++; // intra picture |
---|
1521 | numberOfLevel++; // non-reference picture |
---|
1522 | |
---|
1523 | |
---|
1524 | Int* bitsRatio; |
---|
1525 | bitsRatio = new Int[ GOPSize ]; |
---|
1526 | for ( Int i=0; i<GOPSize; i++ ) |
---|
1527 | { |
---|
1528 | bitsRatio[i] = 10; |
---|
1529 | if ( !GOPList[i].m_refPic ) |
---|
1530 | { |
---|
1531 | bitsRatio[i] = 2; |
---|
1532 | } |
---|
1533 | } |
---|
1534 | |
---|
1535 | if ( keepHierBits > 0 ) |
---|
1536 | { |
---|
1537 | Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); |
---|
1538 | if ( GOPSize == 4 && isLowdelay ) |
---|
1539 | { |
---|
1540 | if ( bpp > 0.2 ) |
---|
1541 | { |
---|
1542 | bitsRatio[0] = 2; |
---|
1543 | bitsRatio[1] = 3; |
---|
1544 | bitsRatio[2] = 2; |
---|
1545 | bitsRatio[3] = 6; |
---|
1546 | } |
---|
1547 | else if( bpp > 0.1 ) |
---|
1548 | { |
---|
1549 | bitsRatio[0] = 2; |
---|
1550 | bitsRatio[1] = 3; |
---|
1551 | bitsRatio[2] = 2; |
---|
1552 | bitsRatio[3] = 10; |
---|
1553 | } |
---|
1554 | else if ( bpp > 0.05 ) |
---|
1555 | { |
---|
1556 | bitsRatio[0] = 2; |
---|
1557 | bitsRatio[1] = 3; |
---|
1558 | bitsRatio[2] = 2; |
---|
1559 | bitsRatio[3] = 12; |
---|
1560 | } |
---|
1561 | else |
---|
1562 | { |
---|
1563 | bitsRatio[0] = 2; |
---|
1564 | bitsRatio[1] = 3; |
---|
1565 | bitsRatio[2] = 2; |
---|
1566 | bitsRatio[3] = 14; |
---|
1567 | } |
---|
1568 | |
---|
1569 | if ( keepHierBits == 2 ) |
---|
1570 | { |
---|
1571 | adaptiveBit = 1; |
---|
1572 | } |
---|
1573 | } |
---|
1574 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
1575 | { |
---|
1576 | if ( bpp > 0.2 ) |
---|
1577 | { |
---|
1578 | bitsRatio[0] = 15; |
---|
1579 | bitsRatio[1] = 5; |
---|
1580 | bitsRatio[2] = 4; |
---|
1581 | bitsRatio[3] = 1; |
---|
1582 | bitsRatio[4] = 1; |
---|
1583 | bitsRatio[5] = 4; |
---|
1584 | bitsRatio[6] = 1; |
---|
1585 | bitsRatio[7] = 1; |
---|
1586 | } |
---|
1587 | else if ( bpp > 0.1 ) |
---|
1588 | { |
---|
1589 | bitsRatio[0] = 20; |
---|
1590 | bitsRatio[1] = 6; |
---|
1591 | bitsRatio[2] = 4; |
---|
1592 | bitsRatio[3] = 1; |
---|
1593 | bitsRatio[4] = 1; |
---|
1594 | bitsRatio[5] = 4; |
---|
1595 | bitsRatio[6] = 1; |
---|
1596 | bitsRatio[7] = 1; |
---|
1597 | } |
---|
1598 | else if ( bpp > 0.05 ) |
---|
1599 | { |
---|
1600 | bitsRatio[0] = 25; |
---|
1601 | bitsRatio[1] = 7; |
---|
1602 | bitsRatio[2] = 4; |
---|
1603 | bitsRatio[3] = 1; |
---|
1604 | bitsRatio[4] = 1; |
---|
1605 | bitsRatio[5] = 4; |
---|
1606 | bitsRatio[6] = 1; |
---|
1607 | bitsRatio[7] = 1; |
---|
1608 | } |
---|
1609 | else |
---|
1610 | { |
---|
1611 | bitsRatio[0] = 30; |
---|
1612 | bitsRatio[1] = 8; |
---|
1613 | bitsRatio[2] = 4; |
---|
1614 | bitsRatio[3] = 1; |
---|
1615 | bitsRatio[4] = 1; |
---|
1616 | bitsRatio[5] = 4; |
---|
1617 | bitsRatio[6] = 1; |
---|
1618 | bitsRatio[7] = 1; |
---|
1619 | } |
---|
1620 | |
---|
1621 | if ( keepHierBits == 2 ) |
---|
1622 | { |
---|
1623 | adaptiveBit = 2; |
---|
1624 | } |
---|
1625 | } |
---|
1626 | else |
---|
1627 | { |
---|
1628 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); |
---|
1629 | } |
---|
1630 | } |
---|
1631 | |
---|
1632 | Int* GOPID2Level = new Int[ GOPSize ]; |
---|
1633 | for ( Int i=0; i<GOPSize; i++ ) |
---|
1634 | { |
---|
1635 | GOPID2Level[i] = 1; |
---|
1636 | if ( !GOPList[i].m_refPic ) |
---|
1637 | { |
---|
1638 | GOPID2Level[i] = 2; |
---|
1639 | } |
---|
1640 | } |
---|
1641 | |
---|
1642 | if ( keepHierBits > 0 ) |
---|
1643 | { |
---|
1644 | if ( GOPSize == 4 && isLowdelay ) |
---|
1645 | { |
---|
1646 | GOPID2Level[0] = 3; |
---|
1647 | GOPID2Level[1] = 2; |
---|
1648 | GOPID2Level[2] = 3; |
---|
1649 | GOPID2Level[3] = 1; |
---|
1650 | } |
---|
1651 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
1652 | { |
---|
1653 | GOPID2Level[0] = 1; |
---|
1654 | GOPID2Level[1] = 2; |
---|
1655 | GOPID2Level[2] = 3; |
---|
1656 | GOPID2Level[3] = 4; |
---|
1657 | GOPID2Level[4] = 4; |
---|
1658 | GOPID2Level[5] = 3; |
---|
1659 | GOPID2Level[6] = 4; |
---|
1660 | GOPID2Level[7] = 4; |
---|
1661 | } |
---|
1662 | } |
---|
1663 | |
---|
1664 | if ( !isLowdelay && GOPSize == 8 ) |
---|
1665 | { |
---|
1666 | GOPID2Level[0] = 1; |
---|
1667 | GOPID2Level[1] = 2; |
---|
1668 | GOPID2Level[2] = 3; |
---|
1669 | GOPID2Level[3] = 4; |
---|
1670 | GOPID2Level[4] = 4; |
---|
1671 | GOPID2Level[5] = 3; |
---|
1672 | GOPID2Level[6] = 4; |
---|
1673 | GOPID2Level[7] = 4; |
---|
1674 | } |
---|
1675 | |
---|
1676 | m_encRCSeq = new TEncRCSeq; |
---|
1677 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); |
---|
1678 | m_encRCSeq->initBitsRatio( bitsRatio ); |
---|
1679 | m_encRCSeq->initGOPID2Level( GOPID2Level ); |
---|
1680 | m_encRCSeq->initPicPara(); |
---|
1681 | if ( useLCUSeparateModel ) |
---|
1682 | { |
---|
1683 | m_encRCSeq->initLCUPara(); |
---|
1684 | } |
---|
1685 | #if U0132_TARGET_BITS_SATURATION |
---|
1686 | m_CpbSaturationEnabled = false; |
---|
1687 | m_cpbSize = targetBitrate; |
---|
1688 | m_cpbState = (UInt)(m_cpbSize*0.5f); |
---|
1689 | m_bufferingRate = (Int)(targetBitrate / frameRate); |
---|
1690 | #endif |
---|
1691 | |
---|
1692 | #if KWU_RC_MADPRED_E0227 |
---|
1693 | setLayerID(layerID); |
---|
1694 | #endif |
---|
1695 | |
---|
1696 | delete[] bitsRatio; |
---|
1697 | delete[] GOPID2Level; |
---|
1698 | } |
---|
1699 | |
---|
1700 | Void TEncRateCtrl::initRCPic( Int frameLevel ) |
---|
1701 | { |
---|
1702 | m_encRCPic = new TEncRCPic; |
---|
1703 | #if KWU_RC_MADPRED_E0227 |
---|
1704 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures, m_LayerID ); |
---|
1705 | #else |
---|
1706 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures ); |
---|
1707 | #endif |
---|
1708 | } |
---|
1709 | |
---|
1710 | Void TEncRateCtrl::initRCGOP( Int numberOfPictures ) |
---|
1711 | { |
---|
1712 | m_encRCGOP = new TEncRCGOP; |
---|
1713 | m_encRCGOP->create( m_encRCSeq, numberOfPictures ); |
---|
1714 | } |
---|
1715 | |
---|
1716 | #if U0132_TARGET_BITS_SATURATION |
---|
1717 | Int TEncRateCtrl::updateCpbState(Int actualBits) |
---|
1718 | { |
---|
1719 | Int cpbState = 1; |
---|
1720 | |
---|
1721 | m_cpbState -= actualBits; |
---|
1722 | if (m_cpbState < 0) |
---|
1723 | { |
---|
1724 | cpbState = -1; |
---|
1725 | } |
---|
1726 | |
---|
1727 | m_cpbState += m_bufferingRate; |
---|
1728 | if (m_cpbState > m_cpbSize) |
---|
1729 | { |
---|
1730 | cpbState = 0; |
---|
1731 | } |
---|
1732 | |
---|
1733 | return cpbState; |
---|
1734 | } |
---|
1735 | |
---|
1736 | Void TEncRateCtrl::initHrdParam(const TComHRD* pcHrd, Int iFrameRate, Double fInitialCpbFullness) |
---|
1737 | { |
---|
1738 | m_CpbSaturationEnabled = true; |
---|
1739 | m_cpbSize = (pcHrd->getCpbSizeValueMinus1(0, 0, 0) + 1) << (4 + pcHrd->getCpbSizeScale()); |
---|
1740 | m_cpbState = (UInt)(m_cpbSize*fInitialCpbFullness); |
---|
1741 | m_bufferingRate = (UInt)(((pcHrd->getBitRateValueMinus1(0, 0, 0) + 1) << (6 + pcHrd->getBitRateScale())) / iFrameRate); |
---|
1742 | printf("\nHRD - [Initial CPB state %6d] [CPB Size %6d] [Buffering Rate %6d]\n", m_cpbState, m_cpbSize, m_bufferingRate); |
---|
1743 | } |
---|
1744 | #endif |
---|
1745 | |
---|
1746 | Void TEncRateCtrl::destroyRCGOP() |
---|
1747 | { |
---|
1748 | delete m_encRCGOP; |
---|
1749 | m_encRCGOP = NULL; |
---|
1750 | } |
---|