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 | #if M0036_RC_IMPROVEMENT |
---|
70 | m_adaptiveBit = 0; |
---|
71 | m_lastLambda = 0.0; |
---|
72 | #endif |
---|
73 | } |
---|
74 | |
---|
75 | TEncRCSeq::~TEncRCSeq() |
---|
76 | { |
---|
77 | destroy(); |
---|
78 | } |
---|
79 | |
---|
80 | #if M0036_RC_IMPROVEMENT |
---|
81 | 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 ) |
---|
82 | #else |
---|
83 | Void TEncRCSeq::create( Int totalFrames, Int targetBitrate, Int frameRate, Int GOPSize, Int picWidth, Int picHeight, Int LCUWidth, Int LCUHeight, Int numberOfLevel, Bool useLCUSeparateModel ) |
---|
84 | #endif |
---|
85 | { |
---|
86 | destroy(); |
---|
87 | m_totalFrames = totalFrames; |
---|
88 | m_targetRate = targetBitrate; |
---|
89 | m_frameRate = frameRate; |
---|
90 | m_GOPSize = GOPSize; |
---|
91 | m_picWidth = picWidth; |
---|
92 | m_picHeight = picHeight; |
---|
93 | m_LCUWidth = LCUWidth; |
---|
94 | m_LCUHeight = LCUHeight; |
---|
95 | m_numberOfLevel = numberOfLevel; |
---|
96 | m_useLCUSeparateModel = useLCUSeparateModel; |
---|
97 | |
---|
98 | m_numberOfPixel = m_picWidth * m_picHeight; |
---|
99 | m_targetBits = (Int64)m_totalFrames * (Int64)m_targetRate / (Int64)m_frameRate; |
---|
100 | m_seqTargetBpp = (Double)m_targetRate / (Double)m_frameRate / (Double)m_numberOfPixel; |
---|
101 | if ( m_seqTargetBpp < 0.03 ) |
---|
102 | { |
---|
103 | m_alphaUpdate = 0.01; |
---|
104 | m_betaUpdate = 0.005; |
---|
105 | } |
---|
106 | else if ( m_seqTargetBpp < 0.08 ) |
---|
107 | { |
---|
108 | m_alphaUpdate = 0.05; |
---|
109 | m_betaUpdate = 0.025; |
---|
110 | } |
---|
111 | #if M0036_RC_IMPROVEMENT |
---|
112 | else if ( m_seqTargetBpp < 0.2 ) |
---|
113 | { |
---|
114 | m_alphaUpdate = 0.1; |
---|
115 | m_betaUpdate = 0.05; |
---|
116 | } |
---|
117 | else if ( m_seqTargetBpp < 0.5 ) |
---|
118 | { |
---|
119 | m_alphaUpdate = 0.2; |
---|
120 | m_betaUpdate = 0.1; |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | m_alphaUpdate = 0.4; |
---|
125 | m_betaUpdate = 0.2; |
---|
126 | } |
---|
127 | #else |
---|
128 | else |
---|
129 | { |
---|
130 | m_alphaUpdate = 0.1; |
---|
131 | m_betaUpdate = 0.05; |
---|
132 | } |
---|
133 | #endif |
---|
134 | m_averageBits = (Int)(m_targetBits / totalFrames); |
---|
135 | Int picWidthInBU = ( m_picWidth % m_LCUWidth ) == 0 ? m_picWidth / m_LCUWidth : m_picWidth / m_LCUWidth + 1; |
---|
136 | Int picHeightInBU = ( m_picHeight % m_LCUHeight ) == 0 ? m_picHeight / m_LCUHeight : m_picHeight / m_LCUHeight + 1; |
---|
137 | m_numberOfLCU = picWidthInBU * picHeightInBU; |
---|
138 | |
---|
139 | m_bitsRatio = new Int[m_GOPSize]; |
---|
140 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
141 | { |
---|
142 | m_bitsRatio[i] = 1; |
---|
143 | } |
---|
144 | |
---|
145 | m_GOPID2Level = new Int[m_GOPSize]; |
---|
146 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
147 | { |
---|
148 | m_GOPID2Level[i] = 1; |
---|
149 | } |
---|
150 | |
---|
151 | m_picPara = new TRCParameter[m_numberOfLevel]; |
---|
152 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
153 | { |
---|
154 | m_picPara[i].m_alpha = 0.0; |
---|
155 | m_picPara[i].m_beta = 0.0; |
---|
156 | } |
---|
157 | |
---|
158 | if ( m_useLCUSeparateModel ) |
---|
159 | { |
---|
160 | m_LCUPara = new TRCParameter*[m_numberOfLevel]; |
---|
161 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
162 | { |
---|
163 | m_LCUPara[i] = new TRCParameter[m_numberOfLCU]; |
---|
164 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
165 | { |
---|
166 | m_LCUPara[i][j].m_alpha = 0.0; |
---|
167 | m_LCUPara[i][j].m_beta = 0.0; |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | m_framesLeft = m_totalFrames; |
---|
173 | m_bitsLeft = m_targetBits; |
---|
174 | #if M0036_RC_IMPROVEMENT |
---|
175 | m_adaptiveBit = adaptiveBit; |
---|
176 | m_lastLambda = 0.0; |
---|
177 | #endif |
---|
178 | } |
---|
179 | |
---|
180 | Void TEncRCSeq::destroy() |
---|
181 | { |
---|
182 | if (m_bitsRatio != NULL) |
---|
183 | { |
---|
184 | delete[] m_bitsRatio; |
---|
185 | m_bitsRatio = NULL; |
---|
186 | } |
---|
187 | |
---|
188 | if ( m_GOPID2Level != NULL ) |
---|
189 | { |
---|
190 | delete[] m_GOPID2Level; |
---|
191 | m_GOPID2Level = NULL; |
---|
192 | } |
---|
193 | |
---|
194 | if ( m_picPara != NULL ) |
---|
195 | { |
---|
196 | delete[] m_picPara; |
---|
197 | m_picPara = NULL; |
---|
198 | } |
---|
199 | |
---|
200 | if ( m_LCUPara != NULL ) |
---|
201 | { |
---|
202 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
203 | { |
---|
204 | delete[] m_LCUPara[i]; |
---|
205 | } |
---|
206 | delete[] m_LCUPara; |
---|
207 | m_LCUPara = NULL; |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | Void TEncRCSeq::initBitsRatio( Int bitsRatio[]) |
---|
212 | { |
---|
213 | for (Int i=0; i<m_GOPSize; i++) |
---|
214 | { |
---|
215 | m_bitsRatio[i] = bitsRatio[i]; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | Void TEncRCSeq::initGOPID2Level( Int GOPID2Level[] ) |
---|
220 | { |
---|
221 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
222 | { |
---|
223 | m_GOPID2Level[i] = GOPID2Level[i]; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | Void TEncRCSeq::initPicPara( TRCParameter* picPara ) |
---|
228 | { |
---|
229 | assert( m_picPara != NULL ); |
---|
230 | |
---|
231 | if ( picPara == NULL ) |
---|
232 | { |
---|
233 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
234 | { |
---|
235 | #if RATE_CONTROL_INTRA |
---|
236 | if (i>0) |
---|
237 | { |
---|
238 | m_picPara[i].m_alpha = 3.2003; |
---|
239 | m_picPara[i].m_beta = -1.367; |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | m_picPara[i].m_alpha = ALPHA; |
---|
244 | m_picPara[i].m_beta = BETA2; |
---|
245 | } |
---|
246 | #else |
---|
247 | m_picPara[i].m_alpha = 3.2003; |
---|
248 | m_picPara[i].m_beta = -1.367; |
---|
249 | #endif |
---|
250 | } |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
254 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
255 | { |
---|
256 | m_picPara[i] = picPara[i]; |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | Void TEncRCSeq::initLCUPara( TRCParameter** LCUPara ) |
---|
262 | { |
---|
263 | if ( m_LCUPara == NULL ) |
---|
264 | { |
---|
265 | return; |
---|
266 | } |
---|
267 | if ( LCUPara == NULL ) |
---|
268 | { |
---|
269 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
270 | { |
---|
271 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
272 | { |
---|
273 | #if RATE_CONTROL_INTRA |
---|
274 | m_LCUPara[i][j].m_alpha = m_picPara[i].m_alpha; |
---|
275 | m_LCUPara[i][j].m_beta = m_picPara[i].m_beta; |
---|
276 | #else |
---|
277 | m_LCUPara[i][j].m_alpha = 3.2003; |
---|
278 | m_LCUPara[i][j].m_beta = -1.367; |
---|
279 | #endif |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | for ( Int i=0; i<m_numberOfLevel; i++ ) |
---|
286 | { |
---|
287 | for ( Int j=0; j<m_numberOfLCU; j++) |
---|
288 | { |
---|
289 | m_LCUPara[i][j] = LCUPara[i][j]; |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | Void TEncRCSeq::updateAfterPic ( Int bits ) |
---|
296 | { |
---|
297 | m_bitsLeft -= bits; |
---|
298 | m_framesLeft--; |
---|
299 | } |
---|
300 | |
---|
301 | #if !RATE_CONTROL_INTRA |
---|
302 | Int TEncRCSeq::getRefineBitsForIntra( Int orgBits ) |
---|
303 | { |
---|
304 | Double bpp = ( (Double)orgBits ) / m_picHeight / m_picHeight; |
---|
305 | if ( bpp > 0.2 ) |
---|
306 | { |
---|
307 | return orgBits * 5; |
---|
308 | } |
---|
309 | if ( bpp > 0.1 ) |
---|
310 | { |
---|
311 | return orgBits * 7; |
---|
312 | } |
---|
313 | return orgBits * 10; |
---|
314 | } |
---|
315 | #endif |
---|
316 | |
---|
317 | #if M0036_RC_IMPROVEMENT |
---|
318 | Void TEncRCSeq::setAllBitRatio( Double basicLambda, Double* equaCoeffA, Double* equaCoeffB ) |
---|
319 | { |
---|
320 | Int* bitsRatio = new Int[m_GOPSize]; |
---|
321 | for ( Int i=0; i<m_GOPSize; i++ ) |
---|
322 | { |
---|
323 | bitsRatio[i] = (Int)( equaCoeffA[i] * pow( basicLambda, equaCoeffB[i] ) * m_numberOfPixel ); |
---|
324 | } |
---|
325 | initBitsRatio( bitsRatio ); |
---|
326 | delete[] bitsRatio; |
---|
327 | } |
---|
328 | #endif |
---|
329 | |
---|
330 | //GOP level |
---|
331 | TEncRCGOP::TEncRCGOP() |
---|
332 | { |
---|
333 | m_encRCSeq = NULL; |
---|
334 | m_picTargetBitInGOP = NULL; |
---|
335 | m_numPic = 0; |
---|
336 | m_targetBits = 0; |
---|
337 | m_picLeft = 0; |
---|
338 | m_bitsLeft = 0; |
---|
339 | } |
---|
340 | |
---|
341 | TEncRCGOP::~TEncRCGOP() |
---|
342 | { |
---|
343 | destroy(); |
---|
344 | } |
---|
345 | |
---|
346 | Void TEncRCGOP::create( TEncRCSeq* encRCSeq, Int numPic ) |
---|
347 | { |
---|
348 | destroy(); |
---|
349 | Int targetBits = xEstGOPTargetBits( encRCSeq, numPic ); |
---|
350 | |
---|
351 | #if M0036_RC_IMPROVEMENT |
---|
352 | if ( encRCSeq->getAdaptiveBits() > 0 && encRCSeq->getLastLambda() > 0.1 ) |
---|
353 | { |
---|
354 | Double targetBpp = (Double)targetBits / encRCSeq->getNumPixel(); |
---|
355 | Double basicLambda = 0.0; |
---|
356 | Double* lambdaRatio = new Double[encRCSeq->getGOPSize()]; |
---|
357 | Double* equaCoeffA = new Double[encRCSeq->getGOPSize()]; |
---|
358 | Double* equaCoeffB = new Double[encRCSeq->getGOPSize()]; |
---|
359 | |
---|
360 | if ( encRCSeq->getAdaptiveBits() == 1 ) // for GOP size =4, low delay case |
---|
361 | { |
---|
362 | if ( encRCSeq->getLastLambda() < 120.0 ) |
---|
363 | { |
---|
364 | lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.5793; |
---|
365 | lambdaRatio[0] = 1.3 * lambdaRatio[1]; |
---|
366 | lambdaRatio[2] = 1.3 * lambdaRatio[1]; |
---|
367 | lambdaRatio[3] = 1.0; |
---|
368 | } |
---|
369 | else |
---|
370 | { |
---|
371 | lambdaRatio[0] = 5.0; |
---|
372 | lambdaRatio[1] = 4.0; |
---|
373 | lambdaRatio[2] = 5.0; |
---|
374 | lambdaRatio[3] = 1.0; |
---|
375 | } |
---|
376 | } |
---|
377 | else if ( encRCSeq->getAdaptiveBits() == 2 ) // for GOP size = 8, random access case |
---|
378 | { |
---|
379 | if ( encRCSeq->getLastLambda() < 90.0 ) |
---|
380 | { |
---|
381 | lambdaRatio[0] = 1.0; |
---|
382 | lambdaRatio[1] = 0.725 * log( encRCSeq->getLastLambda() ) + 0.7963; |
---|
383 | lambdaRatio[2] = 1.3 * lambdaRatio[1]; |
---|
384 | lambdaRatio[3] = 3.25 * lambdaRatio[1]; |
---|
385 | lambdaRatio[4] = 3.25 * lambdaRatio[1]; |
---|
386 | lambdaRatio[5] = 1.3 * lambdaRatio[1]; |
---|
387 | lambdaRatio[6] = 3.25 * lambdaRatio[1]; |
---|
388 | lambdaRatio[7] = 3.25 * lambdaRatio[1]; |
---|
389 | } |
---|
390 | else |
---|
391 | { |
---|
392 | lambdaRatio[0] = 1.0; |
---|
393 | lambdaRatio[1] = 4.0; |
---|
394 | lambdaRatio[2] = 5.0; |
---|
395 | lambdaRatio[3] = 12.3; |
---|
396 | lambdaRatio[4] = 12.3; |
---|
397 | lambdaRatio[5] = 5.0; |
---|
398 | lambdaRatio[6] = 12.3; |
---|
399 | lambdaRatio[7] = 12.3; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | xCalEquaCoeff( encRCSeq, lambdaRatio, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
404 | basicLambda = xSolveEqua( targetBpp, equaCoeffA, equaCoeffB, encRCSeq->getGOPSize() ); |
---|
405 | encRCSeq->setAllBitRatio( basicLambda, equaCoeffA, equaCoeffB ); |
---|
406 | |
---|
407 | delete []lambdaRatio; |
---|
408 | delete []equaCoeffA; |
---|
409 | delete []equaCoeffB; |
---|
410 | } |
---|
411 | #endif |
---|
412 | |
---|
413 | m_picTargetBitInGOP = new Int[numPic]; |
---|
414 | Int i; |
---|
415 | Int totalPicRatio = 0; |
---|
416 | Int currPicRatio = 0; |
---|
417 | for ( i=0; i<numPic; i++ ) |
---|
418 | { |
---|
419 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
420 | } |
---|
421 | for ( i=0; i<numPic; i++ ) |
---|
422 | { |
---|
423 | currPicRatio = encRCSeq->getBitRatio( i ); |
---|
424 | #if M0036_RC_IMPROVEMENT |
---|
425 | m_picTargetBitInGOP[i] = (Int)( ((Double)targetBits) * currPicRatio / totalPicRatio ); |
---|
426 | #else |
---|
427 | m_picTargetBitInGOP[i] = targetBits * currPicRatio / totalPicRatio; |
---|
428 | #endif |
---|
429 | } |
---|
430 | |
---|
431 | m_encRCSeq = encRCSeq; |
---|
432 | m_numPic = numPic; |
---|
433 | m_targetBits = targetBits; |
---|
434 | m_picLeft = m_numPic; |
---|
435 | m_bitsLeft = m_targetBits; |
---|
436 | } |
---|
437 | |
---|
438 | #if M0036_RC_IMPROVEMENT |
---|
439 | Void TEncRCGOP::xCalEquaCoeff( TEncRCSeq* encRCSeq, Double* lambdaRatio, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
440 | { |
---|
441 | for ( Int i=0; i<GOPSize; i++ ) |
---|
442 | { |
---|
443 | Int frameLevel = encRCSeq->getGOPID2Level(i); |
---|
444 | Double alpha = encRCSeq->getPicPara(frameLevel).m_alpha; |
---|
445 | Double beta = encRCSeq->getPicPara(frameLevel).m_beta; |
---|
446 | equaCoeffA[i] = pow( 1.0/alpha, 1.0/beta ) * pow( lambdaRatio[i], 1.0/beta ); |
---|
447 | equaCoeffB[i] = 1.0/beta; |
---|
448 | } |
---|
449 | } |
---|
450 | |
---|
451 | Double TEncRCGOP::xSolveEqua( Double targetBpp, Double* equaCoeffA, Double* equaCoeffB, Int GOPSize ) |
---|
452 | { |
---|
453 | Double solution = 100.0; |
---|
454 | Double minNumber = 0.1; |
---|
455 | Double maxNumber = 10000.0; |
---|
456 | for ( Int i=0; i<g_RCIterationNum; i++ ) |
---|
457 | { |
---|
458 | Double fx = 0.0; |
---|
459 | for ( Int j=0; j<GOPSize; j++ ) |
---|
460 | { |
---|
461 | fx += equaCoeffA[j] * pow( solution, equaCoeffB[j] ); |
---|
462 | } |
---|
463 | |
---|
464 | if ( fabs( fx - targetBpp ) < 0.000001 ) |
---|
465 | { |
---|
466 | break; |
---|
467 | } |
---|
468 | |
---|
469 | if ( fx > targetBpp ) |
---|
470 | { |
---|
471 | minNumber = solution; |
---|
472 | solution = ( solution + maxNumber ) / 2.0; |
---|
473 | } |
---|
474 | else |
---|
475 | { |
---|
476 | maxNumber = solution; |
---|
477 | solution = ( solution + minNumber ) / 2.0; |
---|
478 | } |
---|
479 | } |
---|
480 | |
---|
481 | solution = Clip3( 0.1, 10000.0, solution ); |
---|
482 | return solution; |
---|
483 | } |
---|
484 | #endif |
---|
485 | |
---|
486 | Void TEncRCGOP::destroy() |
---|
487 | { |
---|
488 | m_encRCSeq = NULL; |
---|
489 | if ( m_picTargetBitInGOP != NULL ) |
---|
490 | { |
---|
491 | delete[] m_picTargetBitInGOP; |
---|
492 | m_picTargetBitInGOP = NULL; |
---|
493 | } |
---|
494 | } |
---|
495 | |
---|
496 | Void TEncRCGOP::updateAfterPicture( Int bitsCost ) |
---|
497 | { |
---|
498 | m_bitsLeft -= bitsCost; |
---|
499 | m_picLeft--; |
---|
500 | } |
---|
501 | |
---|
502 | Int TEncRCGOP::xEstGOPTargetBits( TEncRCSeq* encRCSeq, Int GOPSize ) |
---|
503 | { |
---|
504 | Int realInfluencePicture = min( g_RCSmoothWindowSize, encRCSeq->getFramesLeft() ); |
---|
505 | Int averageTargetBitsPerPic = (Int)( encRCSeq->getTargetBits() / encRCSeq->getTotalFrames() ); |
---|
506 | Int currentTargetBitsPerPic = (Int)( ( encRCSeq->getBitsLeft() - averageTargetBitsPerPic * (encRCSeq->getFramesLeft() - realInfluencePicture) ) / realInfluencePicture ); |
---|
507 | Int targetBits = currentTargetBitsPerPic * GOPSize; |
---|
508 | |
---|
509 | if ( targetBits < 200 ) |
---|
510 | { |
---|
511 | targetBits = 200; // at least allocate 200 bits for one GOP |
---|
512 | } |
---|
513 | |
---|
514 | return targetBits; |
---|
515 | } |
---|
516 | |
---|
517 | //picture level |
---|
518 | TEncRCPic::TEncRCPic() |
---|
519 | { |
---|
520 | m_encRCSeq = NULL; |
---|
521 | m_encRCGOP = NULL; |
---|
522 | |
---|
523 | m_frameLevel = 0; |
---|
524 | m_numberOfPixel = 0; |
---|
525 | m_numberOfLCU = 0; |
---|
526 | m_targetBits = 0; |
---|
527 | m_estHeaderBits = 0; |
---|
528 | m_estPicQP = 0; |
---|
529 | m_estPicLambda = 0.0; |
---|
530 | |
---|
531 | m_LCULeft = 0; |
---|
532 | m_bitsLeft = 0; |
---|
533 | m_pixelsLeft = 0; |
---|
534 | |
---|
535 | m_LCUs = NULL; |
---|
536 | #if !M0036_RC_IMPROVEMENT |
---|
537 | m_lastPicture = NULL; |
---|
538 | #endif |
---|
539 | |
---|
540 | #if KWU_RC_MADPRED_E0227 |
---|
541 | m_lastIVPicture = NULL; |
---|
542 | #endif |
---|
543 | |
---|
544 | m_picActualHeaderBits = 0; |
---|
545 | #if !M0036_RC_IMPROVEMENT |
---|
546 | m_totalMAD = 0.0; |
---|
547 | #endif |
---|
548 | m_picActualBits = 0; |
---|
549 | m_picQP = 0; |
---|
550 | m_picLambda = 0.0; |
---|
551 | |
---|
552 | #if KWU_RC_MADPRED_E0227 |
---|
553 | m_IVtotalMAD = 0.0; |
---|
554 | #endif |
---|
555 | } |
---|
556 | |
---|
557 | TEncRCPic::~TEncRCPic() |
---|
558 | { |
---|
559 | destroy(); |
---|
560 | } |
---|
561 | |
---|
562 | Int TEncRCPic::xEstPicTargetBits( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP ) |
---|
563 | { |
---|
564 | Int targetBits = 0; |
---|
565 | Int GOPbitsLeft = encRCGOP->getBitsLeft(); |
---|
566 | |
---|
567 | Int i; |
---|
568 | Int currPicPosition = encRCGOP->getNumPic()-encRCGOP->getPicLeft(); |
---|
569 | Int currPicRatio = encRCSeq->getBitRatio( currPicPosition ); |
---|
570 | Int totalPicRatio = 0; |
---|
571 | for ( i=currPicPosition; i<encRCGOP->getNumPic(); i++ ) |
---|
572 | { |
---|
573 | totalPicRatio += encRCSeq->getBitRatio( i ); |
---|
574 | } |
---|
575 | |
---|
576 | #if M0036_RC_IMPROVEMENT |
---|
577 | targetBits = Int( ((Double)GOPbitsLeft) * currPicRatio / totalPicRatio ); |
---|
578 | #else |
---|
579 | targetBits = Int( GOPbitsLeft * currPicRatio / totalPicRatio ); |
---|
580 | #endif |
---|
581 | |
---|
582 | if ( targetBits < 100 ) |
---|
583 | { |
---|
584 | targetBits = 100; // at least allocate 100 bits for one picture |
---|
585 | } |
---|
586 | |
---|
587 | if ( m_encRCSeq->getFramesLeft() > 16 ) |
---|
588 | { |
---|
589 | targetBits = Int( g_RCWeightPicRargetBitInBuffer * targetBits + g_RCWeightPicTargetBitInGOP * m_encRCGOP->getTargetBitInGOP( currPicPosition ) ); |
---|
590 | } |
---|
591 | |
---|
592 | return targetBits; |
---|
593 | } |
---|
594 | |
---|
595 | Int TEncRCPic::xEstPicHeaderBits( list<TEncRCPic*>& listPreviousPictures, Int frameLevel ) |
---|
596 | { |
---|
597 | Int numPreviousPics = 0; |
---|
598 | Int totalPreviousBits = 0; |
---|
599 | |
---|
600 | list<TEncRCPic*>::iterator it; |
---|
601 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
602 | { |
---|
603 | if ( (*it)->getFrameLevel() == frameLevel ) |
---|
604 | { |
---|
605 | totalPreviousBits += (*it)->getPicActualHeaderBits(); |
---|
606 | numPreviousPics++; |
---|
607 | } |
---|
608 | } |
---|
609 | |
---|
610 | Int estHeaderBits = 0; |
---|
611 | if ( numPreviousPics > 0 ) |
---|
612 | { |
---|
613 | estHeaderBits = totalPreviousBits / numPreviousPics; |
---|
614 | } |
---|
615 | |
---|
616 | return estHeaderBits; |
---|
617 | } |
---|
618 | |
---|
619 | Void TEncRCPic::addToPictureLsit( list<TEncRCPic*>& listPreviousPictures ) |
---|
620 | { |
---|
621 | if ( listPreviousPictures.size() > g_RCMaxPicListSize ) |
---|
622 | { |
---|
623 | TEncRCPic* p = listPreviousPictures.front(); |
---|
624 | listPreviousPictures.pop_front(); |
---|
625 | p->destroy(); |
---|
626 | delete p; |
---|
627 | } |
---|
628 | |
---|
629 | listPreviousPictures.push_back( this ); |
---|
630 | } |
---|
631 | |
---|
632 | #if KWU_RC_MADPRED_E0227 |
---|
633 | Void TEncRCPic::addToPictureLsitIV( list<TEncRCPic*>& listPreviousPictures ) |
---|
634 | { |
---|
635 | m_lastIVPicture = NULL; |
---|
636 | m_lastIVPicture = this; |
---|
637 | } |
---|
638 | |
---|
639 | Void TEncRCPic::setIVPic( TEncRCPic* BaseRCPic ) |
---|
640 | { |
---|
641 | m_lastIVPicture = BaseRCPic; |
---|
642 | } |
---|
643 | #endif |
---|
644 | |
---|
645 | #if KWU_RC_MADPRED_E0227 |
---|
646 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures, Int layerID ) |
---|
647 | #else |
---|
648 | Void TEncRCPic::create( TEncRCSeq* encRCSeq, TEncRCGOP* encRCGOP, Int frameLevel, list<TEncRCPic*>& listPreviousPictures ) |
---|
649 | #endif |
---|
650 | { |
---|
651 | destroy(); |
---|
652 | m_encRCSeq = encRCSeq; |
---|
653 | m_encRCGOP = encRCGOP; |
---|
654 | |
---|
655 | Int targetBits = xEstPicTargetBits( encRCSeq, encRCGOP ); |
---|
656 | Int estHeaderBits = xEstPicHeaderBits( listPreviousPictures, frameLevel ); |
---|
657 | |
---|
658 | if ( targetBits < estHeaderBits + 100 ) |
---|
659 | { |
---|
660 | targetBits = estHeaderBits + 100; // at least allocate 100 bits for picture data |
---|
661 | } |
---|
662 | |
---|
663 | m_frameLevel = frameLevel; |
---|
664 | m_numberOfPixel = encRCSeq->getNumPixel(); |
---|
665 | m_numberOfLCU = encRCSeq->getNumberOfLCU(); |
---|
666 | m_estPicLambda = 100.0; |
---|
667 | m_targetBits = targetBits; |
---|
668 | m_estHeaderBits = estHeaderBits; |
---|
669 | m_bitsLeft = m_targetBits; |
---|
670 | Int picWidth = encRCSeq->getPicWidth(); |
---|
671 | Int picHeight = encRCSeq->getPicHeight(); |
---|
672 | Int LCUWidth = encRCSeq->getLCUWidth(); |
---|
673 | Int LCUHeight = encRCSeq->getLCUHeight(); |
---|
674 | Int picWidthInLCU = ( picWidth % LCUWidth ) == 0 ? picWidth / LCUWidth : picWidth / LCUWidth + 1; |
---|
675 | Int picHeightInLCU = ( picHeight % LCUHeight ) == 0 ? picHeight / LCUHeight : picHeight / LCUHeight + 1; |
---|
676 | |
---|
677 | m_LCULeft = m_numberOfLCU; |
---|
678 | m_bitsLeft -= m_estHeaderBits; |
---|
679 | m_pixelsLeft = m_numberOfPixel; |
---|
680 | |
---|
681 | m_LCUs = new TRCLCU[m_numberOfLCU]; |
---|
682 | Int i, j; |
---|
683 | Int LCUIdx; |
---|
684 | for ( i=0; i<picWidthInLCU; i++ ) |
---|
685 | { |
---|
686 | for ( j=0; j<picHeightInLCU; j++ ) |
---|
687 | { |
---|
688 | LCUIdx = j*picWidthInLCU + i; |
---|
689 | m_LCUs[LCUIdx].m_actualBits = 0; |
---|
690 | m_LCUs[LCUIdx].m_QP = 0; |
---|
691 | m_LCUs[LCUIdx].m_lambda = 0.0; |
---|
692 | m_LCUs[LCUIdx].m_targetBits = 0; |
---|
693 | #if M0036_RC_IMPROVEMENT |
---|
694 | m_LCUs[LCUIdx].m_bitWeight = 1.0; |
---|
695 | #else |
---|
696 | m_LCUs[LCUIdx].m_MAD = 0.0; |
---|
697 | #endif |
---|
698 | Int currWidth = ( (i == picWidthInLCU -1) ? picWidth - LCUWidth *(picWidthInLCU -1) : LCUWidth ); |
---|
699 | Int currHeight = ( (j == picHeightInLCU-1) ? picHeight - LCUHeight*(picHeightInLCU-1) : LCUHeight ); |
---|
700 | m_LCUs[LCUIdx].m_numberOfPixel = currWidth * currHeight; |
---|
701 | |
---|
702 | #if KWU_RC_MADPRED_E0227 |
---|
703 | m_LCUs[LCUIdx].m_CUWidth = currWidth; |
---|
704 | m_LCUs[LCUIdx].m_CUHeight = currHeight; |
---|
705 | m_LCUs[LCUIdx].m_IVMAD = -1.0; |
---|
706 | #endif |
---|
707 | } |
---|
708 | } |
---|
709 | m_picActualHeaderBits = 0; |
---|
710 | #if !M0036_RC_IMPROVEMENT |
---|
711 | m_totalMAD = 0.0; |
---|
712 | #endif |
---|
713 | m_picActualBits = 0; |
---|
714 | m_picQP = 0; |
---|
715 | m_picLambda = 0.0; |
---|
716 | |
---|
717 | |
---|
718 | #if KWU_RC_MADPRED_E0227 |
---|
719 | m_LayerID = layerID; |
---|
720 | m_lastIVPicture = NULL; |
---|
721 | m_IVtotalMAD = 0.0; |
---|
722 | #endif |
---|
723 | |
---|
724 | |
---|
725 | #if !M0036_RC_IMPROVEMENT |
---|
726 | m_lastPicture = NULL; |
---|
727 | list<TEncRCPic*>::reverse_iterator it; |
---|
728 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
---|
729 | { |
---|
730 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
731 | { |
---|
732 | m_lastPicture = (*it); |
---|
733 | break; |
---|
734 | } |
---|
735 | } |
---|
736 | #endif |
---|
737 | |
---|
738 | #if KWU_RC_MADPRED_E0227 |
---|
739 | list<TEncRCPic*>::reverse_iterator it; |
---|
740 | if( m_LayerID != 0) |
---|
741 | { |
---|
742 | m_lastIVPicture = NULL; |
---|
743 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
---|
744 | { |
---|
745 | if ( (*it)->getLayerID() == 0 ) |
---|
746 | { |
---|
747 | m_lastIVPicture = (*it); |
---|
748 | break; |
---|
749 | } |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | m_lastPicture = NULL; |
---|
754 | for ( it = listPreviousPictures.rbegin(); it != listPreviousPictures.rend(); it++ ) |
---|
755 | { |
---|
756 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
757 | { |
---|
758 | m_lastPicture = (*it); |
---|
759 | break; |
---|
760 | } |
---|
761 | } |
---|
762 | #endif |
---|
763 | } |
---|
764 | |
---|
765 | Void TEncRCPic::destroy() |
---|
766 | { |
---|
767 | if( m_LCUs != NULL ) |
---|
768 | { |
---|
769 | delete[] m_LCUs; |
---|
770 | m_LCUs = NULL; |
---|
771 | } |
---|
772 | m_encRCSeq = NULL; |
---|
773 | m_encRCGOP = NULL; |
---|
774 | } |
---|
775 | |
---|
776 | |
---|
777 | #if RATE_CONTROL_INTRA |
---|
778 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures, SliceType eSliceType) |
---|
779 | #else |
---|
780 | Double TEncRCPic::estimatePicLambda( list<TEncRCPic*>& listPreviousPictures ) |
---|
781 | #endif |
---|
782 | { |
---|
783 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
784 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
785 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
786 | #if RATE_CONTROL_INTRA |
---|
787 | Double estLambda; |
---|
788 | if (eSliceType == I_SLICE) |
---|
789 | { |
---|
790 | estLambda = calculateLambdaIntra(alpha, beta, pow(m_totalCostIntra/(Double)m_numberOfPixel, BETA1), bpp); |
---|
791 | } |
---|
792 | else |
---|
793 | { |
---|
794 | estLambda = alpha * pow( bpp, beta ); |
---|
795 | } |
---|
796 | #else |
---|
797 | Double estLambda = alpha * pow( bpp, beta ); |
---|
798 | #endif |
---|
799 | |
---|
800 | Double lastLevelLambda = -1.0; |
---|
801 | Double lastPicLambda = -1.0; |
---|
802 | Double lastValidLambda = -1.0; |
---|
803 | list<TEncRCPic*>::iterator it; |
---|
804 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
805 | { |
---|
806 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
807 | { |
---|
808 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
809 | } |
---|
810 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
811 | |
---|
812 | if ( lastPicLambda > 0.0 ) |
---|
813 | { |
---|
814 | lastValidLambda = lastPicLambda; |
---|
815 | } |
---|
816 | } |
---|
817 | |
---|
818 | if ( lastLevelLambda > 0.0 ) |
---|
819 | { |
---|
820 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
821 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
822 | } |
---|
823 | |
---|
824 | if ( lastPicLambda > 0.0 ) |
---|
825 | { |
---|
826 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
827 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
828 | } |
---|
829 | else if ( lastValidLambda > 0.0 ) |
---|
830 | { |
---|
831 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
832 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
833 | } |
---|
834 | else |
---|
835 | { |
---|
836 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
837 | } |
---|
838 | |
---|
839 | if ( estLambda < 0.1 ) |
---|
840 | { |
---|
841 | estLambda = 0.1; |
---|
842 | } |
---|
843 | |
---|
844 | m_estPicLambda = estLambda; |
---|
845 | |
---|
846 | #if M0036_RC_IMPROVEMENT |
---|
847 | Double totalWeight = 0.0; |
---|
848 | // initial BU bit allocation weight |
---|
849 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
850 | { |
---|
851 | #if RC_FIX |
---|
852 | Double alphaLCU, betaLCU; |
---|
853 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
854 | { |
---|
855 | alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
---|
856 | betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
---|
857 | } |
---|
858 | else |
---|
859 | { |
---|
860 | alphaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
861 | betaLCU = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
862 | } |
---|
863 | #else |
---|
864 | Double alphaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_alpha; |
---|
865 | Double betaLCU = m_encRCSeq->getLCUPara( m_frameLevel, i ).m_beta; |
---|
866 | #endif |
---|
867 | |
---|
868 | m_LCUs[i].m_bitWeight = m_LCUs[i].m_numberOfPixel * pow( estLambda/alphaLCU, 1.0/betaLCU ); |
---|
869 | |
---|
870 | if ( m_LCUs[i].m_bitWeight < 0.01 ) |
---|
871 | { |
---|
872 | m_LCUs[i].m_bitWeight = 0.01; |
---|
873 | } |
---|
874 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
875 | } |
---|
876 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
877 | { |
---|
878 | Double BUTargetBits = m_targetBits * m_LCUs[i].m_bitWeight / totalWeight; |
---|
879 | m_LCUs[i].m_bitWeight = BUTargetBits; |
---|
880 | } |
---|
881 | #endif |
---|
882 | |
---|
883 | return estLambda; |
---|
884 | } |
---|
885 | |
---|
886 | Int TEncRCPic::estimatePicQP( Double lambda, list<TEncRCPic*>& listPreviousPictures ) |
---|
887 | { |
---|
888 | Int QP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
889 | |
---|
890 | Int lastLevelQP = g_RCInvalidQPValue; |
---|
891 | Int lastPicQP = g_RCInvalidQPValue; |
---|
892 | Int lastValidQP = g_RCInvalidQPValue; |
---|
893 | list<TEncRCPic*>::iterator it; |
---|
894 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
895 | { |
---|
896 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
897 | { |
---|
898 | lastLevelQP = (*it)->getPicActualQP(); |
---|
899 | } |
---|
900 | lastPicQP = (*it)->getPicActualQP(); |
---|
901 | if ( lastPicQP > g_RCInvalidQPValue ) |
---|
902 | { |
---|
903 | lastValidQP = lastPicQP; |
---|
904 | } |
---|
905 | } |
---|
906 | |
---|
907 | if ( lastLevelQP > g_RCInvalidQPValue ) |
---|
908 | { |
---|
909 | QP = Clip3( lastLevelQP - 3, lastLevelQP + 3, QP ); |
---|
910 | } |
---|
911 | |
---|
912 | if( lastPicQP > g_RCInvalidQPValue ) |
---|
913 | { |
---|
914 | QP = Clip3( lastPicQP - 10, lastPicQP + 10, QP ); |
---|
915 | } |
---|
916 | else if( lastValidQP > g_RCInvalidQPValue ) |
---|
917 | { |
---|
918 | QP = Clip3( lastValidQP - 10, lastValidQP + 10, QP ); |
---|
919 | } |
---|
920 | |
---|
921 | return QP; |
---|
922 | } |
---|
923 | |
---|
924 | |
---|
925 | #if KWU_RC_MADPRED_E0227 |
---|
926 | Double TEncRCPic::estimatePicLambdaIV( list<TEncRCPic*>& listPreviousPictures, Int CurPOC ) |
---|
927 | { |
---|
928 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
929 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
930 | Double bpp = (Double)m_targetBits/(Double)m_numberOfPixel; |
---|
931 | Double estLambda = alpha * pow( bpp, beta ); |
---|
932 | Double lastLevelLambda = -1.0; |
---|
933 | Double lastPicLambda = -1.0; |
---|
934 | Double lastValidLambda = -1.0; |
---|
935 | list<TEncRCPic*>::iterator it; |
---|
936 | |
---|
937 | if(listPreviousPictures.size() == 0 || CurPOC%8 == 0) |
---|
938 | { |
---|
939 | lastLevelLambda = m_lastIVPicture->getPicActualLambda(); |
---|
940 | lastPicLambda = m_lastIVPicture->getPicActualLambda(); |
---|
941 | } |
---|
942 | else |
---|
943 | { |
---|
944 | for ( it = listPreviousPictures.begin(); it != listPreviousPictures.end(); it++ ) |
---|
945 | { |
---|
946 | if ( (*it)->getFrameLevel() == m_frameLevel ) |
---|
947 | { |
---|
948 | lastLevelLambda = (*it)->getPicActualLambda(); |
---|
949 | } |
---|
950 | lastPicLambda = (*it)->getPicActualLambda(); |
---|
951 | |
---|
952 | if ( lastPicLambda > 0.0 ) |
---|
953 | { |
---|
954 | lastValidLambda = lastPicLambda; |
---|
955 | } |
---|
956 | } |
---|
957 | } |
---|
958 | |
---|
959 | if ( lastLevelLambda > 0.0 ) |
---|
960 | { |
---|
961 | lastLevelLambda = Clip3( 0.1, 10000.0, lastLevelLambda ); |
---|
962 | estLambda = Clip3( lastLevelLambda * pow( 2.0, -3.0/3.0 ), lastLevelLambda * pow( 2.0, 3.0/3.0 ), estLambda ); |
---|
963 | } |
---|
964 | |
---|
965 | if ( lastPicLambda > 0.0 ) |
---|
966 | { |
---|
967 | lastPicLambda = Clip3( 0.1, 2000.0, lastPicLambda ); |
---|
968 | estLambda = Clip3( lastPicLambda * pow( 2.0, -10.0/3.0 ), lastPicLambda * pow( 2.0, 10.0/3.0 ), estLambda ); |
---|
969 | } |
---|
970 | else if ( lastValidLambda > 0.0 ) |
---|
971 | { |
---|
972 | lastValidLambda = Clip3( 0.1, 2000.0, lastValidLambda ); |
---|
973 | estLambda = Clip3( lastValidLambda * pow(2.0, -10.0/3.0), lastValidLambda * pow(2.0, 10.0/3.0), estLambda ); |
---|
974 | } |
---|
975 | else |
---|
976 | { |
---|
977 | estLambda = Clip3( 0.1, 10000.0, estLambda ); |
---|
978 | } |
---|
979 | |
---|
980 | if ( estLambda < 0.1 ) |
---|
981 | { |
---|
982 | estLambda = 0.1; |
---|
983 | } |
---|
984 | |
---|
985 | m_estPicLambda = estLambda; |
---|
986 | return estLambda; |
---|
987 | } |
---|
988 | #endif |
---|
989 | |
---|
990 | |
---|
991 | #if RATE_CONTROL_INTRA |
---|
992 | Double TEncRCPic::getLCUTargetBpp(SliceType eSliceType) |
---|
993 | #else |
---|
994 | Double TEncRCPic::getLCUTargetBpp() |
---|
995 | #endif |
---|
996 | { |
---|
997 | Int LCUIdx = getLCUCoded(); |
---|
998 | Double bpp = -1.0; |
---|
999 | Int avgBits = 0; |
---|
1000 | #if !M0036_RC_IMPROVEMENT |
---|
1001 | Double totalMAD = -1.0; |
---|
1002 | Double MAD = -1.0; |
---|
1003 | #endif |
---|
1004 | |
---|
1005 | #if RATE_CONTROL_INTRA |
---|
1006 | if (eSliceType == I_SLICE){ |
---|
1007 | Int noOfLCUsLeft = m_numberOfLCU - LCUIdx + 1; |
---|
1008 | Int bitrateWindow = min(4,noOfLCUsLeft); |
---|
1009 | Double MAD = getLCU(LCUIdx).m_costIntra; |
---|
1010 | |
---|
1011 | if (m_remainingCostIntra > 0.1 ) |
---|
1012 | { |
---|
1013 | Double weightedBitsLeft = (m_bitsLeft*bitrateWindow+(m_bitsLeft-getLCU(LCUIdx).m_targetBitsLeft)*noOfLCUsLeft)/(Double)bitrateWindow; |
---|
1014 | avgBits = Int( MAD*weightedBitsLeft/m_remainingCostIntra ); |
---|
1015 | } |
---|
1016 | else |
---|
1017 | { |
---|
1018 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
1019 | } |
---|
1020 | m_remainingCostIntra -= MAD; |
---|
1021 | } |
---|
1022 | else |
---|
1023 | { |
---|
1024 | #endif |
---|
1025 | #if M0036_RC_IMPROVEMENT |
---|
1026 | Double totalWeight = 0; |
---|
1027 | for ( Int i=LCUIdx; i<m_numberOfLCU; i++ ) |
---|
1028 | { |
---|
1029 | totalWeight += m_LCUs[i].m_bitWeight; |
---|
1030 | } |
---|
1031 | Int realInfluenceLCU = min( g_RCLCUSmoothWindowSize, getLCULeft() ); |
---|
1032 | avgBits = (Int)( m_LCUs[LCUIdx].m_bitWeight - ( totalWeight - m_bitsLeft ) / realInfluenceLCU + 0.5 ); |
---|
1033 | #else |
---|
1034 | if ( m_lastPicture == NULL ) |
---|
1035 | { |
---|
1036 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
1037 | } |
---|
1038 | else |
---|
1039 | { |
---|
1040 | MAD = m_lastPicture->getLCU(LCUIdx).m_MAD; |
---|
1041 | totalMAD = m_lastPicture->getTotalMAD(); |
---|
1042 | for ( Int i=0; i<LCUIdx; i++ ) |
---|
1043 | { |
---|
1044 | totalMAD -= m_lastPicture->getLCU(i).m_MAD; |
---|
1045 | } |
---|
1046 | |
---|
1047 | if ( totalMAD > 0.1 ) |
---|
1048 | { |
---|
1049 | avgBits = Int( m_bitsLeft * MAD / totalMAD ); |
---|
1050 | } |
---|
1051 | else |
---|
1052 | { |
---|
1053 | avgBits = Int( m_bitsLeft / m_LCULeft ); |
---|
1054 | } |
---|
1055 | } |
---|
1056 | #endif |
---|
1057 | #if RATE_CONTROL_INTRA |
---|
1058 | } |
---|
1059 | #endif |
---|
1060 | |
---|
1061 | if ( avgBits < 1 ) |
---|
1062 | { |
---|
1063 | avgBits = 1; |
---|
1064 | } |
---|
1065 | |
---|
1066 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
1067 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
1068 | |
---|
1069 | return bpp; |
---|
1070 | } |
---|
1071 | |
---|
1072 | |
---|
1073 | #if KWU_RC_MADPRED_E0227 |
---|
1074 | Double TEncRCPic::getLCUTargetBppforInterView( list<TEncRCPic*>& listPreviousPictures, TComDataCU* pcCU, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction, Int* disparity ) |
---|
1075 | { |
---|
1076 | Int LCUIdx = getLCUCoded(); |
---|
1077 | Double bpp = -1.0; |
---|
1078 | Int avgBits = 0; |
---|
1079 | #if !M0036_RC_IMPROVEMENT |
---|
1080 | Double totalMAD = -1.0; |
---|
1081 | Double MAD = -1.0; |
---|
1082 | #endif |
---|
1083 | |
---|
1084 | Double totalMAD = -1.0; |
---|
1085 | Double MAD = -1.0; |
---|
1086 | |
---|
1087 | Double IVMAD = -1.0; |
---|
1088 | Double SAD = 0.0; |
---|
1089 | Int x, y; |
---|
1090 | Int Sum = 0; |
---|
1091 | |
---|
1092 | { |
---|
1093 | Pel* pOrg = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1094 | Pel* pRec = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1095 | Pel* pDep = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1096 | Int iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride(); |
---|
1097 | |
---|
1098 | Int width = m_LCUs[ LCUIdx ].m_CUWidth; |
---|
1099 | Int height = m_LCUs[ LCUIdx ].m_CUHeight; |
---|
1100 | |
---|
1101 | for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8) |
---|
1102 | { |
---|
1103 | for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8) |
---|
1104 | { |
---|
1105 | Sum += pDep[x]; |
---|
1106 | } |
---|
1107 | pDep += iStride; |
---|
1108 | } |
---|
1109 | |
---|
1110 | Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8)); |
---|
1111 | |
---|
1112 | Double fL = focalLen * abs( basePos - curPos ); |
---|
1113 | Double z = abs( 1.0 / znear - 1.0 / zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / zfar); |
---|
1114 | *disparity = (Int)(direction*fL * z); |
---|
1115 | Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8); |
---|
1116 | |
---|
1117 | Int disp = *disparity; |
---|
1118 | Int posX, posY; |
---|
1119 | pcCU->getPosInPic(0, posX, posY); |
---|
1120 | if((posX + *disparity) < 0 || (posX + *disparity + width) >= pcCU->getSlice()->getSPS()->getMaxCUWidth()) |
---|
1121 | { |
---|
1122 | disp = 0; |
---|
1123 | } |
---|
1124 | |
---|
1125 | for( y = 0; y < height; y++ ) |
---|
1126 | { |
---|
1127 | for( x = 0; x < width; x++ ) |
---|
1128 | { |
---|
1129 | SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] |
---|
1130 | - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] )>>shift; |
---|
1131 | } |
---|
1132 | pOrg += iStride; |
---|
1133 | pRec += iStride; |
---|
1134 | } |
---|
1135 | IVMAD = SAD / (Double)(height * width); |
---|
1136 | IVMAD = IVMAD * IVMAD; |
---|
1137 | |
---|
1138 | m_LCUs[ LCUIdx ].m_IVMAD = IVMAD; |
---|
1139 | if(m_lastPicture) |
---|
1140 | { |
---|
1141 | m_LCUs[ LCUIdx ].m_MAD = m_lastPicture->getLCU(LCUIdx).m_MAD; |
---|
1142 | } |
---|
1143 | |
---|
1144 | MAD = m_LCUs[ LCUIdx ].m_IVMAD; |
---|
1145 | |
---|
1146 | if(m_lastPicture) |
---|
1147 | { |
---|
1148 | totalMAD = m_lastPicture->getTotalMAD(); // get total MAD of temporal frame |
---|
1149 | for ( Int i=0; i<LCUIdx; i++ ) |
---|
1150 | { |
---|
1151 | totalMAD -= m_lastPicture->getLCU(i).m_MAD; |
---|
1152 | } |
---|
1153 | } |
---|
1154 | else |
---|
1155 | { |
---|
1156 | totalMAD = m_lastIVPicture->getTotalMAD(); // get total MAD of inter-view frame |
---|
1157 | for ( Int i=0; i<LCUIdx; i++ ) |
---|
1158 | { |
---|
1159 | totalMAD -= m_lastIVPicture->getLCU(i).m_MAD; |
---|
1160 | } |
---|
1161 | } |
---|
1162 | |
---|
1163 | |
---|
1164 | if ( totalMAD > 0.1 ) |
---|
1165 | { |
---|
1166 | avgBits = Int( (m_bitsLeft * MAD) / totalMAD ); |
---|
1167 | } |
---|
1168 | else |
---|
1169 | { |
---|
1170 | avgBits = Int( (m_bitsLeft) / m_LCULeft ); |
---|
1171 | } |
---|
1172 | } |
---|
1173 | |
---|
1174 | if ( avgBits < 5 ) |
---|
1175 | { |
---|
1176 | avgBits = 5; |
---|
1177 | } |
---|
1178 | |
---|
1179 | bpp = ( Double )avgBits/( Double )m_LCUs[ LCUIdx ].m_numberOfPixel; |
---|
1180 | m_LCUs[ LCUIdx ].m_targetBits = avgBits; |
---|
1181 | |
---|
1182 | return bpp; |
---|
1183 | } |
---|
1184 | #endif |
---|
1185 | |
---|
1186 | |
---|
1187 | |
---|
1188 | |
---|
1189 | Double TEncRCPic::getLCUEstLambda( Double bpp ) |
---|
1190 | { |
---|
1191 | Int LCUIdx = getLCUCoded(); |
---|
1192 | Double alpha; |
---|
1193 | Double beta; |
---|
1194 | if ( m_encRCSeq->getUseLCUSeparateModel() ) |
---|
1195 | { |
---|
1196 | alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
1197 | beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
1198 | } |
---|
1199 | else |
---|
1200 | { |
---|
1201 | alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
1202 | beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
1203 | } |
---|
1204 | |
---|
1205 | Double estLambda = alpha * pow( bpp, beta ); |
---|
1206 | //for Lambda clip, picture level clip |
---|
1207 | Double clipPicLambda = m_estPicLambda; |
---|
1208 | |
---|
1209 | //for Lambda clip, LCU level clip |
---|
1210 | Double clipNeighbourLambda = -1.0; |
---|
1211 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
---|
1212 | { |
---|
1213 | if ( m_LCUs[i].m_lambda > 0 ) |
---|
1214 | { |
---|
1215 | clipNeighbourLambda = m_LCUs[i].m_lambda; |
---|
1216 | break; |
---|
1217 | } |
---|
1218 | } |
---|
1219 | |
---|
1220 | if ( clipNeighbourLambda > 0.0 ) |
---|
1221 | { |
---|
1222 | estLambda = Clip3( clipNeighbourLambda * pow( 2.0, -1.0/3.0 ), clipNeighbourLambda * pow( 2.0, 1.0/3.0 ), estLambda ); |
---|
1223 | } |
---|
1224 | |
---|
1225 | if ( clipPicLambda > 0.0 ) |
---|
1226 | { |
---|
1227 | estLambda = Clip3( clipPicLambda * pow( 2.0, -2.0/3.0 ), clipPicLambda * pow( 2.0, 2.0/3.0 ), estLambda ); |
---|
1228 | } |
---|
1229 | else |
---|
1230 | { |
---|
1231 | estLambda = Clip3( 10.0, 1000.0, estLambda ); |
---|
1232 | } |
---|
1233 | |
---|
1234 | if ( estLambda < 0.1 ) |
---|
1235 | { |
---|
1236 | estLambda = 0.1; |
---|
1237 | } |
---|
1238 | |
---|
1239 | return estLambda; |
---|
1240 | } |
---|
1241 | |
---|
1242 | Int TEncRCPic::getLCUEstQP( Double lambda, Int clipPicQP ) |
---|
1243 | { |
---|
1244 | Int LCUIdx = getLCUCoded(); |
---|
1245 | Int estQP = Int( 4.2005 * log( lambda ) + 13.7122 + 0.5 ); |
---|
1246 | |
---|
1247 | //for Lambda clip, LCU level clip |
---|
1248 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
1249 | for ( int i=LCUIdx - 1; i>=0; i-- ) |
---|
1250 | { |
---|
1251 | if ( (getLCU(i)).m_QP > g_RCInvalidQPValue ) |
---|
1252 | { |
---|
1253 | clipNeighbourQP = getLCU(i).m_QP; |
---|
1254 | break; |
---|
1255 | } |
---|
1256 | } |
---|
1257 | |
---|
1258 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
1259 | { |
---|
1260 | estQP = Clip3( clipNeighbourQP - 1, clipNeighbourQP + 1, estQP ); |
---|
1261 | } |
---|
1262 | |
---|
1263 | estQP = Clip3( clipPicQP - 2, clipPicQP + 2, estQP ); |
---|
1264 | |
---|
1265 | return estQP; |
---|
1266 | } |
---|
1267 | |
---|
1268 | Void TEncRCPic::updateAfterLCU( Int LCUIdx, Int bits, Int QP, Double lambda, Bool updateLCUParameter ) |
---|
1269 | { |
---|
1270 | m_LCUs[LCUIdx].m_actualBits = bits; |
---|
1271 | m_LCUs[LCUIdx].m_QP = QP; |
---|
1272 | m_LCUs[LCUIdx].m_lambda = lambda; |
---|
1273 | |
---|
1274 | m_LCULeft--; |
---|
1275 | m_bitsLeft -= bits; |
---|
1276 | m_pixelsLeft -= m_LCUs[LCUIdx].m_numberOfPixel; |
---|
1277 | |
---|
1278 | if ( !updateLCUParameter ) |
---|
1279 | { |
---|
1280 | return; |
---|
1281 | } |
---|
1282 | |
---|
1283 | if ( !m_encRCSeq->getUseLCUSeparateModel() ) |
---|
1284 | { |
---|
1285 | return; |
---|
1286 | } |
---|
1287 | |
---|
1288 | Double alpha = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_alpha; |
---|
1289 | Double beta = m_encRCSeq->getLCUPara( m_frameLevel, LCUIdx ).m_beta; |
---|
1290 | |
---|
1291 | Int LCUActualBits = m_LCUs[LCUIdx].m_actualBits; |
---|
1292 | Int LCUTotalPixels = m_LCUs[LCUIdx].m_numberOfPixel; |
---|
1293 | Double bpp = ( Double )LCUActualBits/( Double )LCUTotalPixels; |
---|
1294 | Double calLambda = alpha * pow( bpp, beta ); |
---|
1295 | Double inputLambda = m_LCUs[LCUIdx].m_lambda; |
---|
1296 | |
---|
1297 | if( inputLambda < 0.01 || calLambda < 0.01 || bpp < 0.0001 ) |
---|
1298 | { |
---|
1299 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
1300 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
1301 | |
---|
1302 | #if M0036_RC_IMPROVEMENT |
---|
1303 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1304 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1305 | #else |
---|
1306 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
1307 | beta = Clip3( -3.0, -0.1, beta ); |
---|
1308 | #endif |
---|
1309 | |
---|
1310 | TRCParameter rcPara; |
---|
1311 | rcPara.m_alpha = alpha; |
---|
1312 | rcPara.m_beta = beta; |
---|
1313 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
1314 | |
---|
1315 | return; |
---|
1316 | } |
---|
1317 | |
---|
1318 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
1319 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
1320 | double lnbpp = log( bpp ); |
---|
1321 | #if M0036_RC_IMPROVEMENT |
---|
1322 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
1323 | #else |
---|
1324 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
---|
1325 | #endif |
---|
1326 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
1327 | |
---|
1328 | #if M0036_RC_IMPROVEMENT |
---|
1329 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1330 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1331 | #else |
---|
1332 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
1333 | beta = Clip3( -3.0, -0.1, beta ); |
---|
1334 | #endif |
---|
1335 | TRCParameter rcPara; |
---|
1336 | rcPara.m_alpha = alpha; |
---|
1337 | rcPara.m_beta = beta; |
---|
1338 | m_encRCSeq->setLCUPara( m_frameLevel, LCUIdx, rcPara ); |
---|
1339 | |
---|
1340 | } |
---|
1341 | |
---|
1342 | #if !M0036_RC_IMPROVEMENT |
---|
1343 | Double TEncRCPic::getEffectivePercentage() |
---|
1344 | { |
---|
1345 | Int effectivePiexels = 0; |
---|
1346 | Int totalPixels = 0; |
---|
1347 | |
---|
1348 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
1349 | { |
---|
1350 | totalPixels += m_LCUs[i].m_numberOfPixel; |
---|
1351 | if ( m_LCUs[i].m_QP > 0 ) |
---|
1352 | { |
---|
1353 | effectivePiexels += m_LCUs[i].m_numberOfPixel; |
---|
1354 | } |
---|
1355 | } |
---|
1356 | |
---|
1357 | Double effectivePixelPercentage = (Double)effectivePiexels/(Double)totalPixels; |
---|
1358 | return effectivePixelPercentage; |
---|
1359 | } |
---|
1360 | #endif |
---|
1361 | |
---|
1362 | Double TEncRCPic::calAverageQP() |
---|
1363 | { |
---|
1364 | Int totalQPs = 0; |
---|
1365 | Int numTotalLCUs = 0; |
---|
1366 | |
---|
1367 | Int i; |
---|
1368 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
1369 | { |
---|
1370 | if ( m_LCUs[i].m_QP > 0 ) |
---|
1371 | { |
---|
1372 | totalQPs += m_LCUs[i].m_QP; |
---|
1373 | numTotalLCUs++; |
---|
1374 | } |
---|
1375 | } |
---|
1376 | |
---|
1377 | Double avgQP = 0.0; |
---|
1378 | if ( numTotalLCUs == 0 ) |
---|
1379 | { |
---|
1380 | avgQP = g_RCInvalidQPValue; |
---|
1381 | } |
---|
1382 | else |
---|
1383 | { |
---|
1384 | avgQP = ((Double)totalQPs) / ((Double)numTotalLCUs); |
---|
1385 | } |
---|
1386 | return avgQP; |
---|
1387 | } |
---|
1388 | |
---|
1389 | Double TEncRCPic::calAverageLambda() |
---|
1390 | { |
---|
1391 | Double totalLambdas = 0.0; |
---|
1392 | Int numTotalLCUs = 0; |
---|
1393 | |
---|
1394 | Int i; |
---|
1395 | for ( i=0; i<m_numberOfLCU; i++ ) |
---|
1396 | { |
---|
1397 | if ( m_LCUs[i].m_lambda > 0.01 ) |
---|
1398 | { |
---|
1399 | totalLambdas += log( m_LCUs[i].m_lambda ); |
---|
1400 | numTotalLCUs++; |
---|
1401 | } |
---|
1402 | } |
---|
1403 | |
---|
1404 | Double avgLambda; |
---|
1405 | if( numTotalLCUs == 0 ) |
---|
1406 | { |
---|
1407 | avgLambda = -1.0; |
---|
1408 | } |
---|
1409 | else |
---|
1410 | { |
---|
1411 | avgLambda = pow( 2.7183, totalLambdas / numTotalLCUs ); |
---|
1412 | } |
---|
1413 | return avgLambda; |
---|
1414 | } |
---|
1415 | |
---|
1416 | #if M0036_RC_IMPROVEMENT |
---|
1417 | #if RATE_CONTROL_INTRA |
---|
1418 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, SliceType eSliceType) |
---|
1419 | #else |
---|
1420 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda ) |
---|
1421 | #endif |
---|
1422 | #else |
---|
1423 | Void TEncRCPic::updateAfterPicture( Int actualHeaderBits, Int actualTotalBits, Double averageQP, Double averageLambda, Double effectivePercentage ) |
---|
1424 | #endif |
---|
1425 | { |
---|
1426 | m_picActualHeaderBits = actualHeaderBits; |
---|
1427 | m_picActualBits = actualTotalBits; |
---|
1428 | if ( averageQP > 0.0 ) |
---|
1429 | { |
---|
1430 | m_picQP = Int( averageQP + 0.5 ); |
---|
1431 | } |
---|
1432 | else |
---|
1433 | { |
---|
1434 | m_picQP = g_RCInvalidQPValue; |
---|
1435 | } |
---|
1436 | m_picLambda = averageLambda; |
---|
1437 | #if !M0036_RC_IMPROVEMENT || KWU_RC_MADPRED_E0227 |
---|
1438 | #if KWU_RC_MADPRED_E0227 |
---|
1439 | m_totalMAD = 0; |
---|
1440 | #endif |
---|
1441 | for ( Int i=0; i<m_numberOfLCU; i++ ) |
---|
1442 | { |
---|
1443 | m_totalMAD += m_LCUs[i].m_MAD; |
---|
1444 | } |
---|
1445 | #endif |
---|
1446 | |
---|
1447 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
1448 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
1449 | #if RATE_CONTROL_INTRA |
---|
1450 | if (eSliceType == I_SLICE) |
---|
1451 | { |
---|
1452 | updateAlphaBetaIntra(&alpha, &beta); |
---|
1453 | } |
---|
1454 | else |
---|
1455 | { |
---|
1456 | #endif |
---|
1457 | // update parameters |
---|
1458 | Double picActualBits = ( Double )m_picActualBits; |
---|
1459 | Double picActualBpp = picActualBits/(Double)m_numberOfPixel; |
---|
1460 | Double calLambda = alpha * pow( picActualBpp, beta ); |
---|
1461 | Double inputLambda = m_picLambda; |
---|
1462 | |
---|
1463 | #if M0036_RC_IMPROVEMENT |
---|
1464 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 ) |
---|
1465 | #else |
---|
1466 | if ( inputLambda < 0.01 || calLambda < 0.01 || picActualBpp < 0.0001 || effectivePercentage < 0.05 ) |
---|
1467 | #endif |
---|
1468 | { |
---|
1469 | alpha *= ( 1.0 - m_encRCSeq->getAlphaUpdate() / 2.0 ); |
---|
1470 | beta *= ( 1.0 - m_encRCSeq->getBetaUpdate() / 2.0 ); |
---|
1471 | |
---|
1472 | #if M0036_RC_IMPROVEMENT |
---|
1473 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1474 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1475 | #else |
---|
1476 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
1477 | beta = Clip3( -3.0, -0.1, beta ); |
---|
1478 | #endif |
---|
1479 | TRCParameter rcPara; |
---|
1480 | rcPara.m_alpha = alpha; |
---|
1481 | rcPara.m_beta = beta; |
---|
1482 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
1483 | |
---|
1484 | return; |
---|
1485 | } |
---|
1486 | |
---|
1487 | calLambda = Clip3( inputLambda / 10.0, inputLambda * 10.0, calLambda ); |
---|
1488 | alpha += m_encRCSeq->getAlphaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * alpha; |
---|
1489 | double lnbpp = log( picActualBpp ); |
---|
1490 | #if M0036_RC_IMPROVEMENT |
---|
1491 | lnbpp = Clip3( -5.0, -0.1, lnbpp ); |
---|
1492 | #else |
---|
1493 | lnbpp = Clip3( -5.0, 1.0, lnbpp ); |
---|
1494 | #endif |
---|
1495 | beta += m_encRCSeq->getBetaUpdate() * ( log( inputLambda ) - log( calLambda ) ) * lnbpp; |
---|
1496 | |
---|
1497 | #if M0036_RC_IMPROVEMENT |
---|
1498 | alpha = Clip3( g_RCAlphaMinValue, g_RCAlphaMaxValue, alpha ); |
---|
1499 | beta = Clip3( g_RCBetaMinValue, g_RCBetaMaxValue, beta ); |
---|
1500 | #else |
---|
1501 | alpha = Clip3( 0.05, 20.0, alpha ); |
---|
1502 | beta = Clip3( -3.0, -0.1, beta ); |
---|
1503 | #endif |
---|
1504 | #if RATE_CONTROL_INTRA |
---|
1505 | } |
---|
1506 | #endif |
---|
1507 | |
---|
1508 | TRCParameter rcPara; |
---|
1509 | rcPara.m_alpha = alpha; |
---|
1510 | rcPara.m_beta = beta; |
---|
1511 | |
---|
1512 | m_encRCSeq->setPicPara( m_frameLevel, rcPara ); |
---|
1513 | |
---|
1514 | #if M0036_RC_IMPROVEMENT |
---|
1515 | if ( m_frameLevel == 1 ) |
---|
1516 | { |
---|
1517 | Double currLambda = Clip3( 0.1, 10000.0, m_picLambda ); |
---|
1518 | Double updateLastLambda = g_RCWeightHistoryLambda * m_encRCSeq->getLastLambda() + g_RCWeightCurrentLambda * currLambda; |
---|
1519 | m_encRCSeq->setLastLambda( updateLastLambda ); |
---|
1520 | } |
---|
1521 | #endif |
---|
1522 | } |
---|
1523 | |
---|
1524 | #if RATE_CONTROL_INTRA |
---|
1525 | Int TEncRCPic::getRefineBitsForIntra( Int orgBits ) |
---|
1526 | { |
---|
1527 | Double alpha=0.25, beta=0.5582; |
---|
1528 | Int iIntraBits; |
---|
1529 | |
---|
1530 | if (orgBits*40 < m_numberOfPixel) |
---|
1531 | { |
---|
1532 | alpha=0.25; |
---|
1533 | } |
---|
1534 | else |
---|
1535 | { |
---|
1536 | alpha=0.30; |
---|
1537 | } |
---|
1538 | |
---|
1539 | iIntraBits = (Int)(alpha* pow(m_totalCostIntra*4.0/(Double)orgBits, beta)*(Double)orgBits+0.5); |
---|
1540 | |
---|
1541 | return iIntraBits; |
---|
1542 | } |
---|
1543 | |
---|
1544 | Double TEncRCPic::calculateLambdaIntra(double alpha, double beta, double MADPerPixel, double bitsPerPixel) |
---|
1545 | { |
---|
1546 | return ( (alpha/256.0) * pow( MADPerPixel/bitsPerPixel, beta ) ); |
---|
1547 | } |
---|
1548 | |
---|
1549 | Void TEncRCPic::updateAlphaBetaIntra(double *alpha, double *beta) |
---|
1550 | { |
---|
1551 | Double lnbpp = log(pow(m_totalCostIntra / (Double)m_numberOfPixel, BETA1)); |
---|
1552 | Double diffLambda = (*beta)*(log((Double)m_picActualBits)-log((Double)m_targetBits)); |
---|
1553 | |
---|
1554 | diffLambda = Clip3(-0.125, 0.125, 0.25*diffLambda); |
---|
1555 | *alpha = (*alpha) * exp(diffLambda); |
---|
1556 | *beta = (*beta) + diffLambda / lnbpp; |
---|
1557 | } |
---|
1558 | |
---|
1559 | |
---|
1560 | Void TEncRCPic::getLCUInitTargetBits() |
---|
1561 | { |
---|
1562 | Int iAvgBits = 0; |
---|
1563 | |
---|
1564 | m_remainingCostIntra = m_totalCostIntra; |
---|
1565 | for (Int i=m_numberOfLCU-1; i>=0; i--) |
---|
1566 | { |
---|
1567 | iAvgBits += Int(m_targetBits * getLCU(i).m_costIntra/m_totalCostIntra); |
---|
1568 | getLCU(i).m_targetBitsLeft = iAvgBits; |
---|
1569 | } |
---|
1570 | } |
---|
1571 | |
---|
1572 | |
---|
1573 | Double TEncRCPic::getLCUEstLambdaAndQP(Double bpp, Int clipPicQP, Int *estQP) |
---|
1574 | { |
---|
1575 | Int LCUIdx = getLCUCoded(); |
---|
1576 | |
---|
1577 | Double alpha = m_encRCSeq->getPicPara( m_frameLevel ).m_alpha; |
---|
1578 | Double beta = m_encRCSeq->getPicPara( m_frameLevel ).m_beta; |
---|
1579 | |
---|
1580 | Double costPerPixel = getLCU(LCUIdx).m_costIntra/(Double)getLCU(LCUIdx).m_numberOfPixel; |
---|
1581 | costPerPixel = pow(costPerPixel, BETA1); |
---|
1582 | Double estLambda = calculateLambdaIntra(alpha, beta, costPerPixel, bpp); |
---|
1583 | |
---|
1584 | Int clipNeighbourQP = g_RCInvalidQPValue; |
---|
1585 | for (int i=LCUIdx-1; i>=0; i--) |
---|
1586 | { |
---|
1587 | if ((getLCU(i)).m_QP > g_RCInvalidQPValue) |
---|
1588 | { |
---|
1589 | clipNeighbourQP = getLCU(i).m_QP; |
---|
1590 | break; |
---|
1591 | } |
---|
1592 | } |
---|
1593 | |
---|
1594 | Int minQP = clipPicQP - 2; |
---|
1595 | Int maxQP = clipPicQP + 2; |
---|
1596 | |
---|
1597 | if ( clipNeighbourQP > g_RCInvalidQPValue ) |
---|
1598 | { |
---|
1599 | maxQP = min(clipNeighbourQP + 1, maxQP); |
---|
1600 | minQP = max(clipNeighbourQP - 1, minQP); |
---|
1601 | } |
---|
1602 | |
---|
1603 | Double maxLambda=exp(((Double)(maxQP+0.49)-13.7122)/4.2005); |
---|
1604 | Double minLambda=exp(((Double)(minQP-0.49)-13.7122)/4.2005); |
---|
1605 | |
---|
1606 | estLambda = Clip3(minLambda, maxLambda, estLambda); |
---|
1607 | |
---|
1608 | *estQP = Int( 4.2005 * log(estLambda) + 13.7122 + 0.5 ); |
---|
1609 | *estQP = Clip3(minQP, maxQP, *estQP); |
---|
1610 | |
---|
1611 | return estLambda; |
---|
1612 | } |
---|
1613 | #endif |
---|
1614 | |
---|
1615 | TEncRateCtrl::TEncRateCtrl() |
---|
1616 | { |
---|
1617 | m_encRCSeq = NULL; |
---|
1618 | m_encRCGOP = NULL; |
---|
1619 | m_encRCPic = NULL; |
---|
1620 | } |
---|
1621 | |
---|
1622 | TEncRateCtrl::~TEncRateCtrl() |
---|
1623 | { |
---|
1624 | destroy(); |
---|
1625 | } |
---|
1626 | |
---|
1627 | Void TEncRateCtrl::destroy() |
---|
1628 | { |
---|
1629 | if ( m_encRCSeq != NULL ) |
---|
1630 | { |
---|
1631 | delete m_encRCSeq; |
---|
1632 | m_encRCSeq = NULL; |
---|
1633 | } |
---|
1634 | if ( m_encRCGOP != NULL ) |
---|
1635 | { |
---|
1636 | delete m_encRCGOP; |
---|
1637 | m_encRCGOP = NULL; |
---|
1638 | } |
---|
1639 | while ( m_listRCPictures.size() > 0 ) |
---|
1640 | { |
---|
1641 | TEncRCPic* p = m_listRCPictures.front(); |
---|
1642 | m_listRCPictures.pop_front(); |
---|
1643 | delete p; |
---|
1644 | } |
---|
1645 | } |
---|
1646 | |
---|
1647 | #if M0036_RC_IMPROVEMENT |
---|
1648 | #if KWU_RC_MADPRED_E0227 |
---|
1649 | 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 ) |
---|
1650 | #else |
---|
1651 | 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] ) |
---|
1652 | #endif |
---|
1653 | #else |
---|
1654 | 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] ) |
---|
1655 | #endif |
---|
1656 | { |
---|
1657 | destroy(); |
---|
1658 | |
---|
1659 | Bool isLowdelay = true; |
---|
1660 | for ( Int i=0; i<GOPSize-1; i++ ) |
---|
1661 | { |
---|
1662 | if ( GOPList[i].m_POC > GOPList[i+1].m_POC ) |
---|
1663 | { |
---|
1664 | isLowdelay = false; |
---|
1665 | break; |
---|
1666 | } |
---|
1667 | } |
---|
1668 | |
---|
1669 | Int numberOfLevel = 1; |
---|
1670 | #if M0036_RC_IMPROVEMENT |
---|
1671 | Int adaptiveBit = 0; |
---|
1672 | if ( keepHierBits > 0 ) |
---|
1673 | #else |
---|
1674 | if ( keepHierBits ) |
---|
1675 | #endif |
---|
1676 | { |
---|
1677 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
1678 | } |
---|
1679 | if ( !isLowdelay && GOPSize == 8 ) |
---|
1680 | { |
---|
1681 | numberOfLevel = Int( log((Double)GOPSize)/log(2.0) + 0.5 ) + 1; |
---|
1682 | } |
---|
1683 | numberOfLevel++; // intra picture |
---|
1684 | numberOfLevel++; // non-reference picture |
---|
1685 | |
---|
1686 | |
---|
1687 | Int* bitsRatio; |
---|
1688 | bitsRatio = new Int[ GOPSize ]; |
---|
1689 | for ( Int i=0; i<GOPSize; i++ ) |
---|
1690 | { |
---|
1691 | bitsRatio[i] = 10; |
---|
1692 | if ( !GOPList[i].m_refPic ) |
---|
1693 | { |
---|
1694 | bitsRatio[i] = 2; |
---|
1695 | } |
---|
1696 | } |
---|
1697 | |
---|
1698 | #if M0036_RC_IMPROVEMENT |
---|
1699 | if ( keepHierBits > 0 ) |
---|
1700 | #else |
---|
1701 | if ( keepHierBits ) |
---|
1702 | #endif |
---|
1703 | { |
---|
1704 | Double bpp = (Double)( targetBitrate / (Double)( frameRate*picWidth*picHeight ) ); |
---|
1705 | if ( GOPSize == 4 && isLowdelay ) |
---|
1706 | { |
---|
1707 | if ( bpp > 0.2 ) |
---|
1708 | { |
---|
1709 | bitsRatio[0] = 2; |
---|
1710 | bitsRatio[1] = 3; |
---|
1711 | bitsRatio[2] = 2; |
---|
1712 | bitsRatio[3] = 6; |
---|
1713 | } |
---|
1714 | else if( bpp > 0.1 ) |
---|
1715 | { |
---|
1716 | bitsRatio[0] = 2; |
---|
1717 | bitsRatio[1] = 3; |
---|
1718 | bitsRatio[2] = 2; |
---|
1719 | bitsRatio[3] = 10; |
---|
1720 | } |
---|
1721 | else if ( bpp > 0.05 ) |
---|
1722 | { |
---|
1723 | bitsRatio[0] = 2; |
---|
1724 | bitsRatio[1] = 3; |
---|
1725 | bitsRatio[2] = 2; |
---|
1726 | bitsRatio[3] = 12; |
---|
1727 | } |
---|
1728 | else |
---|
1729 | { |
---|
1730 | bitsRatio[0] = 2; |
---|
1731 | bitsRatio[1] = 3; |
---|
1732 | bitsRatio[2] = 2; |
---|
1733 | bitsRatio[3] = 14; |
---|
1734 | } |
---|
1735 | #if M0036_RC_IMPROVEMENT |
---|
1736 | if ( keepHierBits == 2 ) |
---|
1737 | { |
---|
1738 | adaptiveBit = 1; |
---|
1739 | } |
---|
1740 | #endif |
---|
1741 | } |
---|
1742 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
1743 | { |
---|
1744 | if ( bpp > 0.2 ) |
---|
1745 | { |
---|
1746 | bitsRatio[0] = 15; |
---|
1747 | bitsRatio[1] = 5; |
---|
1748 | bitsRatio[2] = 4; |
---|
1749 | bitsRatio[3] = 1; |
---|
1750 | bitsRatio[4] = 1; |
---|
1751 | bitsRatio[5] = 4; |
---|
1752 | bitsRatio[6] = 1; |
---|
1753 | bitsRatio[7] = 1; |
---|
1754 | } |
---|
1755 | else if ( bpp > 0.1 ) |
---|
1756 | { |
---|
1757 | bitsRatio[0] = 20; |
---|
1758 | bitsRatio[1] = 6; |
---|
1759 | bitsRatio[2] = 4; |
---|
1760 | bitsRatio[3] = 1; |
---|
1761 | bitsRatio[4] = 1; |
---|
1762 | bitsRatio[5] = 4; |
---|
1763 | bitsRatio[6] = 1; |
---|
1764 | bitsRatio[7] = 1; |
---|
1765 | } |
---|
1766 | else if ( bpp > 0.05 ) |
---|
1767 | { |
---|
1768 | bitsRatio[0] = 25; |
---|
1769 | bitsRatio[1] = 7; |
---|
1770 | bitsRatio[2] = 4; |
---|
1771 | bitsRatio[3] = 1; |
---|
1772 | bitsRatio[4] = 1; |
---|
1773 | bitsRatio[5] = 4; |
---|
1774 | bitsRatio[6] = 1; |
---|
1775 | bitsRatio[7] = 1; |
---|
1776 | } |
---|
1777 | else |
---|
1778 | { |
---|
1779 | bitsRatio[0] = 30; |
---|
1780 | bitsRatio[1] = 8; |
---|
1781 | bitsRatio[2] = 4; |
---|
1782 | bitsRatio[3] = 1; |
---|
1783 | bitsRatio[4] = 1; |
---|
1784 | bitsRatio[5] = 4; |
---|
1785 | bitsRatio[6] = 1; |
---|
1786 | bitsRatio[7] = 1; |
---|
1787 | } |
---|
1788 | #if M0036_RC_IMPROVEMENT |
---|
1789 | if ( keepHierBits == 2 ) |
---|
1790 | { |
---|
1791 | adaptiveBit = 2; |
---|
1792 | } |
---|
1793 | #endif |
---|
1794 | } |
---|
1795 | else |
---|
1796 | { |
---|
1797 | #if M0036_RC_IMPROVEMENT |
---|
1798 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently.\n" ); |
---|
1799 | #else |
---|
1800 | printf( "\n hierarchical bit allocation is not support for the specified coding structure currently." ); |
---|
1801 | #endif |
---|
1802 | } |
---|
1803 | } |
---|
1804 | |
---|
1805 | Int* GOPID2Level = new int[ GOPSize ]; |
---|
1806 | for ( int i=0; i<GOPSize; i++ ) |
---|
1807 | { |
---|
1808 | GOPID2Level[i] = 1; |
---|
1809 | if ( !GOPList[i].m_refPic ) |
---|
1810 | { |
---|
1811 | GOPID2Level[i] = 2; |
---|
1812 | } |
---|
1813 | } |
---|
1814 | #if M0036_RC_IMPROVEMENT |
---|
1815 | if ( keepHierBits > 0 ) |
---|
1816 | #else |
---|
1817 | if ( keepHierBits ) |
---|
1818 | #endif |
---|
1819 | { |
---|
1820 | if ( GOPSize == 4 && isLowdelay ) |
---|
1821 | { |
---|
1822 | GOPID2Level[0] = 3; |
---|
1823 | GOPID2Level[1] = 2; |
---|
1824 | GOPID2Level[2] = 3; |
---|
1825 | GOPID2Level[3] = 1; |
---|
1826 | } |
---|
1827 | else if ( GOPSize == 8 && !isLowdelay ) |
---|
1828 | { |
---|
1829 | GOPID2Level[0] = 1; |
---|
1830 | GOPID2Level[1] = 2; |
---|
1831 | GOPID2Level[2] = 3; |
---|
1832 | GOPID2Level[3] = 4; |
---|
1833 | GOPID2Level[4] = 4; |
---|
1834 | GOPID2Level[5] = 3; |
---|
1835 | GOPID2Level[6] = 4; |
---|
1836 | GOPID2Level[7] = 4; |
---|
1837 | } |
---|
1838 | } |
---|
1839 | |
---|
1840 | if ( !isLowdelay && GOPSize == 8 ) |
---|
1841 | { |
---|
1842 | GOPID2Level[0] = 1; |
---|
1843 | GOPID2Level[1] = 2; |
---|
1844 | GOPID2Level[2] = 3; |
---|
1845 | GOPID2Level[3] = 4; |
---|
1846 | GOPID2Level[4] = 4; |
---|
1847 | GOPID2Level[5] = 3; |
---|
1848 | GOPID2Level[6] = 4; |
---|
1849 | GOPID2Level[7] = 4; |
---|
1850 | } |
---|
1851 | |
---|
1852 | m_encRCSeq = new TEncRCSeq; |
---|
1853 | #if M0036_RC_IMPROVEMENT |
---|
1854 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel, adaptiveBit ); |
---|
1855 | #else |
---|
1856 | m_encRCSeq->create( totalFrames, targetBitrate, frameRate, GOPSize, picWidth, picHeight, LCUWidth, LCUHeight, numberOfLevel, useLCUSeparateModel ); |
---|
1857 | #endif |
---|
1858 | m_encRCSeq->initBitsRatio( bitsRatio ); |
---|
1859 | m_encRCSeq->initGOPID2Level( GOPID2Level ); |
---|
1860 | m_encRCSeq->initPicPara(); |
---|
1861 | if ( useLCUSeparateModel ) |
---|
1862 | { |
---|
1863 | m_encRCSeq->initLCUPara(); |
---|
1864 | } |
---|
1865 | |
---|
1866 | #if KWU_RC_MADPRED_E0227 |
---|
1867 | setLayerID(layerID); |
---|
1868 | #endif |
---|
1869 | |
---|
1870 | delete[] bitsRatio; |
---|
1871 | delete[] GOPID2Level; |
---|
1872 | } |
---|
1873 | |
---|
1874 | Void TEncRateCtrl::initRCPic( Int frameLevel ) |
---|
1875 | { |
---|
1876 | m_encRCPic = new TEncRCPic; |
---|
1877 | #if KWU_RC_MADPRED_E0227 |
---|
1878 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures, m_LayerID ); |
---|
1879 | #else |
---|
1880 | m_encRCPic->create( m_encRCSeq, m_encRCGOP, frameLevel, m_listRCPictures ); |
---|
1881 | #endif |
---|
1882 | } |
---|
1883 | |
---|
1884 | Void TEncRateCtrl::initRCGOP( Int numberOfPictures ) |
---|
1885 | { |
---|
1886 | m_encRCGOP = new TEncRCGOP; |
---|
1887 | m_encRCGOP->create( m_encRCSeq, numberOfPictures ); |
---|
1888 | } |
---|
1889 | |
---|
1890 | Void TEncRateCtrl::destroyRCGOP() |
---|
1891 | { |
---|
1892 | delete m_encRCGOP; |
---|
1893 | m_encRCGOP = NULL; |
---|
1894 | } |
---|
1895 | |
---|
1896 | #else |
---|
1897 | |
---|
1898 | #define ADJUSTMENT_FACTOR 0.60 |
---|
1899 | #define HIGH_QSTEP_THRESHOLD 9.5238 |
---|
1900 | #define HIGH_QSTEP_ALPHA 4.9371 |
---|
1901 | #define HIGH_QSTEP_BETA 0.0922 |
---|
1902 | #define LOW_QSTEP_ALPHA 16.7429 |
---|
1903 | #define LOW_QSTEP_BETA -1.1494 |
---|
1904 | |
---|
1905 | #define MAD_PRED_Y1 1.0 |
---|
1906 | #define MAD_PRED_Y2 0.0 |
---|
1907 | |
---|
1908 | enum MAD_HISOTRY { |
---|
1909 | MAD_PPPrevious = 0, |
---|
1910 | MAD_PPrevious = 1, |
---|
1911 | MAD_Previous = 2 |
---|
1912 | }; |
---|
1913 | |
---|
1914 | Void MADLinearModel::initMADLinearModel() |
---|
1915 | { |
---|
1916 | m_activeOn = false; |
---|
1917 | m_paramY1 = 1.0; |
---|
1918 | m_paramY2 = 0.0; |
---|
1919 | m_costMADs[0] = m_costMADs[1] = m_costMADs[2] = 0.0; |
---|
1920 | } |
---|
1921 | |
---|
1922 | Double MADLinearModel::getMAD() |
---|
1923 | { |
---|
1924 | Double costPredMAD = m_paramY1 * m_costMADs[MAD_Previous] + m_paramY2; |
---|
1925 | |
---|
1926 | if(costPredMAD < 0) |
---|
1927 | { |
---|
1928 | costPredMAD = m_costMADs[MAD_Previous]; |
---|
1929 | m_paramY1 = MAD_PRED_Y1; |
---|
1930 | m_paramY2 = MAD_PRED_Y2; |
---|
1931 | } |
---|
1932 | return costPredMAD; |
---|
1933 | } |
---|
1934 | |
---|
1935 | Void MADLinearModel::updateMADLiearModel() |
---|
1936 | { |
---|
1937 | Double dNewY1 = ((m_costMADs[MAD_Previous] - m_costMADs[MAD_PPrevious]) / (m_costMADs[MAD_PPrevious] - m_costMADs[MAD_PPPrevious])); |
---|
1938 | Double dNewY2 = (m_costMADs[MAD_Previous] - (dNewY1*m_costMADs[MAD_PPrevious])); |
---|
1939 | |
---|
1940 | m_paramY1 = 0.70+0.20*m_paramY1+ 0.10*dNewY1; |
---|
1941 | m_paramY2 = 0.20*m_paramY2+ 0.10*dNewY2; |
---|
1942 | } |
---|
1943 | |
---|
1944 | Void MADLinearModel::updateMADHistory(Double dMAD) |
---|
1945 | { |
---|
1946 | m_costMADs[MAD_PPPrevious] = m_costMADs[MAD_PPrevious]; |
---|
1947 | m_costMADs[MAD_PPrevious ] = m_costMADs[MAD_Previous ]; |
---|
1948 | m_costMADs[MAD_Previous ] = dMAD; |
---|
1949 | m_activeOn = (m_costMADs[MAD_Previous ] && m_costMADs[MAD_PPrevious ] && m_costMADs[MAD_PPPrevious]); |
---|
1950 | } |
---|
1951 | |
---|
1952 | |
---|
1953 | Void PixelBaseURQQuadraticModel::initPixelBaseQuadraticModel() |
---|
1954 | { |
---|
1955 | m_paramHighX1 = HIGH_QSTEP_ALPHA; |
---|
1956 | m_paramHighX2 = HIGH_QSTEP_BETA; |
---|
1957 | m_paramLowX1 = LOW_QSTEP_ALPHA; |
---|
1958 | m_paramLowX2 = LOW_QSTEP_BETA; |
---|
1959 | } |
---|
1960 | |
---|
1961 | Int PixelBaseURQQuadraticModel::getQP(Int qp, Int targetBits, Int numberOfPixels, Double costPredMAD) |
---|
1962 | { |
---|
1963 | Double qStep; |
---|
1964 | Double bppPerMAD = (Double)(targetBits/(numberOfPixels*costPredMAD)); |
---|
1965 | |
---|
1966 | if(xConvertQP2QStep(qp) >= HIGH_QSTEP_THRESHOLD) |
---|
1967 | { |
---|
1968 | qStep = 1/( sqrt((bppPerMAD/m_paramHighX1)+((m_paramHighX2*m_paramHighX2)/(4*m_paramHighX1*m_paramHighX1))) - (m_paramHighX2/(2*m_paramHighX1))); |
---|
1969 | } |
---|
1970 | else |
---|
1971 | { |
---|
1972 | qStep = 1/( sqrt((bppPerMAD/m_paramLowX1)+((m_paramLowX2*m_paramLowX2)/(4*m_paramLowX1*m_paramLowX1))) - (m_paramLowX2/(2*m_paramLowX1))); |
---|
1973 | } |
---|
1974 | |
---|
1975 | return xConvertQStep2QP(qStep); |
---|
1976 | } |
---|
1977 | |
---|
1978 | Void PixelBaseURQQuadraticModel::updatePixelBasedURQQuadraticModel (Int qp, Int bits, Int numberOfPixels, Double costMAD) |
---|
1979 | { |
---|
1980 | Double qStep = xConvertQP2QStep(qp); |
---|
1981 | Double invqStep = (1/qStep); |
---|
1982 | Double paramNewX1, paramNewX2; |
---|
1983 | |
---|
1984 | if(qStep >= HIGH_QSTEP_THRESHOLD) |
---|
1985 | { |
---|
1986 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(23.3772*invqStep*invqStep))/((1-200*invqStep)*invqStep)); |
---|
1987 | paramNewX1 = (23.3772-200*paramNewX2); |
---|
1988 | m_paramHighX1 = 0.70*HIGH_QSTEP_ALPHA + 0.20 * m_paramHighX1 + 0.10 * paramNewX1; |
---|
1989 | m_paramHighX2 = 0.70*HIGH_QSTEP_BETA + 0.20 * m_paramHighX2 + 0.10 * paramNewX2; |
---|
1990 | } |
---|
1991 | else |
---|
1992 | { |
---|
1993 | paramNewX2 = (((bits/(numberOfPixels*costMAD))-(5.8091*invqStep*invqStep))/((1-9.5455*invqStep)*invqStep)); |
---|
1994 | paramNewX1 = (5.8091-9.5455*paramNewX2); |
---|
1995 | m_paramLowX1 = 0.90*LOW_QSTEP_ALPHA + 0.09 * m_paramLowX1 + 0.01 * paramNewX1; |
---|
1996 | m_paramLowX2 = 0.90*LOW_QSTEP_BETA + 0.09 * m_paramLowX2 + 0.01 * paramNewX2; |
---|
1997 | } |
---|
1998 | } |
---|
1999 | |
---|
2000 | Bool PixelBaseURQQuadraticModel::checkUpdateAvailable(Int qpReference ) |
---|
2001 | { |
---|
2002 | Double qStep = xConvertQP2QStep(qpReference); |
---|
2003 | |
---|
2004 | if (qStep > xConvertQP2QStep(MAX_QP) |
---|
2005 | ||qStep < xConvertQP2QStep(MIN_QP) ) |
---|
2006 | { |
---|
2007 | return false; |
---|
2008 | } |
---|
2009 | |
---|
2010 | return true; |
---|
2011 | } |
---|
2012 | |
---|
2013 | Double PixelBaseURQQuadraticModel::xConvertQP2QStep(Int qp ) |
---|
2014 | { |
---|
2015 | Int i; |
---|
2016 | Double qStep; |
---|
2017 | static const Double mapQP2QSTEP[6] = { 0.625, 0.703, 0.797, 0.891, 1.000, 1.125 }; |
---|
2018 | |
---|
2019 | qStep = mapQP2QSTEP[qp % 6]; |
---|
2020 | for( i=0; i<(qp/6); i++) |
---|
2021 | { |
---|
2022 | qStep *= 2; |
---|
2023 | } |
---|
2024 | |
---|
2025 | return qStep; |
---|
2026 | } |
---|
2027 | |
---|
2028 | Int PixelBaseURQQuadraticModel::xConvertQStep2QP(Double qStep ) |
---|
2029 | { |
---|
2030 | Int per = 0, rem = 0; |
---|
2031 | |
---|
2032 | if( qStep < xConvertQP2QStep(MIN_QP)) |
---|
2033 | { |
---|
2034 | return MIN_QP; |
---|
2035 | } |
---|
2036 | else if (qStep > xConvertQP2QStep(MAX_QP) ) |
---|
2037 | { |
---|
2038 | return MAX_QP; |
---|
2039 | } |
---|
2040 | |
---|
2041 | while( qStep > xConvertQP2QStep(5) ) |
---|
2042 | { |
---|
2043 | qStep /= 2.0; |
---|
2044 | per++; |
---|
2045 | } |
---|
2046 | |
---|
2047 | if (qStep <= 0.625) |
---|
2048 | { |
---|
2049 | rem = 0; |
---|
2050 | } |
---|
2051 | else if (qStep <= 0.703) |
---|
2052 | { |
---|
2053 | rem = 1; |
---|
2054 | } |
---|
2055 | else if (qStep <= 0.797) |
---|
2056 | { |
---|
2057 | rem = 2; |
---|
2058 | } |
---|
2059 | else if (qStep <= 0.891) |
---|
2060 | { |
---|
2061 | rem = 3; |
---|
2062 | } |
---|
2063 | else if (qStep <= 1.000) |
---|
2064 | { |
---|
2065 | rem = 4; |
---|
2066 | } |
---|
2067 | else |
---|
2068 | { |
---|
2069 | rem = 5; |
---|
2070 | } |
---|
2071 | return (per * 6 + rem); |
---|
2072 | } |
---|
2073 | |
---|
2074 | |
---|
2075 | Void TEncRateCtrl::create(Int sizeIntraPeriod, Int sizeGOP, Int frameRate, Int targetKbps, Int qp, Int numLCUInBasicUnit, Int sourceWidth, Int sourceHeight, Int maxCUWidth, Int maxCUHeight) |
---|
2076 | { |
---|
2077 | Int leftInHeight, leftInWidth; |
---|
2078 | |
---|
2079 | m_sourceWidthInLCU = (sourceWidth / maxCUWidth ) + (( sourceWidth % maxCUWidth ) ? 1 : 0); |
---|
2080 | m_sourceHeightInLCU = (sourceHeight / maxCUHeight) + (( sourceHeight % maxCUHeight) ? 1 : 0); |
---|
2081 | m_isLowdelay = (sizeIntraPeriod == -1) ? true : false; |
---|
2082 | |
---|
2083 | m_prevBitrate = ( targetKbps << 10 ); // in units of 1,024 bps |
---|
2084 | m_currBitrate = ( targetKbps << 10 ); |
---|
2085 | |
---|
2086 | m_frameRate = frameRate; |
---|
2087 | m_refFrameNum = m_isLowdelay ? (sizeGOP) : (sizeGOP>>1); |
---|
2088 | m_nonRefFrameNum = sizeGOP-m_refFrameNum; |
---|
2089 | m_sizeGOP = sizeGOP; |
---|
2090 | m_numOfPixels = ((sourceWidth*sourceHeight*3)>>1); |
---|
2091 | m_indexGOP = 0; |
---|
2092 | m_indexFrame = 0; |
---|
2093 | m_indexLCU = 0; |
---|
2094 | m_indexUnit = 0; |
---|
2095 | m_indexRefFrame = 0; |
---|
2096 | m_indexNonRefFrame = 0; |
---|
2097 | m_occupancyVB = 0; |
---|
2098 | m_initialOVB = 0; |
---|
2099 | m_targetBufLevel = 0; |
---|
2100 | m_initialTBL = 0; |
---|
2101 | m_occupancyVBInFrame = 0; |
---|
2102 | m_remainingBitsInGOP = (m_currBitrate*sizeGOP/m_frameRate); |
---|
2103 | m_remainingBitsInFrame = 0; |
---|
2104 | m_numUnitInFrame = m_sourceWidthInLCU*m_sourceHeightInLCU; |
---|
2105 | m_cMADLinearModel. initMADLinearModel(); |
---|
2106 | m_cPixelURQQuadraticModel.initPixelBaseQuadraticModel(); |
---|
2107 | |
---|
2108 | m_costRefAvgWeighting = 0.0; |
---|
2109 | m_costNonRefAvgWeighting = 0.0; |
---|
2110 | m_costAvgbpp = 0.0; |
---|
2111 | m_activeUnitLevelOn = false; |
---|
2112 | |
---|
2113 | m_pcFrameData = new FrameData [sizeGOP+1]; initFrameData(qp); |
---|
2114 | m_pcLCUData = new LCUData [m_numUnitInFrame]; initUnitData (qp); |
---|
2115 | |
---|
2116 | for(Int i = 0, addressUnit = 0; i < m_sourceHeightInLCU*maxCUHeight; i += maxCUHeight) |
---|
2117 | { |
---|
2118 | leftInHeight = sourceHeight - i; |
---|
2119 | leftInHeight = min(leftInHeight, maxCUHeight); |
---|
2120 | for(Int j = 0; j < m_sourceWidthInLCU*maxCUWidth; j += maxCUWidth, addressUnit++) |
---|
2121 | { |
---|
2122 | leftInWidth = sourceWidth - j; |
---|
2123 | leftInWidth = min(leftInWidth, maxCUWidth); |
---|
2124 | m_pcLCUData[addressUnit].m_widthInPixel = leftInWidth; |
---|
2125 | m_pcLCUData[addressUnit].m_heightInPixel= leftInHeight; |
---|
2126 | m_pcLCUData[addressUnit].m_pixels = ((leftInHeight*leftInWidth*3)>>1); |
---|
2127 | } |
---|
2128 | } |
---|
2129 | } |
---|
2130 | |
---|
2131 | Void TEncRateCtrl::destroy() |
---|
2132 | { |
---|
2133 | if(m_pcFrameData) |
---|
2134 | { |
---|
2135 | delete [] m_pcFrameData; |
---|
2136 | m_pcFrameData = NULL; |
---|
2137 | } |
---|
2138 | if(m_pcLCUData) |
---|
2139 | { |
---|
2140 | delete [] m_pcLCUData; |
---|
2141 | m_pcLCUData = NULL; |
---|
2142 | } |
---|
2143 | } |
---|
2144 | |
---|
2145 | Void TEncRateCtrl::initFrameData (Int qp) |
---|
2146 | { |
---|
2147 | for(Int i = 0 ; i <= m_sizeGOP; i++) |
---|
2148 | { |
---|
2149 | m_pcFrameData[i].m_isReferenced = false; |
---|
2150 | m_pcFrameData[i].m_costMAD = 0.0; |
---|
2151 | m_pcFrameData[i].m_bits = 0; |
---|
2152 | m_pcFrameData[i].m_qp = qp; |
---|
2153 | } |
---|
2154 | } |
---|
2155 | |
---|
2156 | Void TEncRateCtrl::initUnitData (Int qp) |
---|
2157 | { |
---|
2158 | for(Int i = 1 ; i < m_numUnitInFrame; i++) |
---|
2159 | { |
---|
2160 | m_pcLCUData[i].m_qp = qp; |
---|
2161 | m_pcLCUData[i].m_bits = 0; |
---|
2162 | m_pcLCUData[i].m_pixels = 0; |
---|
2163 | m_pcLCUData[i].m_widthInPixel = 0; |
---|
2164 | m_pcLCUData[i].m_heightInPixel = 0; |
---|
2165 | m_pcLCUData[i].m_costMAD = 0.0; |
---|
2166 | } |
---|
2167 | } |
---|
2168 | |
---|
2169 | Int TEncRateCtrl::getFrameQP(Bool isReferenced, Int POC) |
---|
2170 | { |
---|
2171 | Int numofReferenced = 0; |
---|
2172 | Int finalQP = 0; |
---|
2173 | FrameData* pcFrameData; |
---|
2174 | |
---|
2175 | m_indexPOCInGOP = (POC%m_sizeGOP) == 0 ? m_sizeGOP : (POC%m_sizeGOP); |
---|
2176 | pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
---|
2177 | |
---|
2178 | if(m_indexFrame != 0) |
---|
2179 | { |
---|
2180 | if(isReferenced) |
---|
2181 | { |
---|
2182 | Double gamma = m_isLowdelay ? 0.5 : 0.25; |
---|
2183 | Double beta = m_isLowdelay ? 0.9 : 0.6; |
---|
2184 | Int numRemainingRefFrames = m_refFrameNum - m_indexRefFrame; |
---|
2185 | Int numRemainingNRefFrames = m_nonRefFrameNum - m_indexNonRefFrame; |
---|
2186 | |
---|
2187 | Double targetBitsOccupancy = (m_currBitrate/(Double)m_frameRate) + gamma*(m_targetBufLevel-m_occupancyVB - (m_initialOVB/(Double)m_frameRate)); |
---|
2188 | Double targetBitsLeftBudget = ((m_costRefAvgWeighting*m_remainingBitsInGOP)/((m_costRefAvgWeighting*numRemainingRefFrames)+(m_costNonRefAvgWeighting*numRemainingNRefFrames))); |
---|
2189 | |
---|
2190 | m_targetBits = (Int)(beta * targetBitsLeftBudget + (1-beta) * targetBitsOccupancy); |
---|
2191 | |
---|
2192 | if(m_targetBits <= 0 || m_remainingBitsInGOP <= 0) |
---|
2193 | { |
---|
2194 | finalQP = m_pcFrameData[m_indexPrevPOCInGOP].m_qp + 2; |
---|
2195 | } |
---|
2196 | else |
---|
2197 | { |
---|
2198 | Double costPredMAD = m_cMADLinearModel.getMAD(); |
---|
2199 | Int qpLowerBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp-2; |
---|
2200 | Int qpUpperBound = m_pcFrameData[m_indexPrevPOCInGOP].m_qp+2; |
---|
2201 | finalQP = m_cPixelURQQuadraticModel.getQP(m_pcFrameData[m_indexPrevPOCInGOP].m_qp, m_targetBits, m_numOfPixels, costPredMAD); |
---|
2202 | finalQP = max(qpLowerBound, min(qpUpperBound, finalQP)); |
---|
2203 | m_activeUnitLevelOn = true; |
---|
2204 | m_remainingBitsInFrame = m_targetBits; |
---|
2205 | m_costAvgbpp = (m_targetBits/(Double)m_numOfPixels); |
---|
2206 | } |
---|
2207 | |
---|
2208 | m_indexRefFrame++; |
---|
2209 | } |
---|
2210 | else |
---|
2211 | { |
---|
2212 | Int bwdQP = m_pcFrameData[m_indexPOCInGOP-1].m_qp; |
---|
2213 | Int fwdQP = m_pcFrameData[m_indexPOCInGOP+1].m_qp; |
---|
2214 | |
---|
2215 | if( (fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP-1].m_qp |
---|
2216 | ||(fwdQP+bwdQP) == m_pcFrameData[m_indexPOCInGOP+1].m_qp) |
---|
2217 | { |
---|
2218 | finalQP = (fwdQP+bwdQP); |
---|
2219 | } |
---|
2220 | else if(bwdQP != fwdQP) |
---|
2221 | { |
---|
2222 | finalQP = ((bwdQP+fwdQP+2)>>1); |
---|
2223 | } |
---|
2224 | else |
---|
2225 | { |
---|
2226 | finalQP = bwdQP+2; |
---|
2227 | } |
---|
2228 | m_indexNonRefFrame++; |
---|
2229 | } |
---|
2230 | } |
---|
2231 | else |
---|
2232 | { |
---|
2233 | Int lastQPminus2 = m_pcFrameData[0].m_qp - 2; |
---|
2234 | Int lastQPplus2 = m_pcFrameData[0].m_qp + 2; |
---|
2235 | |
---|
2236 | for(Int idx = 1; idx <= m_sizeGOP; idx++) |
---|
2237 | { |
---|
2238 | if(m_pcFrameData[idx].m_isReferenced) |
---|
2239 | { |
---|
2240 | finalQP += m_pcFrameData[idx].m_qp; |
---|
2241 | numofReferenced++; |
---|
2242 | } |
---|
2243 | } |
---|
2244 | |
---|
2245 | finalQP = (numofReferenced == 0) ? m_pcFrameData[0].m_qp : ((finalQP + (1<<(numofReferenced>>1)))/numofReferenced); |
---|
2246 | finalQP = max( lastQPminus2, min( lastQPplus2, finalQP)); |
---|
2247 | |
---|
2248 | Double costAvgFrameBits = m_remainingBitsInGOP/(Double)m_sizeGOP; |
---|
2249 | Int bufLevel = m_occupancyVB + m_initialOVB; |
---|
2250 | |
---|
2251 | if(abs(bufLevel) > costAvgFrameBits) |
---|
2252 | { |
---|
2253 | if(bufLevel < 0) |
---|
2254 | { |
---|
2255 | finalQP -= 2; |
---|
2256 | } |
---|
2257 | else |
---|
2258 | { |
---|
2259 | finalQP += 2; |
---|
2260 | } |
---|
2261 | } |
---|
2262 | m_indexRefFrame++; |
---|
2263 | } |
---|
2264 | finalQP = max(MIN_QP, min(MAX_QP, finalQP)); |
---|
2265 | |
---|
2266 | for(Int indexLCU = 0 ; indexLCU < m_numUnitInFrame; indexLCU++) |
---|
2267 | { |
---|
2268 | m_pcLCUData[indexLCU].m_qp = finalQP; |
---|
2269 | } |
---|
2270 | |
---|
2271 | pcFrameData->m_isReferenced = isReferenced; |
---|
2272 | pcFrameData->m_qp = finalQP; |
---|
2273 | |
---|
2274 | return finalQP; |
---|
2275 | } |
---|
2276 | |
---|
2277 | Bool TEncRateCtrl::calculateUnitQP () |
---|
2278 | { |
---|
2279 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
---|
2280 | { |
---|
2281 | return false; |
---|
2282 | } |
---|
2283 | Int upperQPBound, lowerQPBound, finalQP; |
---|
2284 | Int colQP = m_pcLCUData[m_indexLCU].m_qp; |
---|
2285 | Double colMAD = m_pcLCUData[m_indexLCU].m_costMAD; |
---|
2286 | Double budgetInUnit = m_pcLCUData[m_indexLCU].m_pixels*m_costAvgbpp; |
---|
2287 | |
---|
2288 | |
---|
2289 | Int targetBitsOccupancy = (Int)(budgetInUnit - (m_occupancyVBInFrame/(m_numUnitInFrame-m_indexUnit))); |
---|
2290 | Int targetBitsLeftBudget= (Int)((m_remainingBitsInFrame*m_pcLCUData[m_indexLCU].m_pixels)/(Double)(m_numOfPixels-m_codedPixels)); |
---|
2291 | Int targetBits = (targetBitsLeftBudget>>1) + (targetBitsOccupancy>>1); |
---|
2292 | |
---|
2293 | |
---|
2294 | if( m_indexLCU >= m_sourceWidthInLCU) |
---|
2295 | { |
---|
2296 | upperQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) + MAX_DELTA_QP; |
---|
2297 | lowerQPBound = ( (m_pcLCUData[m_indexLCU-1].m_qp + m_pcLCUData[m_indexLCU - m_sourceWidthInLCU].m_qp)>>1) - MAX_DELTA_QP; |
---|
2298 | } |
---|
2299 | else |
---|
2300 | { |
---|
2301 | upperQPBound = m_pcLCUData[m_indexLCU-1].m_qp + MAX_DELTA_QP; |
---|
2302 | lowerQPBound = m_pcLCUData[m_indexLCU-1].m_qp - MAX_DELTA_QP; |
---|
2303 | } |
---|
2304 | |
---|
2305 | if(targetBits < 0) |
---|
2306 | { |
---|
2307 | finalQP = m_pcLCUData[m_indexLCU-1].m_qp + 1; |
---|
2308 | } |
---|
2309 | else |
---|
2310 | { |
---|
2311 | finalQP = m_cPixelURQQuadraticModel.getQP(colQP, targetBits, m_pcLCUData[m_indexLCU].m_pixels, colMAD); |
---|
2312 | } |
---|
2313 | |
---|
2314 | finalQP = max(lowerQPBound, min(upperQPBound, finalQP)); |
---|
2315 | m_pcLCUData[m_indexLCU].m_qp = max(MIN_QP, min(MAX_QP, finalQP)); |
---|
2316 | |
---|
2317 | return true; |
---|
2318 | } |
---|
2319 | |
---|
2320 | Void TEncRateCtrl::updateRCGOPStatus() |
---|
2321 | { |
---|
2322 | m_remainingBitsInGOP = ((m_currBitrate/m_frameRate)*m_sizeGOP) - m_occupancyVB; |
---|
2323 | |
---|
2324 | FrameData cFrameData = m_pcFrameData[m_sizeGOP]; |
---|
2325 | initFrameData(); |
---|
2326 | |
---|
2327 | m_pcFrameData[0] = cFrameData; |
---|
2328 | m_indexGOP++; |
---|
2329 | m_indexFrame = 0; |
---|
2330 | m_indexRefFrame = 0; |
---|
2331 | m_indexNonRefFrame = 0; |
---|
2332 | } |
---|
2333 | |
---|
2334 | Void TEncRateCtrl::updataRCFrameStatus(Int frameBits, SliceType eSliceType) |
---|
2335 | { |
---|
2336 | FrameData* pcFrameData = &m_pcFrameData[m_indexPOCInGOP]; |
---|
2337 | Int occupancyBits; |
---|
2338 | Double adjustmentBits; |
---|
2339 | |
---|
2340 | m_remainingBitsInGOP = m_remainingBitsInGOP + ( ((m_currBitrate-m_prevBitrate)/m_frameRate)*(m_sizeGOP-m_indexFrame) ) - frameBits; |
---|
2341 | occupancyBits = (Int)((Double)frameBits - (m_currBitrate/(Double)m_frameRate)); |
---|
2342 | |
---|
2343 | if( (occupancyBits < 0) && (m_initialOVB > 0) ) |
---|
2344 | { |
---|
2345 | adjustmentBits = xAdjustmentBits(occupancyBits, m_initialOVB ); |
---|
2346 | |
---|
2347 | if(m_initialOVB < 0) |
---|
2348 | { |
---|
2349 | adjustmentBits = m_initialOVB; |
---|
2350 | occupancyBits += (Int)adjustmentBits; |
---|
2351 | m_initialOVB = 0; |
---|
2352 | } |
---|
2353 | } |
---|
2354 | else if( (occupancyBits > 0) && (m_initialOVB < 0) ) |
---|
2355 | { |
---|
2356 | adjustmentBits = xAdjustmentBits(m_initialOVB, occupancyBits ); |
---|
2357 | |
---|
2358 | if(occupancyBits < 0) |
---|
2359 | { |
---|
2360 | adjustmentBits = occupancyBits; |
---|
2361 | m_initialOVB += (Int)adjustmentBits; |
---|
2362 | occupancyBits = 0; |
---|
2363 | } |
---|
2364 | } |
---|
2365 | |
---|
2366 | if(m_indexGOP == 0) |
---|
2367 | { |
---|
2368 | m_initialOVB = occupancyBits; |
---|
2369 | } |
---|
2370 | else |
---|
2371 | { |
---|
2372 | m_occupancyVB= m_occupancyVB + occupancyBits; |
---|
2373 | } |
---|
2374 | |
---|
2375 | if(pcFrameData->m_isReferenced) |
---|
2376 | { |
---|
2377 | m_costRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costRefAvgWeighting)/8.0); |
---|
2378 | |
---|
2379 | if(m_indexFrame == 0) |
---|
2380 | { |
---|
2381 | m_initialTBL = m_targetBufLevel = (frameBits - (m_currBitrate/m_frameRate)); |
---|
2382 | } |
---|
2383 | else |
---|
2384 | { |
---|
2385 | Int distance = (m_costNonRefAvgWeighting == 0) ? 0 : 1; |
---|
2386 | m_targetBufLevel = m_targetBufLevel |
---|
2387 | - (m_initialTBL/(m_refFrameNum-1)) |
---|
2388 | + (Int)((m_costRefAvgWeighting*(distance+1)*m_currBitrate)/(m_frameRate*(m_costRefAvgWeighting+(m_costNonRefAvgWeighting*distance)))) |
---|
2389 | - (m_currBitrate/m_frameRate); |
---|
2390 | } |
---|
2391 | |
---|
2392 | if(m_cMADLinearModel.IsUpdateAvailable()) |
---|
2393 | { |
---|
2394 | m_cMADLinearModel.updateMADLiearModel(); |
---|
2395 | } |
---|
2396 | |
---|
2397 | if(eSliceType != I_SLICE && |
---|
2398 | m_cPixelURQQuadraticModel.checkUpdateAvailable(pcFrameData->m_qp)) |
---|
2399 | { |
---|
2400 | m_cPixelURQQuadraticModel.updatePixelBasedURQQuadraticModel(pcFrameData->m_qp, pcFrameData->m_bits, m_numOfPixels, pcFrameData->m_costMAD); |
---|
2401 | } |
---|
2402 | } |
---|
2403 | else |
---|
2404 | { |
---|
2405 | m_costNonRefAvgWeighting = ((pcFrameData->m_bits*pcFrameData->m_qp)/8.0) + (7.0*(m_costNonRefAvgWeighting)/8.0); |
---|
2406 | } |
---|
2407 | |
---|
2408 | m_indexFrame++; |
---|
2409 | m_indexLCU = 0; |
---|
2410 | m_indexUnit = 0; |
---|
2411 | m_occupancyVBInFrame = 0; |
---|
2412 | m_remainingBitsInFrame = 0; |
---|
2413 | m_codedPixels = 0; |
---|
2414 | m_activeUnitLevelOn = false; |
---|
2415 | m_costAvgbpp = 0.0; |
---|
2416 | } |
---|
2417 | Void TEncRateCtrl::updataRCUnitStatus () |
---|
2418 | { |
---|
2419 | if(!m_activeUnitLevelOn || m_indexLCU == 0) |
---|
2420 | { |
---|
2421 | return; |
---|
2422 | } |
---|
2423 | |
---|
2424 | m_codedPixels += m_pcLCUData[m_indexLCU-1].m_pixels; |
---|
2425 | m_remainingBitsInFrame = m_remainingBitsInFrame - m_pcLCUData[m_indexLCU-1].m_bits; |
---|
2426 | m_occupancyVBInFrame = (Int)(m_occupancyVBInFrame + m_pcLCUData[m_indexLCU-1].m_bits - m_pcLCUData[m_indexLCU-1].m_pixels*m_costAvgbpp); |
---|
2427 | |
---|
2428 | if( m_cPixelURQQuadraticModel.checkUpdateAvailable(m_pcLCUData[m_indexLCU-1].m_qp) ) |
---|
2429 | { |
---|
2430 | 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); |
---|
2431 | } |
---|
2432 | |
---|
2433 | m_indexUnit++; |
---|
2434 | } |
---|
2435 | |
---|
2436 | Void TEncRateCtrl::updateFrameData(UInt64 actualFrameBits) |
---|
2437 | { |
---|
2438 | Double costMAD = 0.0; |
---|
2439 | |
---|
2440 | for(Int i = 0; i < m_numUnitInFrame; i++) |
---|
2441 | { |
---|
2442 | costMAD += m_pcLCUData[i].m_costMAD; |
---|
2443 | } |
---|
2444 | |
---|
2445 | m_pcFrameData[m_indexPOCInGOP].m_costMAD = (costMAD/(Double)m_numUnitInFrame); |
---|
2446 | m_pcFrameData[m_indexPOCInGOP].m_bits = (Int)actualFrameBits; |
---|
2447 | |
---|
2448 | if(m_pcFrameData[m_indexPOCInGOP].m_isReferenced) |
---|
2449 | { |
---|
2450 | m_indexPrevPOCInGOP = m_indexPOCInGOP; |
---|
2451 | m_cMADLinearModel.updateMADHistory(m_pcFrameData[m_indexPOCInGOP].m_costMAD); |
---|
2452 | } |
---|
2453 | } |
---|
2454 | |
---|
2455 | Void TEncRateCtrl::updateLCUData(TComDataCU* pcCU, UInt64 actualLCUBits, Int qp) |
---|
2456 | { |
---|
2457 | Int x, y; |
---|
2458 | Double costMAD = 0.0; |
---|
2459 | |
---|
2460 | Pel* pOrg = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
2461 | Pel* pRec = pcCU->getPic()->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0); |
---|
2462 | Int stride = pcCU->getPic()->getStride(); |
---|
2463 | |
---|
2464 | Int width = m_pcLCUData[m_indexLCU].m_widthInPixel; |
---|
2465 | Int height = m_pcLCUData[m_indexLCU].m_heightInPixel; |
---|
2466 | |
---|
2467 | for( y = 0; y < height; y++ ) |
---|
2468 | { |
---|
2469 | for( x = 0; x < width; x++ ) |
---|
2470 | { |
---|
2471 | costMAD += abs( pOrg[x] - pRec[x] ); |
---|
2472 | } |
---|
2473 | pOrg += stride; |
---|
2474 | pRec += stride; |
---|
2475 | } |
---|
2476 | m_pcLCUData[m_indexLCU ].m_qp = qp; |
---|
2477 | m_pcLCUData[m_indexLCU ].m_costMAD = (costMAD /(Double)(width*height)); |
---|
2478 | m_pcLCUData[m_indexLCU++].m_bits = (Int)actualLCUBits; |
---|
2479 | } |
---|
2480 | |
---|
2481 | |
---|
2482 | #if KWU_RC_MADPRED_E0227 |
---|
2483 | Void TEncRateCtrl::updateLCUDataEnhancedView(TComDataCU* pcCU, UInt64 uiBits, Int qp, Double basePos, Double curPos, Double focalLen, Double znear, Double zfar, Int direction) |
---|
2484 | { |
---|
2485 | Int x, y; |
---|
2486 | Double dMAD = 0.0; |
---|
2487 | Int Sum = 0; |
---|
2488 | Double SAD = 0.0; |
---|
2489 | |
---|
2490 | Pel* pOrg = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
2491 | Pel* pRec = pcCU->getSlice()->getIvPic(false, 0)->getPicYuvRec()->getLumaAddr(pcCU->getAddr(), 0); |
---|
2492 | Pel* pDep = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
2493 | Int iStride = pcCU->getSlice()->getIvPic(true, pcCU->getSlice()->getViewIndex())->getPicYuvOrg()->getStride(); |
---|
2494 | |
---|
2495 | Int width = m_pcLCUData[m_indexLCU].m_widthInPixel; |
---|
2496 | Int height = m_pcLCUData[m_indexLCU].m_heightInPixel; |
---|
2497 | |
---|
2498 | for( y = 0 ; y < pcCU->getSlice()->getSPS()->getMaxCUHeight() ; y+=8) |
---|
2499 | { |
---|
2500 | for( x = 0 ; x < pcCU->getSlice()->getSPS()->getMaxCUWidth() ; x+=8) |
---|
2501 | { |
---|
2502 | Sum += pDep[x]; |
---|
2503 | } |
---|
2504 | pDep += iStride; |
---|
2505 | } |
---|
2506 | |
---|
2507 | Double AvgDepth = (Double)Sum/((pcCU->getSlice()->getSPS()->getMaxCUHeight()/8)*(pcCU->getSlice()->getSPS()->getMaxCUWidth()/8)); |
---|
2508 | Double fL = focalLen * abs( basePos - curPos ); |
---|
2509 | Double z = abs( 1.0 / znear - 1.0 / zfar ) * ((Double)(AvgDepth) / (( 1 << g_bitDepthY ) - 1) ) + abs(1.0 / zfar); |
---|
2510 | Int disparity = (Int)(direction*fL * z); |
---|
2511 | Int shift = DISTORTION_PRECISION_ADJUSTMENT(g_bitDepthY-8); |
---|
2512 | Int disp = disparity; |
---|
2513 | |
---|
2514 | for( y = 0; y < height; y++ ) |
---|
2515 | { |
---|
2516 | for( x = 0; x < width; x++ ) |
---|
2517 | { |
---|
2518 | SAD += abs( pOrg[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] |
---|
2519 | - pRec[Clip3(0, (Int)(pcCU->getPic()->getPicYuvOrg()->getWidth() - pcCU->getSlice()->getSPS()->getMaxCUWidth()), x + disp)] )>>shift; |
---|
2520 | } |
---|
2521 | pOrg += iStride; |
---|
2522 | pRec += iStride; |
---|
2523 | } |
---|
2524 | m_pcLCUData[m_indexLCU].m_qp = qp; |
---|
2525 | m_pcLCUData[m_indexLCU].m_costMAD = (SAD /(Double)(width*height)); |
---|
2526 | m_pcLCUData[m_indexLCU].m_bits = (Int)uiBits; |
---|
2527 | } |
---|
2528 | #endif |
---|
2529 | |
---|
2530 | |
---|
2531 | Double TEncRateCtrl::xAdjustmentBits(Int& reductionBits, Int& compensationBits) |
---|
2532 | { |
---|
2533 | Double adjustment = ADJUSTMENT_FACTOR*reductionBits; |
---|
2534 | reductionBits -= (Int)adjustment; |
---|
2535 | compensationBits += (Int)adjustment; |
---|
2536 | |
---|
2537 | return adjustment; |
---|
2538 | } |
---|
2539 | |
---|
2540 | #endif |
---|
2541 | |
---|