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