HEVC Test Model (HM)  HM-16.3
TComTrQuant.cpp
Go to the documentation of this file.
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 
38 #include <stdlib.h>
39 #include <math.h>
40 #include <limits>
41 #include <memory.h>
42 #include "TComTrQuant.h"
43 #include "TComPic.h"
44 #include "ContextTables.h"
45 #include "TComTU.h"
46 #include "Debug.h"
47 
48 typedef struct
49 {
51  Double d64CodedLevelandDist; // distortion and level cost only
52  Double d64UncodedDist; // all zero coded block distortion
56 
59 
60 // ====================================================================================================================
61 // Constants
62 // ====================================================================================================================
63 
64 #define RDOQ_CHROMA 1
65 
66 
67 // ====================================================================================================================
68 // QpParam constructor
69 // ====================================================================================================================
70 
72  const ChannelType chType,
73  const Int qpBdOffset,
74  const Int chromaQPOffset,
75  const ChromaFormat chFmt )
76 {
77  Int baseQp;
78 
79  if(isLuma(chType))
80  {
81  baseQp = qpy + qpBdOffset;
82  }
83  else
84  {
85  baseQp = Clip3( -qpBdOffset, (chromaQPMappingTableSize - 1), qpy + chromaQPOffset );
86 
87  if(baseQp < 0)
88  {
89  baseQp = baseQp + qpBdOffset;
90  }
91  else
92  {
93  baseQp = getScaledChromaQP(baseQp, chFmt) + qpBdOffset;
94  }
95  }
96 
97  Qp =baseQp;
98  per=baseQp/6;
99  rem=baseQp%6;
100 }
101 
102 QpParam::QpParam(const TComDataCU &cu, const ComponentID compID)
103 {
104  Int chromaQpOffset = 0;
105 
106  if (isChroma(compID))
107  {
108  chromaQpOffset += cu.getSlice()->getPPS()->getQpOffset(compID);
109  chromaQpOffset += cu.getSlice()->getSliceChromaQpDelta(compID);
110 
111  chromaQpOffset += cu.getSlice()->getPPS()->getChromaQpAdjTableAt(cu.getChromaQpAdj(0)).u.offset[Int(compID)-1];
112  }
113 
114  *this = QpParam(cu.getQP( 0 ),
115  toChannelType(compID),
116  cu.getSlice()->getSPS()->getQpBDOffset(toChannelType(compID)),
117  chromaQpOffset,
118  cu.getPic()->getChromaFormat());
119 }
120 
121 
122 // ====================================================================================================================
123 // TComTrQuant class member functions
124 // ====================================================================================================================
125 
127 {
128  // allocate temporary buffers
130 
131  // allocate bit estimation class (for RDOQ)
133  initScalingList();
134 }
135 
137 {
138  // delete temporary buffers
139  if ( m_plTempCoeff )
140  {
141  delete [] m_plTempCoeff;
143  }
144 
145  // delete bit estimation class
146  if ( m_pcEstBitsSbac )
147  {
148  delete m_pcEstBitsSbac;
149  }
151 }
152 
153 #if ADAPTIVE_QP_SELECTION
155 {
156  // NOTE: does this work with negative QPs or when some blocks are transquant-bypass enabled?
157 
158  Int qpBase = pcSlice->getSliceQpBase();
159  Int sliceQpused = pcSlice->getSliceQp();
160  Int sliceQpnext;
161  Double alpha = qpBase < 17 ? 0.5 : 1;
162 
163  Int cnt=0;
164  for(Int u=1; u<=LEVEL_RANGE; u++)
165  {
166  cnt += m_sliceNsamples[u] ;
167  }
168 
169  if( !m_useRDOQ )
170  {
171  sliceQpused = qpBase;
172  alpha = 0.5;
173  }
174 
175  if( cnt > 120 )
176  {
177  Double sum = 0;
178  Int k = 0;
179  for(Int u=1; u<LEVEL_RANGE; u++)
180  {
181  sum += u*m_sliceSumC[u];
182  k += u*u*m_sliceNsamples[u];
183  }
184 
185  Int v;
186  Double q[MAX_QP+1] ;
187  for(v=0; v<=MAX_QP; v++)
188  {
189  q[v] = (Double)(g_invQuantScales[v%6] * (1<<(v/6)))/64 ;
190  }
191 
192  Double qnext = sum/k * q[sliceQpused] / (1<<ARL_C_PRECISION);
193 
194  for(v=0; v<MAX_QP; v++)
195  {
196  if(qnext < alpha * q[v] + (1 - alpha) * q[v+1] )
197  {
198  break;
199  }
200  }
201  sliceQpnext = Clip3(sliceQpused - 3, sliceQpused + 3, v);
202  }
203  else
204  {
205  sliceQpnext = sliceQpused;
206  }
207 
208  m_qpDelta[qpBase] = sliceQpnext - qpBase;
209 }
210 
212 {
213  for(Int qp=0; qp<=MAX_QP; qp++)
214  {
215  m_qpDelta[qp] = qp < 17 ? 0 : 1;
216  }
217 }
218 
220 {
221  memset(m_sliceSumC, 0, sizeof(Double)*(LEVEL_RANGE+1));
222  memset(m_sliceNsamples, 0, sizeof(Int)*(LEVEL_RANGE+1));
223 }
224 #endif
225 
226 
227 
228 #if MATRIX_MULT
229 
236 Void xTr(Int bitDepth, Pel *block, TCoeff *coeff, UInt uiStride, UInt uiTrSize, Bool useDST, const Int maxTrDynamicRange)
237 {
238  UInt i,j,k;
239  TCoeff iSum;
241  const TMatrixCoeff *iT;
242  UInt uiLog2TrSize = g_aucConvertToBit[ uiTrSize ] + 2;
243 
244  if (uiTrSize==4)
245  {
246  iT = (useDST ? g_as_DST_MAT_4[TRANSFORM_FORWARD][0] : g_aiT4[TRANSFORM_FORWARD][0]);
247  }
248  else if (uiTrSize==8)
249  {
250  iT = g_aiT8[TRANSFORM_FORWARD][0];
251  }
252  else if (uiTrSize==16)
253  {
254  iT = g_aiT16[TRANSFORM_FORWARD][0];
255  }
256  else if (uiTrSize==32)
257  {
258  iT = g_aiT32[TRANSFORM_FORWARD][0];
259  }
260  else
261  {
262  assert(0);
263  }
264 
265  static const Int TRANSFORM_MATRIX_SHIFT = g_transformMatrixShift[TRANSFORM_FORWARD];
266 
267  const Int shift_1st = (uiLog2TrSize + bitDepth + TRANSFORM_MATRIX_SHIFT) - maxTrDynamicRange;
268  const Int shift_2nd = uiLog2TrSize + TRANSFORM_MATRIX_SHIFT;
269  const Int add_1st = (shift_1st>0) ? (1<<(shift_1st-1)) : 0;
270  const Int add_2nd = 1<<(shift_2nd-1);
271 
272  /* Horizontal transform */
273 
274  for (i=0; i<uiTrSize; i++)
275  {
276  for (j=0; j<uiTrSize; j++)
277  {
278  iSum = 0;
279  for (k=0; k<uiTrSize; k++)
280  {
281  iSum += iT[i*uiTrSize+k]*block[j*uiStride+k];
282  }
283  tmp[i*uiTrSize+j] = (iSum + add_1st)>>shift_1st;
284  }
285  }
286 
287  /* Vertical transform */
288  for (i=0; i<uiTrSize; i++)
289  {
290  for (j=0; j<uiTrSize; j++)
291  {
292  iSum = 0;
293  for (k=0; k<uiTrSize; k++)
294  {
295  iSum += iT[i*uiTrSize+k]*tmp[j*uiTrSize+k];
296  }
297  coeff[i*uiTrSize+j] = (iSum + add_2nd)>>shift_2nd;
298  }
299  }
300 }
301 
309 Void xITr(Int bitDepth, TCoeff *coeff, Pel *block, UInt uiStride, UInt uiTrSize, Bool useDST, const Int maxTrDynamicRange)
310 {
311  UInt i,j,k;
312  TCoeff iSum;
314  const TMatrixCoeff *iT;
315 
316  if (uiTrSize==4)
317  {
318  iT = (useDST ? g_as_DST_MAT_4[TRANSFORM_INVERSE][0] : g_aiT4[TRANSFORM_INVERSE][0]);
319  }
320  else if (uiTrSize==8)
321  {
322  iT = g_aiT8[TRANSFORM_INVERSE][0];
323  }
324  else if (uiTrSize==16)
325  {
326  iT = g_aiT16[TRANSFORM_INVERSE][0];
327  }
328  else if (uiTrSize==32)
329  {
330  iT = g_aiT32[TRANSFORM_INVERSE][0];
331  }
332  else
333  {
334  assert(0);
335  }
336 
337  static const Int TRANSFORM_MATRIX_SHIFT = g_transformMatrixShift[TRANSFORM_INVERSE];
338 
339  const Int shift_1st = TRANSFORM_MATRIX_SHIFT + 1; //1 has been added to shift_1st at the expense of shift_2nd
340  const Int shift_2nd = (TRANSFORM_MATRIX_SHIFT + maxTrDynamicRange - 1) - bitDepth;
341  const TCoeff clipMinimum = -(1 << maxTrDynamicRange);
342  const TCoeff clipMaximum = (1 << maxTrDynamicRange) - 1;
343  assert(shift_2nd>=0);
344  const Int add_1st = 1<<(shift_1st-1);
345  const Int add_2nd = (shift_2nd>0) ? (1<<(shift_2nd-1)) : 0;
346 
347  /* Horizontal transform */
348  for (i=0; i<uiTrSize; i++)
349  {
350  for (j=0; j<uiTrSize; j++)
351  {
352  iSum = 0;
353  for (k=0; k<uiTrSize; k++)
354  {
355  iSum += iT[k*uiTrSize+i]*coeff[k*uiTrSize+j];
356  }
357 
358  // Clipping here is not in the standard, but is used to protect the "Pel" data type into which the inverse-transformed samples will be copied
359  tmp[i*uiTrSize+j] = Clip3<TCoeff>(clipMinimum, clipMaximum, (iSum + add_1st)>>shift_1st);
360  }
361  }
362 
363  /* Vertical transform */
364  for (i=0; i<uiTrSize; i++)
365  {
366  for (j=0; j<uiTrSize; j++)
367  {
368  iSum = 0;
369  for (k=0; k<uiTrSize; k++)
370  {
371  iSum += iT[k*uiTrSize+j]*tmp[i*uiTrSize+k];
372  }
373 
374  block[i*uiStride+j] = Clip3<TCoeff>(std::numeric_limits<Pel>::min(), std::numeric_limits<Pel>::max(), (iSum + add_2nd)>>shift_2nd);
375  }
376  }
377 }
378 
379 #endif //MATRIX_MULT
380 
381 
388 Void partialButterfly4(TCoeff *src, TCoeff *dst, Int shift, Int line)
389 {
390  Int j;
391  TCoeff E[2],O[2];
392  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
393 
394  for (j=0; j<line; j++)
395  {
396  /* E and O */
397  E[0] = src[0] + src[3];
398  O[0] = src[0] - src[3];
399  E[1] = src[1] + src[2];
400  O[1] = src[1] - src[2];
401 
402  dst[0] = (g_aiT4[TRANSFORM_FORWARD][0][0]*E[0] + g_aiT4[TRANSFORM_FORWARD][0][1]*E[1] + add)>>shift;
403  dst[2*line] = (g_aiT4[TRANSFORM_FORWARD][2][0]*E[0] + g_aiT4[TRANSFORM_FORWARD][2][1]*E[1] + add)>>shift;
404  dst[line] = (g_aiT4[TRANSFORM_FORWARD][1][0]*O[0] + g_aiT4[TRANSFORM_FORWARD][1][1]*O[1] + add)>>shift;
405  dst[3*line] = (g_aiT4[TRANSFORM_FORWARD][3][0]*O[0] + g_aiT4[TRANSFORM_FORWARD][3][1]*O[1] + add)>>shift;
406 
407  src += 4;
408  dst ++;
409  }
410 }
411 
412 // Fast DST Algorithm. Full matrix multiplication for DST and Fast DST algorithm
413 // give identical results
414 Void fastForwardDst(TCoeff *block, TCoeff *coeff, Int shift) // input block, output coeff
415 {
416  Int i;
417  TCoeff c[4];
418  TCoeff rnd_factor = (shift > 0) ? (1<<(shift-1)) : 0;
419  for (i=0; i<4; i++)
420  {
421  // Intermediate Variables
422  c[0] = block[4*i+0];
423  c[1] = block[4*i+1];
424  c[2] = block[4*i+2];
425  c[3] = block[4*i+3];
426 
427  for (Int row = 0; row < 4; row++)
428  {
429  TCoeff result = 0;
430  for (Int column = 0; column < 4; column++)
431  {
432  result += c[column] * g_as_DST_MAT_4[TRANSFORM_FORWARD][row][column]; // use the defined matrix, rather than hard-wired numbers
433  }
434 
435  coeff[(row * 4) + i] = rightShift((result + rnd_factor), shift);
436  }
437  }
438 }
439 
440 Void fastInverseDst(TCoeff *tmp, TCoeff *block, Int shift, const TCoeff outputMinimum, const TCoeff outputMaximum) // input tmp, output block
441 {
442  Int i;
443  TCoeff c[4];
444  TCoeff rnd_factor = (shift > 0) ? (1<<(shift-1)) : 0;
445  for (i=0; i<4; i++)
446  {
447  // Intermediate Variables
448  c[0] = tmp[ i];
449  c[1] = tmp[4 +i];
450  c[2] = tmp[8 +i];
451  c[3] = tmp[12+i];
452 
453  for (Int column = 0; column < 4; column++)
454  {
455  TCoeff &result = block[(i * 4) + column];
456 
457  result = 0;
458  for (Int row = 0; row < 4; row++)
459  {
460  result += c[row] * g_as_DST_MAT_4[TRANSFORM_INVERSE][row][column]; // use the defined matrix, rather than hard-wired numbers
461  }
462 
463  result = Clip3( outputMinimum, outputMaximum, rightShift((result + rnd_factor), shift));
464  }
465  }
466 }
467 
476 Void partialButterflyInverse4(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
477 {
478  Int j;
479  TCoeff E[2],O[2];
480  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
481 
482  for (j=0; j<line; j++)
483  {
484  /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
485  O[0] = g_aiT4[TRANSFORM_INVERSE][1][0]*src[line] + g_aiT4[TRANSFORM_INVERSE][3][0]*src[3*line];
486  O[1] = g_aiT4[TRANSFORM_INVERSE][1][1]*src[line] + g_aiT4[TRANSFORM_INVERSE][3][1]*src[3*line];
487  E[0] = g_aiT4[TRANSFORM_INVERSE][0][0]*src[0] + g_aiT4[TRANSFORM_INVERSE][2][0]*src[2*line];
488  E[1] = g_aiT4[TRANSFORM_INVERSE][0][1]*src[0] + g_aiT4[TRANSFORM_INVERSE][2][1]*src[2*line];
489 
490  /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
491  dst[0] = Clip3( outputMinimum, outputMaximum, (E[0] + O[0] + add)>>shift );
492  dst[1] = Clip3( outputMinimum, outputMaximum, (E[1] + O[1] + add)>>shift );
493  dst[2] = Clip3( outputMinimum, outputMaximum, (E[1] - O[1] + add)>>shift );
494  dst[3] = Clip3( outputMinimum, outputMaximum, (E[0] - O[0] + add)>>shift );
495 
496  src ++;
497  dst += 4;
498  }
499 }
500 
507 Void partialButterfly8(TCoeff *src, TCoeff *dst, Int shift, Int line)
508 {
509  Int j,k;
510  TCoeff E[4],O[4];
511  TCoeff EE[2],EO[2];
512  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
513 
514  for (j=0; j<line; j++)
515  {
516  /* E and O*/
517  for (k=0;k<4;k++)
518  {
519  E[k] = src[k] + src[7-k];
520  O[k] = src[k] - src[7-k];
521  }
522  /* EE and EO */
523  EE[0] = E[0] + E[3];
524  EO[0] = E[0] - E[3];
525  EE[1] = E[1] + E[2];
526  EO[1] = E[1] - E[2];
527 
528  dst[0] = (g_aiT8[TRANSFORM_FORWARD][0][0]*EE[0] + g_aiT8[TRANSFORM_FORWARD][0][1]*EE[1] + add)>>shift;
529  dst[4*line] = (g_aiT8[TRANSFORM_FORWARD][4][0]*EE[0] + g_aiT8[TRANSFORM_FORWARD][4][1]*EE[1] + add)>>shift;
530  dst[2*line] = (g_aiT8[TRANSFORM_FORWARD][2][0]*EO[0] + g_aiT8[TRANSFORM_FORWARD][2][1]*EO[1] + add)>>shift;
531  dst[6*line] = (g_aiT8[TRANSFORM_FORWARD][6][0]*EO[0] + g_aiT8[TRANSFORM_FORWARD][6][1]*EO[1] + add)>>shift;
532 
533  dst[line] = (g_aiT8[TRANSFORM_FORWARD][1][0]*O[0] + g_aiT8[TRANSFORM_FORWARD][1][1]*O[1] + g_aiT8[TRANSFORM_FORWARD][1][2]*O[2] + g_aiT8[TRANSFORM_FORWARD][1][3]*O[3] + add)>>shift;
534  dst[3*line] = (g_aiT8[TRANSFORM_FORWARD][3][0]*O[0] + g_aiT8[TRANSFORM_FORWARD][3][1]*O[1] + g_aiT8[TRANSFORM_FORWARD][3][2]*O[2] + g_aiT8[TRANSFORM_FORWARD][3][3]*O[3] + add)>>shift;
535  dst[5*line] = (g_aiT8[TRANSFORM_FORWARD][5][0]*O[0] + g_aiT8[TRANSFORM_FORWARD][5][1]*O[1] + g_aiT8[TRANSFORM_FORWARD][5][2]*O[2] + g_aiT8[TRANSFORM_FORWARD][5][3]*O[3] + add)>>shift;
536  dst[7*line] = (g_aiT8[TRANSFORM_FORWARD][7][0]*O[0] + g_aiT8[TRANSFORM_FORWARD][7][1]*O[1] + g_aiT8[TRANSFORM_FORWARD][7][2]*O[2] + g_aiT8[TRANSFORM_FORWARD][7][3]*O[3] + add)>>shift;
537 
538  src += 8;
539  dst ++;
540  }
541 }
542 
551 Void partialButterflyInverse8(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
552 {
553  Int j,k;
554  TCoeff E[4],O[4];
555  TCoeff EE[2],EO[2];
556  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
557 
558  for (j=0; j<line; j++)
559  {
560  /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
561  for (k=0;k<4;k++)
562  {
563  O[k] = g_aiT8[TRANSFORM_INVERSE][ 1][k]*src[line] + g_aiT8[TRANSFORM_INVERSE][ 3][k]*src[3*line] +
564  g_aiT8[TRANSFORM_INVERSE][ 5][k]*src[5*line] + g_aiT8[TRANSFORM_INVERSE][ 7][k]*src[7*line];
565  }
566 
567  EO[0] = g_aiT8[TRANSFORM_INVERSE][2][0]*src[ 2*line ] + g_aiT8[TRANSFORM_INVERSE][6][0]*src[ 6*line ];
568  EO[1] = g_aiT8[TRANSFORM_INVERSE][2][1]*src[ 2*line ] + g_aiT8[TRANSFORM_INVERSE][6][1]*src[ 6*line ];
569  EE[0] = g_aiT8[TRANSFORM_INVERSE][0][0]*src[ 0 ] + g_aiT8[TRANSFORM_INVERSE][4][0]*src[ 4*line ];
570  EE[1] = g_aiT8[TRANSFORM_INVERSE][0][1]*src[ 0 ] + g_aiT8[TRANSFORM_INVERSE][4][1]*src[ 4*line ];
571 
572  /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
573  E[0] = EE[0] + EO[0];
574  E[3] = EE[0] - EO[0];
575  E[1] = EE[1] + EO[1];
576  E[2] = EE[1] - EO[1];
577  for (k=0;k<4;k++)
578  {
579  dst[ k ] = Clip3( outputMinimum, outputMaximum, (E[k] + O[k] + add)>>shift );
580  dst[ k+4 ] = Clip3( outputMinimum, outputMaximum, (E[3-k] - O[3-k] + add)>>shift );
581  }
582  src ++;
583  dst += 8;
584  }
585 }
586 
593 Void partialButterfly16(TCoeff *src, TCoeff *dst, Int shift, Int line)
594 {
595  Int j,k;
596  TCoeff E[8],O[8];
597  TCoeff EE[4],EO[4];
598  TCoeff EEE[2],EEO[2];
599  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
600 
601  for (j=0; j<line; j++)
602  {
603  /* E and O*/
604  for (k=0;k<8;k++)
605  {
606  E[k] = src[k] + src[15-k];
607  O[k] = src[k] - src[15-k];
608  }
609  /* EE and EO */
610  for (k=0;k<4;k++)
611  {
612  EE[k] = E[k] + E[7-k];
613  EO[k] = E[k] - E[7-k];
614  }
615  /* EEE and EEO */
616  EEE[0] = EE[0] + EE[3];
617  EEO[0] = EE[0] - EE[3];
618  EEE[1] = EE[1] + EE[2];
619  EEO[1] = EE[1] - EE[2];
620 
621  dst[ 0 ] = (g_aiT16[TRANSFORM_FORWARD][ 0][0]*EEE[0] + g_aiT16[TRANSFORM_FORWARD][ 0][1]*EEE[1] + add)>>shift;
622  dst[ 8*line ] = (g_aiT16[TRANSFORM_FORWARD][ 8][0]*EEE[0] + g_aiT16[TRANSFORM_FORWARD][ 8][1]*EEE[1] + add)>>shift;
623  dst[ 4*line ] = (g_aiT16[TRANSFORM_FORWARD][ 4][0]*EEO[0] + g_aiT16[TRANSFORM_FORWARD][ 4][1]*EEO[1] + add)>>shift;
624  dst[ 12*line] = (g_aiT16[TRANSFORM_FORWARD][12][0]*EEO[0] + g_aiT16[TRANSFORM_FORWARD][12][1]*EEO[1] + add)>>shift;
625 
626  for (k=2;k<16;k+=4)
627  {
628  dst[ k*line ] = (g_aiT16[TRANSFORM_FORWARD][k][0]*EO[0] + g_aiT16[TRANSFORM_FORWARD][k][1]*EO[1] +
629  g_aiT16[TRANSFORM_FORWARD][k][2]*EO[2] + g_aiT16[TRANSFORM_FORWARD][k][3]*EO[3] + add)>>shift;
630  }
631 
632  for (k=1;k<16;k+=2)
633  {
634  dst[ k*line ] = (g_aiT16[TRANSFORM_FORWARD][k][0]*O[0] + g_aiT16[TRANSFORM_FORWARD][k][1]*O[1] +
635  g_aiT16[TRANSFORM_FORWARD][k][2]*O[2] + g_aiT16[TRANSFORM_FORWARD][k][3]*O[3] +
636  g_aiT16[TRANSFORM_FORWARD][k][4]*O[4] + g_aiT16[TRANSFORM_FORWARD][k][5]*O[5] +
637  g_aiT16[TRANSFORM_FORWARD][k][6]*O[6] + g_aiT16[TRANSFORM_FORWARD][k][7]*O[7] + add)>>shift;
638  }
639 
640  src += 16;
641  dst ++;
642 
643  }
644 }
645 
654 Void partialButterflyInverse16(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
655 {
656  Int j,k;
657  TCoeff E[8],O[8];
658  TCoeff EE[4],EO[4];
659  TCoeff EEE[2],EEO[2];
660  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
661 
662  for (j=0; j<line; j++)
663  {
664  /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
665  for (k=0;k<8;k++)
666  {
667  O[k] = g_aiT16[TRANSFORM_INVERSE][ 1][k]*src[ line] + g_aiT16[TRANSFORM_INVERSE][ 3][k]*src[ 3*line] +
668  g_aiT16[TRANSFORM_INVERSE][ 5][k]*src[ 5*line] + g_aiT16[TRANSFORM_INVERSE][ 7][k]*src[ 7*line] +
669  g_aiT16[TRANSFORM_INVERSE][ 9][k]*src[ 9*line] + g_aiT16[TRANSFORM_INVERSE][11][k]*src[11*line] +
670  g_aiT16[TRANSFORM_INVERSE][13][k]*src[13*line] + g_aiT16[TRANSFORM_INVERSE][15][k]*src[15*line];
671  }
672  for (k=0;k<4;k++)
673  {
674  EO[k] = g_aiT16[TRANSFORM_INVERSE][ 2][k]*src[ 2*line] + g_aiT16[TRANSFORM_INVERSE][ 6][k]*src[ 6*line] +
675  g_aiT16[TRANSFORM_INVERSE][10][k]*src[10*line] + g_aiT16[TRANSFORM_INVERSE][14][k]*src[14*line];
676  }
677  EEO[0] = g_aiT16[TRANSFORM_INVERSE][4][0]*src[ 4*line ] + g_aiT16[TRANSFORM_INVERSE][12][0]*src[ 12*line ];
678  EEE[0] = g_aiT16[TRANSFORM_INVERSE][0][0]*src[ 0 ] + g_aiT16[TRANSFORM_INVERSE][ 8][0]*src[ 8*line ];
679  EEO[1] = g_aiT16[TRANSFORM_INVERSE][4][1]*src[ 4*line ] + g_aiT16[TRANSFORM_INVERSE][12][1]*src[ 12*line ];
680  EEE[1] = g_aiT16[TRANSFORM_INVERSE][0][1]*src[ 0 ] + g_aiT16[TRANSFORM_INVERSE][ 8][1]*src[ 8*line ];
681 
682  /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
683  for (k=0;k<2;k++)
684  {
685  EE[k] = EEE[k] + EEO[k];
686  EE[k+2] = EEE[1-k] - EEO[1-k];
687  }
688  for (k=0;k<4;k++)
689  {
690  E[k] = EE[k] + EO[k];
691  E[k+4] = EE[3-k] - EO[3-k];
692  }
693  for (k=0;k<8;k++)
694  {
695  dst[k] = Clip3( outputMinimum, outputMaximum, (E[k] + O[k] + add)>>shift );
696  dst[k+8] = Clip3( outputMinimum, outputMaximum, (E[7-k] - O[7-k] + add)>>shift );
697  }
698  src ++;
699  dst += 16;
700  }
701 }
702 
709 Void partialButterfly32(TCoeff *src, TCoeff *dst, Int shift, Int line)
710 {
711  Int j,k;
712  TCoeff E[16],O[16];
713  TCoeff EE[8],EO[8];
714  TCoeff EEE[4],EEO[4];
715  TCoeff EEEE[2],EEEO[2];
716  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
717 
718  for (j=0; j<line; j++)
719  {
720  /* E and O*/
721  for (k=0;k<16;k++)
722  {
723  E[k] = src[k] + src[31-k];
724  O[k] = src[k] - src[31-k];
725  }
726  /* EE and EO */
727  for (k=0;k<8;k++)
728  {
729  EE[k] = E[k] + E[15-k];
730  EO[k] = E[k] - E[15-k];
731  }
732  /* EEE and EEO */
733  for (k=0;k<4;k++)
734  {
735  EEE[k] = EE[k] + EE[7-k];
736  EEO[k] = EE[k] - EE[7-k];
737  }
738  /* EEEE and EEEO */
739  EEEE[0] = EEE[0] + EEE[3];
740  EEEO[0] = EEE[0] - EEE[3];
741  EEEE[1] = EEE[1] + EEE[2];
742  EEEO[1] = EEE[1] - EEE[2];
743 
744  dst[ 0 ] = (g_aiT32[TRANSFORM_FORWARD][ 0][0]*EEEE[0] + g_aiT32[TRANSFORM_FORWARD][ 0][1]*EEEE[1] + add)>>shift;
745  dst[ 16*line ] = (g_aiT32[TRANSFORM_FORWARD][16][0]*EEEE[0] + g_aiT32[TRANSFORM_FORWARD][16][1]*EEEE[1] + add)>>shift;
746  dst[ 8*line ] = (g_aiT32[TRANSFORM_FORWARD][ 8][0]*EEEO[0] + g_aiT32[TRANSFORM_FORWARD][ 8][1]*EEEO[1] + add)>>shift;
747  dst[ 24*line ] = (g_aiT32[TRANSFORM_FORWARD][24][0]*EEEO[0] + g_aiT32[TRANSFORM_FORWARD][24][1]*EEEO[1] + add)>>shift;
748  for (k=4;k<32;k+=8)
749  {
750  dst[ k*line ] = (g_aiT32[TRANSFORM_FORWARD][k][0]*EEO[0] + g_aiT32[TRANSFORM_FORWARD][k][1]*EEO[1] +
751  g_aiT32[TRANSFORM_FORWARD][k][2]*EEO[2] + g_aiT32[TRANSFORM_FORWARD][k][3]*EEO[3] + add)>>shift;
752  }
753  for (k=2;k<32;k+=4)
754  {
755  dst[ k*line ] = (g_aiT32[TRANSFORM_FORWARD][k][0]*EO[0] + g_aiT32[TRANSFORM_FORWARD][k][1]*EO[1] +
756  g_aiT32[TRANSFORM_FORWARD][k][2]*EO[2] + g_aiT32[TRANSFORM_FORWARD][k][3]*EO[3] +
757  g_aiT32[TRANSFORM_FORWARD][k][4]*EO[4] + g_aiT32[TRANSFORM_FORWARD][k][5]*EO[5] +
758  g_aiT32[TRANSFORM_FORWARD][k][6]*EO[6] + g_aiT32[TRANSFORM_FORWARD][k][7]*EO[7] + add)>>shift;
759  }
760  for (k=1;k<32;k+=2)
761  {
762  dst[ k*line ] = (g_aiT32[TRANSFORM_FORWARD][k][ 0]*O[ 0] + g_aiT32[TRANSFORM_FORWARD][k][ 1]*O[ 1] +
763  g_aiT32[TRANSFORM_FORWARD][k][ 2]*O[ 2] + g_aiT32[TRANSFORM_FORWARD][k][ 3]*O[ 3] +
764  g_aiT32[TRANSFORM_FORWARD][k][ 4]*O[ 4] + g_aiT32[TRANSFORM_FORWARD][k][ 5]*O[ 5] +
765  g_aiT32[TRANSFORM_FORWARD][k][ 6]*O[ 6] + g_aiT32[TRANSFORM_FORWARD][k][ 7]*O[ 7] +
766  g_aiT32[TRANSFORM_FORWARD][k][ 8]*O[ 8] + g_aiT32[TRANSFORM_FORWARD][k][ 9]*O[ 9] +
767  g_aiT32[TRANSFORM_FORWARD][k][10]*O[10] + g_aiT32[TRANSFORM_FORWARD][k][11]*O[11] +
768  g_aiT32[TRANSFORM_FORWARD][k][12]*O[12] + g_aiT32[TRANSFORM_FORWARD][k][13]*O[13] +
769  g_aiT32[TRANSFORM_FORWARD][k][14]*O[14] + g_aiT32[TRANSFORM_FORWARD][k][15]*O[15] + add)>>shift;
770  }
771 
772  src += 32;
773  dst ++;
774  }
775 }
776 
785 Void partialButterflyInverse32(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
786 {
787  Int j,k;
788  TCoeff E[16],O[16];
789  TCoeff EE[8],EO[8];
790  TCoeff EEE[4],EEO[4];
791  TCoeff EEEE[2],EEEO[2];
792  TCoeff add = (shift > 0) ? (1<<(shift-1)) : 0;
793 
794  for (j=0; j<line; j++)
795  {
796  /* Utilizing symmetry properties to the maximum to minimize the number of multiplications */
797  for (k=0;k<16;k++)
798  {
799  O[k] = g_aiT32[TRANSFORM_INVERSE][ 1][k]*src[ line ] + g_aiT32[TRANSFORM_INVERSE][ 3][k]*src[ 3*line ] +
800  g_aiT32[TRANSFORM_INVERSE][ 5][k]*src[ 5*line ] + g_aiT32[TRANSFORM_INVERSE][ 7][k]*src[ 7*line ] +
801  g_aiT32[TRANSFORM_INVERSE][ 9][k]*src[ 9*line ] + g_aiT32[TRANSFORM_INVERSE][11][k]*src[ 11*line ] +
802  g_aiT32[TRANSFORM_INVERSE][13][k]*src[ 13*line ] + g_aiT32[TRANSFORM_INVERSE][15][k]*src[ 15*line ] +
803  g_aiT32[TRANSFORM_INVERSE][17][k]*src[ 17*line ] + g_aiT32[TRANSFORM_INVERSE][19][k]*src[ 19*line ] +
804  g_aiT32[TRANSFORM_INVERSE][21][k]*src[ 21*line ] + g_aiT32[TRANSFORM_INVERSE][23][k]*src[ 23*line ] +
805  g_aiT32[TRANSFORM_INVERSE][25][k]*src[ 25*line ] + g_aiT32[TRANSFORM_INVERSE][27][k]*src[ 27*line ] +
806  g_aiT32[TRANSFORM_INVERSE][29][k]*src[ 29*line ] + g_aiT32[TRANSFORM_INVERSE][31][k]*src[ 31*line ];
807  }
808  for (k=0;k<8;k++)
809  {
810  EO[k] = g_aiT32[TRANSFORM_INVERSE][ 2][k]*src[ 2*line ] + g_aiT32[TRANSFORM_INVERSE][ 6][k]*src[ 6*line ] +
811  g_aiT32[TRANSFORM_INVERSE][10][k]*src[ 10*line ] + g_aiT32[TRANSFORM_INVERSE][14][k]*src[ 14*line ] +
812  g_aiT32[TRANSFORM_INVERSE][18][k]*src[ 18*line ] + g_aiT32[TRANSFORM_INVERSE][22][k]*src[ 22*line ] +
813  g_aiT32[TRANSFORM_INVERSE][26][k]*src[ 26*line ] + g_aiT32[TRANSFORM_INVERSE][30][k]*src[ 30*line ];
814  }
815  for (k=0;k<4;k++)
816  {
817  EEO[k] = g_aiT32[TRANSFORM_INVERSE][ 4][k]*src[ 4*line ] + g_aiT32[TRANSFORM_INVERSE][12][k]*src[ 12*line ] +
818  g_aiT32[TRANSFORM_INVERSE][20][k]*src[ 20*line ] + g_aiT32[TRANSFORM_INVERSE][28][k]*src[ 28*line ];
819  }
820  EEEO[0] = g_aiT32[TRANSFORM_INVERSE][8][0]*src[ 8*line ] + g_aiT32[TRANSFORM_INVERSE][24][0]*src[ 24*line ];
821  EEEO[1] = g_aiT32[TRANSFORM_INVERSE][8][1]*src[ 8*line ] + g_aiT32[TRANSFORM_INVERSE][24][1]*src[ 24*line ];
822  EEEE[0] = g_aiT32[TRANSFORM_INVERSE][0][0]*src[ 0 ] + g_aiT32[TRANSFORM_INVERSE][16][0]*src[ 16*line ];
823  EEEE[1] = g_aiT32[TRANSFORM_INVERSE][0][1]*src[ 0 ] + g_aiT32[TRANSFORM_INVERSE][16][1]*src[ 16*line ];
824 
825  /* Combining even and odd terms at each hierarchy levels to calculate the final spatial domain vector */
826  EEE[0] = EEEE[0] + EEEO[0];
827  EEE[3] = EEEE[0] - EEEO[0];
828  EEE[1] = EEEE[1] + EEEO[1];
829  EEE[2] = EEEE[1] - EEEO[1];
830  for (k=0;k<4;k++)
831  {
832  EE[k] = EEE[k] + EEO[k];
833  EE[k+4] = EEE[3-k] - EEO[3-k];
834  }
835  for (k=0;k<8;k++)
836  {
837  E[k] = EE[k] + EO[k];
838  E[k+8] = EE[7-k] - EO[7-k];
839  }
840  for (k=0;k<16;k++)
841  {
842  dst[k] = Clip3( outputMinimum, outputMaximum, (E[k] + O[k] + add)>>shift );
843  dst[k+16] = Clip3( outputMinimum, outputMaximum, (E[15-k] - O[15-k] + add)>>shift );
844  }
845  src ++;
846  dst += 32;
847  }
848 }
849 
860 Void xTrMxN(Int bitDepth, TCoeff *block, TCoeff *coeff, Int iWidth, Int iHeight, Bool useDST, const Int maxTrDynamicRange)
861 {
862  static const Int TRANSFORM_MATRIX_SHIFT = g_transformMatrixShift[TRANSFORM_FORWARD];
863 
864  const Int shift_1st = ((g_aucConvertToBit[iWidth] + 2) + bitDepth + TRANSFORM_MATRIX_SHIFT) - maxTrDynamicRange;
865  const Int shift_2nd = (g_aucConvertToBit[iHeight] + 2) + TRANSFORM_MATRIX_SHIFT;
866 
867  assert(shift_1st >= 0);
868  assert(shift_2nd >= 0);
869 
870  TCoeff tmp[ MAX_TU_SIZE * MAX_TU_SIZE ];
871 
872  switch (iWidth)
873  {
874  case 4:
875  {
876  if ((iHeight == 4) && useDST) // Check for DCT or DST
877  {
878  fastForwardDst( block, tmp, shift_1st );
879  }
880  else
881  {
882  partialButterfly4 ( block, tmp, shift_1st, iHeight );
883  }
884  }
885  break;
886 
887  case 8: partialButterfly8 ( block, tmp, shift_1st, iHeight ); break;
888  case 16: partialButterfly16( block, tmp, shift_1st, iHeight ); break;
889  case 32: partialButterfly32( block, tmp, shift_1st, iHeight ); break;
890  default:
891  assert(0); exit (1); break;
892  }
893 
894  switch (iHeight)
895  {
896  case 4:
897  {
898  if ((iWidth == 4) && useDST) // Check for DCT or DST
899  {
900  fastForwardDst( tmp, coeff, shift_2nd );
901  }
902  else
903  {
904  partialButterfly4 ( tmp, coeff, shift_2nd, iWidth );
905  }
906  }
907  break;
908 
909  case 8: partialButterfly8 ( tmp, coeff, shift_2nd, iWidth ); break;
910  case 16: partialButterfly16( tmp, coeff, shift_2nd, iWidth ); break;
911  case 32: partialButterfly32( tmp, coeff, shift_2nd, iWidth ); break;
912  default:
913  assert(0); exit (1); break;
914  }
915 }
916 
917 
927 Void xITrMxN(Int bitDepth, TCoeff *coeff, TCoeff *block, Int iWidth, Int iHeight, Bool useDST, const Int maxTrDynamicRange)
928 {
929  static const Int TRANSFORM_MATRIX_SHIFT = g_transformMatrixShift[TRANSFORM_INVERSE];
930 
931  Int shift_1st = TRANSFORM_MATRIX_SHIFT + 1; //1 has been added to shift_1st at the expense of shift_2nd
932  Int shift_2nd = (TRANSFORM_MATRIX_SHIFT + maxTrDynamicRange - 1) - bitDepth;
933  const TCoeff clipMinimum = -(1 << maxTrDynamicRange);
934  const TCoeff clipMaximum = (1 << maxTrDynamicRange) - 1;
935 
936  assert(shift_1st >= 0);
937  assert(shift_2nd >= 0);
938 
940 
941  switch (iHeight)
942  {
943  case 4:
944  {
945  if ((iWidth == 4) && useDST) // Check for DCT or DST
946  {
947  fastInverseDst( coeff, tmp, shift_1st, clipMinimum, clipMaximum);
948  }
949  else
950  {
951  partialButterflyInverse4 ( coeff, tmp, shift_1st, iWidth, clipMinimum, clipMaximum);
952  }
953  }
954  break;
955 
956  case 8: partialButterflyInverse8 ( coeff, tmp, shift_1st, iWidth, clipMinimum, clipMaximum); break;
957  case 16: partialButterflyInverse16( coeff, tmp, shift_1st, iWidth, clipMinimum, clipMaximum); break;
958  case 32: partialButterflyInverse32( coeff, tmp, shift_1st, iWidth, clipMinimum, clipMaximum); break;
959 
960  default:
961  assert(0); exit (1); break;
962  }
963 
964  switch (iWidth)
965  {
966  // Clipping here is not in the standard, but is used to protect the "Pel" data type into which the inverse-transformed samples will be copied
967  case 4:
968  {
969  if ((iHeight == 4) && useDST) // Check for DCT or DST
970  {
971  fastInverseDst( tmp, block, shift_2nd, std::numeric_limits<Pel>::min(), std::numeric_limits<Pel>::max() );
972  }
973  else
974  {
975  partialButterflyInverse4 ( tmp, block, shift_2nd, iHeight, std::numeric_limits<Pel>::min(), std::numeric_limits<Pel>::max());
976  }
977  }
978  break;
979 
980  case 8: partialButterflyInverse8 ( tmp, block, shift_2nd, iHeight, std::numeric_limits<Pel>::min(), std::numeric_limits<Pel>::max()); break;
981  case 16: partialButterflyInverse16( tmp, block, shift_2nd, iHeight, std::numeric_limits<Pel>::min(), std::numeric_limits<Pel>::max()); break;
982  case 32: partialButterflyInverse32( tmp, block, shift_2nd, iHeight, std::numeric_limits<Pel>::min(), std::numeric_limits<Pel>::max()); break;
983 
984  default:
985  assert(0); exit (1); break;
986  }
987 }
988 
989 
990 // To minimize the distortion only. No rate is considered.
991 Void TComTrQuant::signBitHidingHDQ( const ComponentID compID, TCoeff* pQCoef, TCoeff* pCoef, TCoeff* deltaU, const TUEntropyCodingParameters &codingParameters )
992 {
993  const UInt width = codingParameters.widthInGroups << MLS_CG_LOG2_WIDTH;
994  const UInt height = codingParameters.heightInGroups << MLS_CG_LOG2_HEIGHT;
995  const UInt groupSize = 1 << MLS_CG_SIZE;
996 
997  const TCoeff entropyCodingMinimum = -(1 << g_maxTrDynamicRange[toChannelType(compID)]);
998  const TCoeff entropyCodingMaximum = (1 << g_maxTrDynamicRange[toChannelType(compID)]) - 1;
999 
1000  Int lastCG = -1;
1001  Int absSum = 0 ;
1002  Int n ;
1003 
1004  for( Int subSet = (width*height-1) >> MLS_CG_SIZE; subSet >= 0; subSet-- )
1005  {
1006  Int subPos = subSet << MLS_CG_SIZE;
1007  Int firstNZPosInCG=groupSize , lastNZPosInCG=-1 ;
1008  absSum = 0 ;
1009 
1010  for(n = groupSize-1; n >= 0; --n )
1011  {
1012  if( pQCoef[ codingParameters.scan[ n + subPos ]] )
1013  {
1014  lastNZPosInCG = n;
1015  break;
1016  }
1017  }
1018 
1019  for(n = 0; n <groupSize; n++ )
1020  {
1021  if( pQCoef[ codingParameters.scan[ n + subPos ]] )
1022  {
1023  firstNZPosInCG = n;
1024  break;
1025  }
1026  }
1027 
1028  for(n = firstNZPosInCG; n <=lastNZPosInCG; n++ )
1029  {
1030  absSum += Int(pQCoef[ codingParameters.scan[ n + subPos ]]);
1031  }
1032 
1033  if(lastNZPosInCG>=0 && lastCG==-1)
1034  {
1035  lastCG = 1 ;
1036  }
1037 
1038  if( lastNZPosInCG-firstNZPosInCG>=SBH_THRESHOLD )
1039  {
1040  UInt signbit = (pQCoef[codingParameters.scan[subPos+firstNZPosInCG]]>0?0:1) ;
1041  if( signbit!=(absSum&0x1) ) //compare signbit with sum_parity
1042  {
1043  TCoeff curCost = std::numeric_limits<TCoeff>::max();
1044  TCoeff minCostInc = std::numeric_limits<TCoeff>::max();
1045  Int minPos =-1, finalChange=0, curChange=0;
1046 
1047  for( n = (lastCG==1?lastNZPosInCG:groupSize-1) ; n >= 0; --n )
1048  {
1049  UInt blkPos = codingParameters.scan[ n+subPos ];
1050  if(pQCoef[ blkPos ] != 0 )
1051  {
1052  if(deltaU[blkPos]>0)
1053  {
1054  curCost = - deltaU[blkPos];
1055  curChange=1 ;
1056  }
1057  else
1058  {
1059  //curChange =-1;
1060  if(n==firstNZPosInCG && abs(pQCoef[blkPos])==1)
1061  {
1062  curCost = std::numeric_limits<TCoeff>::max();
1063  }
1064  else
1065  {
1066  curCost = deltaU[blkPos];
1067  curChange =-1;
1068  }
1069  }
1070  }
1071  else
1072  {
1073  if(n<firstNZPosInCG)
1074  {
1075  UInt thisSignBit = (pCoef[blkPos]>=0?0:1);
1076  if(thisSignBit != signbit )
1077  {
1078  curCost = std::numeric_limits<TCoeff>::max();
1079  }
1080  else
1081  {
1082  curCost = - (deltaU[blkPos]) ;
1083  curChange = 1 ;
1084  }
1085  }
1086  else
1087  {
1088  curCost = - (deltaU[blkPos]) ;
1089  curChange = 1 ;
1090  }
1091  }
1092 
1093  if( curCost<minCostInc)
1094  {
1095  minCostInc = curCost ;
1096  finalChange = curChange ;
1097  minPos = blkPos ;
1098  }
1099  } //CG loop
1100 
1101  if(pQCoef[minPos] == entropyCodingMaximum || pQCoef[minPos] == entropyCodingMinimum)
1102  {
1103  finalChange = -1;
1104  }
1105 
1106  if(pCoef[minPos]>=0)
1107  {
1108  pQCoef[minPos] += finalChange ;
1109  }
1110  else
1111  {
1112  pQCoef[minPos] -= finalChange ;
1113  }
1114  } // Hide
1115  }
1116  if(lastCG==1)
1117  {
1118  lastCG=0 ;
1119  }
1120  } // TU loop
1121 
1122  return;
1123 }
1124 
1125 
1127  TCoeff * pSrc,
1128  TCoeff * pDes,
1130  TCoeff *pArlDes,
1131 #endif
1132  TCoeff &uiAbsSum,
1133  const ComponentID compID,
1134  const QpParam &cQP )
1135 {
1136  const TComRectangle &rect = rTu.getRect(compID);
1137  const UInt uiWidth = rect.width;
1138  const UInt uiHeight = rect.height;
1139  TComDataCU* pcCU = rTu.getCU();
1140  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
1141 
1142  TCoeff* piCoef = pSrc;
1143  TCoeff* piQCoef = pDes;
1144 #if ADAPTIVE_QP_SELECTION
1145  TCoeff* piArlCCoef = pArlDes;
1146 #endif
1147 
1148  const Bool useTransformSkip = pcCU->getTransformSkip(uiAbsPartIdx, compID);
1149 
1150  Bool useRDOQ = useTransformSkip ? m_useRDOQTS : m_useRDOQ;
1151  if ( useRDOQ && (isLuma(compID) || RDOQ_CHROMA) )
1152  {
1153 #if ADAPTIVE_QP_SELECTION
1154  xRateDistOptQuant( rTu, piCoef, pDes, pArlDes, uiAbsSum, compID, cQP );
1155 #else
1156  xRateDistOptQuant( rTu, piCoef, pDes, uiAbsSum, compID, cQP );
1157 #endif
1158  }
1159  else
1160  {
1161  TUEntropyCodingParameters codingParameters;
1162  getTUEntropyCodingParameters(codingParameters, rTu, compID);
1163 
1164  const TCoeff entropyCodingMinimum = -(1 << g_maxTrDynamicRange[toChannelType(compID)]);
1165  const TCoeff entropyCodingMaximum = (1 << g_maxTrDynamicRange[toChannelType(compID)]) - 1;
1166 
1167  TCoeff deltaU[MAX_TU_SIZE * MAX_TU_SIZE];
1168 
1169  const UInt uiLog2TrSize = rTu.GetEquivalentLog2TrSize(compID);
1170 
1171  Int scalingListType = getScalingListType(pcCU->getPredictionMode(uiAbsPartIdx), compID);
1172  assert(scalingListType < SCALING_LIST_NUM);
1173  Int *piQuantCoeff = getQuantCoeff(scalingListType, cQP.rem, uiLog2TrSize-2);
1174 
1175  const Bool enableScalingLists = getUseScalingList(uiWidth, uiHeight, (pcCU->getTransformSkip(uiAbsPartIdx, compID) != 0));
1176  const Int defaultQuantisationCoefficient = g_quantScales[cQP.rem];
1177 
1178  /* for 422 chroma blocks, the effective scaling applied during transformation is not a power of 2, hence it cannot be
1179  * implemented as a bit-shift (the quantised result will be sqrt(2) * larger than required). Alternatively, adjust the
1180  * uiLog2TrSize applied in iTransformShift, such that the result is 1/sqrt(2) the required result (i.e. smaller)
1181  * Then a QP+3 (sqrt(2)) or QP-3 (1/sqrt(2)) method could be used to get the required result
1182  */
1183 
1184  // Represents scaling through forward transform
1185  Int iTransformShift = getTransformShift(toChannelType(compID), uiLog2TrSize);
1186  if (useTransformSkip && pcCU->getSlice()->getSPS()->getUseExtendedPrecision())
1187  {
1188  iTransformShift = std::max<Int>(0, iTransformShift);
1189  }
1190 
1191  const Int iQBits = QUANT_SHIFT + cQP.per + iTransformShift;
1192  // QBits will be OK for any internal bit depth as the reduction in transform shift is balanced by an increase in Qp_per due to QpBDOffset
1193 
1194 #if ADAPTIVE_QP_SELECTION
1195  Int iQBitsC = MAX_INT;
1196  Int iAddC = MAX_INT;
1197 
1198  if (m_bUseAdaptQpSelect)
1199  {
1200  iQBitsC = iQBits - ARL_C_PRECISION;
1201  iAddC = 1 << (iQBitsC-1);
1202  }
1203 #endif
1204 
1205  const Int iAdd = (pcCU->getSlice()->getSliceType()==I_SLICE ? 171 : 85) << (iQBits-9);
1206  const Int qBits8 = iQBits - 8;
1207 
1208  for( Int uiBlockPos = 0; uiBlockPos < uiWidth*uiHeight; uiBlockPos++ )
1209  {
1210  const TCoeff iLevel = piCoef[uiBlockPos];
1211  const TCoeff iSign = (iLevel < 0 ? -1: 1);
1212 
1213  const Int64 tmpLevel = (Int64)abs(iLevel) * (enableScalingLists ? piQuantCoeff[uiBlockPos] : defaultQuantisationCoefficient);
1214 
1215 #if ADAPTIVE_QP_SELECTION
1216  if( m_bUseAdaptQpSelect )
1217  {
1218  piArlCCoef[uiBlockPos] = (TCoeff)((tmpLevel + iAddC ) >> iQBitsC);
1219  }
1220 #endif
1221 
1222  const TCoeff quantisedMagnitude = TCoeff((tmpLevel + iAdd ) >> iQBits);
1223  deltaU[uiBlockPos] = (TCoeff)((tmpLevel - (quantisedMagnitude<<iQBits) )>> qBits8);
1224 
1225  uiAbsSum += quantisedMagnitude;
1226  const TCoeff quantisedCoefficient = quantisedMagnitude * iSign;
1227 
1228  piQCoef[uiBlockPos] = Clip3<TCoeff>( entropyCodingMinimum, entropyCodingMaximum, quantisedCoefficient );
1229  } // for n
1230 
1231  if( pcCU->getSlice()->getPPS()->getSignHideFlag() )
1232  {
1233  if(uiAbsSum >= 2) //this prevents TUs with only one coefficient of value 1 from being tested
1234  {
1235  signBitHidingHDQ( compID, piQCoef, piCoef, deltaU, codingParameters ) ;
1236  }
1237  }
1238  } //if RDOQ
1239  //return;
1240 }
1241 
1243  const TCoeff * pSrc,
1244  TCoeff * pDes,
1245  const ComponentID compID,
1246  const QpParam &cQP )
1247 {
1248  assert(compID<MAX_NUM_COMPONENT);
1249 
1250  TComDataCU *pcCU = rTu.getCU();
1251  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
1252  const TComRectangle &rect = rTu.getRect(compID);
1253  const UInt uiWidth = rect.width;
1254  const UInt uiHeight = rect.height;
1255  const TCoeff *const piQCoef = pSrc;
1256  TCoeff *const piCoef = pDes;
1257  const UInt uiLog2TrSize = rTu.GetEquivalentLog2TrSize(compID);
1258  const UInt numSamplesInBlock = uiWidth*uiHeight;
1259  const TCoeff transformMinimum = -(1 << g_maxTrDynamicRange[toChannelType(compID)]);
1260  const TCoeff transformMaximum = (1 << g_maxTrDynamicRange[toChannelType(compID)]) - 1;
1261  const Bool enableScalingLists = getUseScalingList(uiWidth, uiHeight, (pcCU->getTransformSkip(uiAbsPartIdx, compID) != 0));
1262  const Int scalingListType = getScalingListType(pcCU->getPredictionMode(uiAbsPartIdx), compID);
1263 
1264  assert (scalingListType < SCALING_LIST_NUM);
1265  assert ( uiWidth <= m_uiMaxTrSize );
1266 
1267  // Represents scaling through forward transform
1268  const Bool bClipTransformShiftTo0 = (pcCU->getTransformSkip(uiAbsPartIdx, compID) != 0) && pcCU->getSlice()->getSPS()->getUseExtendedPrecision();
1269  const Int originalTransformShift = getTransformShift(toChannelType(compID), uiLog2TrSize);
1270  const Int iTransformShift = bClipTransformShiftTo0 ? std::max<Int>(0, originalTransformShift) : originalTransformShift;
1271 
1272  const Int QP_per = cQP.per;
1273  const Int QP_rem = cQP.rem;
1274 
1275  const Int rightShift = (IQUANT_SHIFT - (iTransformShift + QP_per)) + (enableScalingLists ? LOG2_SCALING_LIST_NEUTRAL_VALUE : 0);
1276 
1277  if(enableScalingLists)
1278  {
1279  //from the dequantisation equation:
1280  //iCoeffQ = ((Intermediate_Int(clipQCoef) * piDequantCoef[deQuantIdx]) + iAdd ) >> rightShift
1281  //(sizeof(Intermediate_Int) * 8) = inputBitDepth + dequantCoefBits - rightShift
1282  const UInt dequantCoefBits = 1 + IQUANT_SHIFT + SCALING_LIST_BITS;
1283  const UInt targetInputBitDepth = std::min<UInt>((g_maxTrDynamicRange[toChannelType(compID)] + 1), (((sizeof(Intermediate_Int) * 8) + rightShift) - dequantCoefBits));
1284 
1285  const Intermediate_Int inputMinimum = -(1 << (targetInputBitDepth - 1));
1286  const Intermediate_Int inputMaximum = (1 << (targetInputBitDepth - 1)) - 1;
1287 
1288  Int *piDequantCoef = getDequantCoeff(scalingListType,QP_rem,uiLog2TrSize-2);
1289 
1290  if(rightShift > 0)
1291  {
1292  const Intermediate_Int iAdd = 1 << (rightShift - 1);
1293 
1294  for( Int n = 0; n < numSamplesInBlock; n++ )
1295  {
1296  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[n]));
1297  const Intermediate_Int iCoeffQ = ((Intermediate_Int(clipQCoef) * piDequantCoef[n]) + iAdd ) >> rightShift;
1298 
1299  piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
1300  }
1301  }
1302  else
1303  {
1304  const Int leftShift = -rightShift;
1305 
1306  for( Int n = 0; n < numSamplesInBlock; n++ )
1307  {
1308  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[n]));
1309  const Intermediate_Int iCoeffQ = (Intermediate_Int(clipQCoef) * piDequantCoef[n]) << leftShift;
1310 
1311  piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
1312  }
1313  }
1314  }
1315  else
1316  {
1317  const Int scale = g_invQuantScales[QP_rem];
1318  const Int scaleBits = (IQUANT_SHIFT + 1) ;
1319 
1320  //from the dequantisation equation:
1321  //iCoeffQ = Intermediate_Int((Int64(clipQCoef) * scale + iAdd) >> rightShift);
1322  //(sizeof(Intermediate_Int) * 8) = inputBitDepth + scaleBits - rightShift
1323  const UInt targetInputBitDepth = std::min<UInt>((g_maxTrDynamicRange[toChannelType(compID)] + 1), (((sizeof(Intermediate_Int) * 8) + rightShift) - scaleBits));
1324  const Intermediate_Int inputMinimum = -(1 << (targetInputBitDepth - 1));
1325  const Intermediate_Int inputMaximum = (1 << (targetInputBitDepth - 1)) - 1;
1326 
1327  if (rightShift > 0)
1328  {
1329  const Intermediate_Int iAdd = 1 << (rightShift - 1);
1330 
1331  for( Int n = 0; n < numSamplesInBlock; n++ )
1332  {
1333  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[n]));
1334  const Intermediate_Int iCoeffQ = (Intermediate_Int(clipQCoef) * scale + iAdd) >> rightShift;
1335 
1336  piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
1337  }
1338  }
1339  else
1340  {
1341  const Int leftShift = -rightShift;
1342 
1343  for( Int n = 0; n < numSamplesInBlock; n++ )
1344  {
1345  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, piQCoef[n]));
1346  const Intermediate_Int iCoeffQ = (Intermediate_Int(clipQCoef) * scale) << leftShift;
1347 
1348  piCoef[n] = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
1349  }
1350  }
1351  }
1352 }
1353 
1354 
1356  Bool bUseRDOQ,
1357  Bool bUseRDOQTS,
1358  Bool bEnc,
1359  Bool useTransformSkipFast
1361  , Bool bUseAdaptQpSelect
1362 #endif
1363  )
1364 {
1365  m_uiMaxTrSize = uiMaxTrSize;
1366  m_bEnc = bEnc;
1367  m_useRDOQ = bUseRDOQ;
1368  m_useRDOQTS = bUseRDOQTS;
1369 #if ADAPTIVE_QP_SELECTION
1370  m_bUseAdaptQpSelect = bUseAdaptQpSelect;
1371 #endif
1372  m_useTransformSkipFast = useTransformSkipFast;
1373 }
1374 
1375 
1377  const ComponentID compID,
1378  Pel * pcResidual,
1379  const UInt uiStride,
1380  TCoeff * rpcCoeff,
1382  TCoeff * pcArlCoeff,
1383 #endif
1384  TCoeff & uiAbsSum,
1385  const QpParam & cQP
1386  )
1387 {
1388  const TComRectangle &rect = rTu.getRect(compID);
1389  const UInt uiWidth = rect.width;
1390  const UInt uiHeight = rect.height;
1391  TComDataCU* pcCU = rTu.getCU();
1392  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
1393  const UInt uiOrgTrDepth = rTu.GetTransformDepthRel();
1394 
1395  uiAbsSum=0;
1396 
1397  RDPCMMode rdpcmMode = RDPCM_OFF;
1398  rdpcmNxN( rTu, compID, pcResidual, uiStride, cQP, rpcCoeff, uiAbsSum, rdpcmMode );
1399 
1400  if (rdpcmMode == RDPCM_OFF)
1401  {
1402  uiAbsSum = 0;
1403  //transform and quantise
1404  if(pcCU->getCUTransquantBypass(uiAbsPartIdx))
1405  {
1406  const Bool rotateResidual = rTu.isNonTransformedResidualRotated(compID);
1407  const UInt uiSizeMinus1 = (uiWidth * uiHeight) - 1;
1408 
1409  for (UInt y = 0, coefficientIndex = 0; y<uiHeight; y++)
1410  {
1411  for (UInt x = 0; x<uiWidth; x++, coefficientIndex++)
1412  {
1413  const Pel currentSample = pcResidual[(y * uiStride) + x];
1414 
1415  rpcCoeff[rotateResidual ? (uiSizeMinus1 - coefficientIndex) : coefficientIndex] = currentSample;
1416  uiAbsSum += TCoeff(abs(currentSample));
1417  }
1418  }
1419  }
1420  else
1421  {
1422 #ifdef DEBUG_TRANSFORM_AND_QUANTISE
1423  std::cout << g_debugCounter << ": " << uiWidth << "x" << uiHeight << " channel " << compID << " TU at input to transform\n";
1424  printBlock(pcResidual, uiWidth, uiHeight, uiStride);
1425 #endif
1426 
1427  assert( (pcCU->getSlice()->getSPS()->getMaxTrSize() >= uiWidth) );
1428 
1429  if(pcCU->getTransformSkip(uiAbsPartIdx, compID) != 0)
1430  {
1431  xTransformSkip( pcResidual, uiStride, m_plTempCoeff, rTu, compID );
1432  }
1433  else
1434  {
1435  xT( compID, rTu.useDST(compID), pcResidual, uiStride, m_plTempCoeff, uiWidth, uiHeight );
1436  }
1437 
1438 #ifdef DEBUG_TRANSFORM_AND_QUANTISE
1439  std::cout << g_debugCounter << ": " << uiWidth << "x" << uiHeight << " channel " << compID << " TU between transform and quantiser\n";
1440  printBlock(m_plTempCoeff, uiWidth, uiHeight, uiWidth);
1441 #endif
1442 
1443  xQuant( rTu, m_plTempCoeff, rpcCoeff,
1444 
1446  pcArlCoeff,
1447 #endif
1448  uiAbsSum, compID, cQP );
1449 
1450 #ifdef DEBUG_TRANSFORM_AND_QUANTISE
1451  std::cout << g_debugCounter << ": " << uiWidth << "x" << uiHeight << " channel " << compID << " TU at output of quantiser\n";
1452  printBlock(rpcCoeff, uiWidth, uiHeight, uiWidth);
1453 #endif
1454  }
1455  }
1456 
1457  //set the CBF
1458  pcCU->setCbfPartRange((((uiAbsSum > 0) ? 1 : 0) << uiOrgTrDepth), compID, uiAbsPartIdx, rTu.GetAbsPartIdxNumParts(compID));
1459 }
1460 
1461 
1463  const ComponentID compID,
1464  Pel *pcResidual,
1465  const UInt uiStride,
1466  TCoeff * pcCoeff,
1467  const QpParam &cQP
1468  DEBUG_STRING_FN_DECLAREP(psDebug))
1469 {
1470  TComDataCU* pcCU=rTu.getCU();
1471  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
1472  const TComRectangle &rect = rTu.getRect(compID);
1473  const UInt uiWidth = rect.width;
1474  const UInt uiHeight = rect.height;
1475 
1476  if (uiWidth != uiHeight) //for intra, the TU will have been split above this level, so this condition won't be true, hence this only affects inter
1477  {
1478  //------------------------------------------------
1479 
1480  //recurse deeper
1481 
1482  TComTURecurse subTURecurse(rTu, false, TComTU::VERTICAL_SPLIT, true, compID);
1483 
1484  do
1485  {
1486  //------------------
1487 
1488  const UInt lineOffset = subTURecurse.GetSectionNumber() * subTURecurse.getRect(compID).height;
1489 
1490  Pel *subTUResidual = pcResidual + (lineOffset * uiStride);
1491  TCoeff *subTUCoefficients = pcCoeff + (lineOffset * subTURecurse.getRect(compID).width);
1492 
1493  invTransformNxN(subTURecurse, compID, subTUResidual, uiStride, subTUCoefficients, cQP DEBUG_STRING_PASS_INTO(psDebug));
1494 
1495  //------------------
1496 
1497  } while (subTURecurse.nextSection(rTu));
1498 
1499  //------------------------------------------------
1500 
1501  return;
1502  }
1503 
1504 #if defined DEBUG_STRING
1505  if (psDebug)
1506  {
1507  std::stringstream ss(stringstream::out);
1508  printBlockToStream(ss, (compID==0)?"###InvTran ip Ch0: " : ((compID==1)?"###InvTran ip Ch1: ":"###InvTran ip Ch2: "), pcCoeff, uiWidth, uiHeight, uiWidth);
1509  DEBUG_STRING_APPEND((*psDebug), ss.str())
1510  }
1511 #endif
1512 
1513  if(pcCU->getCUTransquantBypass(uiAbsPartIdx))
1514  {
1515  const Bool rotateResidual = rTu.isNonTransformedResidualRotated(compID);
1516  const UInt uiSizeMinus1 = (uiWidth * uiHeight) - 1;
1517 
1518  for (UInt y = 0, coefficientIndex = 0; y<uiHeight; y++)
1519  {
1520  for (UInt x = 0; x<uiWidth; x++, coefficientIndex++)
1521  {
1522  pcResidual[(y * uiStride) + x] = Pel(pcCoeff[rotateResidual ? (uiSizeMinus1 - coefficientIndex) : coefficientIndex]);
1523  }
1524  }
1525  }
1526  else
1527  {
1528 #ifdef DEBUG_TRANSFORM_AND_QUANTISE
1529  std::cout << g_debugCounter << ": " << uiWidth << "x" << uiHeight << " channel " << compID << " TU at input to dequantiser\n";
1530  printBlock(pcCoeff, uiWidth, uiHeight, uiWidth);
1531 #endif
1532 
1533  xDeQuant(rTu, pcCoeff, m_plTempCoeff, compID, cQP);
1534 
1535 #ifdef DEBUG_TRANSFORM_AND_QUANTISE
1536  std::cout << g_debugCounter << ": " << uiWidth << "x" << uiHeight << " channel " << compID << " TU between dequantiser and inverse-transform\n";
1537  printBlock(m_plTempCoeff, uiWidth, uiHeight, uiWidth);
1538 #endif
1539 
1540 #if defined DEBUG_STRING
1541  if (psDebug)
1542  {
1543  std::stringstream ss(stringstream::out);
1544  printBlockToStream(ss, "###InvTran deq: ", m_plTempCoeff, uiWidth, uiHeight, uiWidth);
1545  (*psDebug)+=ss.str();
1546  }
1547 #endif
1548 
1549  if(pcCU->getTransformSkip(uiAbsPartIdx, compID))
1550  {
1551  xITransformSkip( m_plTempCoeff, pcResidual, uiStride, rTu, compID );
1552 
1553 #if defined DEBUG_STRING
1554  if (psDebug)
1555  {
1556  std::stringstream ss(stringstream::out);
1557  printBlockToStream(ss, "###InvTran resi: ", pcResidual, uiWidth, uiHeight, uiStride);
1558  (*psDebug)+=ss.str();
1559  (*psDebug)+="(<- was a Transform-skipped block)\n";
1560  }
1561 #endif
1562  }
1563  else
1564  {
1565  xIT( compID, rTu.useDST(compID), m_plTempCoeff, pcResidual, uiStride, uiWidth, uiHeight );
1566 
1567 #if defined DEBUG_STRING
1568  if (psDebug)
1569  {
1570  std::stringstream ss(stringstream::out);
1571  printBlockToStream(ss, "###InvTran resi: ", pcResidual, uiWidth, uiHeight, uiStride);
1572  (*psDebug)+=ss.str();
1573  (*psDebug)+="(<- was a Transformed block)\n";
1574  }
1575 #endif
1576  }
1577 
1578 #ifdef DEBUG_TRANSFORM_AND_QUANTISE
1579  std::cout << g_debugCounter << ": " << uiWidth << "x" << uiHeight << " channel " << compID << " TU at output of inverse-transform\n";
1580  printBlock(pcResidual, uiWidth, uiHeight, uiStride);
1581  g_debugCounter++;
1582 #endif
1583  }
1584 
1585  invRdpcmNxN( rTu, compID, pcResidual, uiStride );
1586 }
1587 
1589  TComYuv *pResidual,
1590  TComTU &rTu)
1591 {
1592  if (!rTu.ProcessComponentSection(compID))
1593  {
1594  return;
1595  }
1596 
1597  TComDataCU* pcCU = rTu.getCU();
1598  UInt absPartIdxTU = rTu.GetAbsPartIdxTU();
1599  UInt uiTrMode=rTu.GetTransformDepthRel();
1600  if( (pcCU->getCbf(absPartIdxTU, compID, uiTrMode) == 0) && (isLuma(compID) || !pcCU->getSlice()->getPPS()->getUseCrossComponentPrediction()) )
1601  {
1602  return;
1603  }
1604 
1605  if( uiTrMode == pcCU->getTransformIdx( absPartIdxTU ) )
1606  {
1607  const TComRectangle &tuRect = rTu.getRect(compID);
1608  const Int uiStride = pResidual->getStride( compID );
1609  Pel *rpcResidual = pResidual->getAddr( compID );
1610  UInt uiAddr = (tuRect.x0 + uiStride*tuRect.y0);
1611  Pel *pResi = rpcResidual + uiAddr;
1612  TCoeff *pcCoeff = pcCU->getCoeff(compID) + rTu.getCoefficientOffset(compID);
1613 
1614  const QpParam cQP(*pcCU, compID);
1615 
1616  if(pcCU->getCbf(absPartIdxTU, compID, uiTrMode) != 0)
1617  {
1618  DEBUG_STRING_NEW(sTemp)
1619 #ifdef DEBUG_STRING
1620  std::string *psDebug=((DebugOptionList::DebugString_InvTran.getInt()&(pcCU->isIntra(absPartIdxTU)?1:(pcCU->isInter(absPartIdxTU)?2:4)))!=0) ? &sTemp : 0;
1621 #endif
1622 
1623  invTransformNxN( rTu, compID, pResi, uiStride, pcCoeff, cQP DEBUG_STRING_PASS_INTO(psDebug) );
1624 
1625 #ifdef DEBUG_STRING
1626  if (psDebug != 0)
1627  {
1628  std::cout << (*psDebug);
1629  }
1630 #endif
1631  }
1632 
1633  if (isChroma(compID) && (pcCU->getCrossComponentPredictionAlpha(absPartIdxTU, compID) != 0))
1634  {
1635  const Pel *piResiLuma = pResidual->getAddr( COMPONENT_Y );
1636  const Int strideLuma = pResidual->getStride( COMPONENT_Y );
1637  const Int tuWidth = rTu.getRect( compID ).width;
1638  const Int tuHeight = rTu.getRect( compID ).height;
1639 
1640  if(pcCU->getCbf(absPartIdxTU, COMPONENT_Y, uiTrMode) != 0)
1641  {
1642  pResi = rpcResidual + uiAddr;
1643  const Pel *pResiLuma = piResiLuma + uiAddr;
1644 
1645  crossComponentPrediction( rTu, compID, pResiLuma, pResi, pResi, tuWidth, tuHeight, strideLuma, uiStride, uiStride, true );
1646  }
1647  }
1648  }
1649  else
1650  {
1651  TComTURecurse tuRecurseChild(rTu, false);
1652  do
1653  {
1654  invRecurTransformNxN( compID, pResidual, tuRecurseChild );
1655  } while (tuRecurseChild.nextSection(rTu));
1656  }
1657 }
1658 
1659 Void TComTrQuant::applyForwardRDPCM( TComTU& rTu, const ComponentID compID, Pel* pcResidual, const UInt uiStride, const QpParam& cQP, TCoeff* pcCoeff, TCoeff &uiAbsSum, const RDPCMMode mode )
1660 {
1661  TComDataCU *pcCU=rTu.getCU();
1662  const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU();
1663 
1664  const Bool bLossless = pcCU->getCUTransquantBypass( uiAbsPartIdx );
1665  const UInt uiWidth = rTu.getRect(compID).width;
1666  const UInt uiHeight = rTu.getRect(compID).height;
1667  const Bool rotateResidual = rTu.isNonTransformedResidualRotated(compID);
1668  const UInt uiSizeMinus1 = (uiWidth * uiHeight) - 1;
1669 
1670  UInt uiX = 0;
1671  UInt uiY = 0;
1672 
1673  UInt &majorAxis = (mode == RDPCM_VER) ? uiX : uiY;
1674  UInt &minorAxis = (mode == RDPCM_VER) ? uiY : uiX;
1675  const UInt majorAxisLimit = (mode == RDPCM_VER) ? uiWidth : uiHeight;
1676  const UInt minorAxisLimit = (mode == RDPCM_VER) ? uiHeight : uiWidth;
1677 
1678  const Bool bUseHalfRoundingPoint = (mode != RDPCM_OFF);
1679 
1680  uiAbsSum = 0;
1681 
1682  for ( majorAxis = 0; majorAxis < majorAxisLimit; majorAxis++ )
1683  {
1684  TCoeff accumulatorValue = 0; // 32-bit accumulator
1685  for ( minorAxis = 0; minorAxis < minorAxisLimit; minorAxis++ )
1686  {
1687  const UInt sampleIndex = (uiY * uiWidth) + uiX;
1688  const UInt coefficientIndex = (rotateResidual ? (uiSizeMinus1-sampleIndex) : sampleIndex);
1689  const Pel currentSample = pcResidual[(uiY * uiStride) + uiX];
1690  const TCoeff encoderSideDelta = TCoeff(currentSample) - accumulatorValue;
1691 
1692  Pel reconstructedDelta;
1693  if ( bLossless )
1694  {
1695  pcCoeff[coefficientIndex] = encoderSideDelta;
1696  reconstructedDelta = encoderSideDelta;
1697  }
1698  else
1699  {
1700  transformSkipQuantOneSample(rTu, compID, encoderSideDelta, pcCoeff, coefficientIndex, cQP, bUseHalfRoundingPoint);
1701  invTrSkipDeQuantOneSample (rTu, compID, pcCoeff[coefficientIndex], reconstructedDelta, cQP, coefficientIndex);
1702  }
1703 
1704  uiAbsSum += abs(pcCoeff[coefficientIndex]);
1705 
1706  if (mode != RDPCM_OFF)
1707  {
1708  accumulatorValue += reconstructedDelta;
1709  }
1710  }
1711  }
1712 }
1713 
1714 Void TComTrQuant::rdpcmNxN ( TComTU& rTu, const ComponentID compID, Pel* pcResidual, const UInt uiStride, const QpParam& cQP, TCoeff* pcCoeff, TCoeff &uiAbsSum, RDPCMMode& rdpcmMode )
1715 {
1716  TComDataCU *pcCU=rTu.getCU();
1717  const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU();
1718 
1719  if (!pcCU->isRDPCMEnabled(uiAbsPartIdx) || ((pcCU->getTransformSkip(uiAbsPartIdx, compID) == 0) && !pcCU->getCUTransquantBypass(uiAbsPartIdx)))
1720  {
1721  rdpcmMode = RDPCM_OFF;
1722  }
1723  else if ( pcCU->isIntra( uiAbsPartIdx ) )
1724  {
1725  const ChromaFormat chFmt = pcCU->getPic()->getPicYuvOrg()->getChromaFormat();
1726  const ChannelType chType = toChannelType(compID);
1727  const UInt uiChPredMode = pcCU->getIntraDir( chType, uiAbsPartIdx );
1728  const UInt uiChCodedMode = (uiChPredMode==DM_CHROMA_IDX && isChroma(compID)) ? pcCU->getIntraDir(CHANNEL_TYPE_LUMA, getChromasCorrespondingPULumaIdx(uiAbsPartIdx, chFmt)) : uiChPredMode;
1729  const UInt uiChFinalMode = ((chFmt == CHROMA_422) && isChroma(compID)) ? g_chroma422IntraAngleMappingTable[uiChCodedMode] : uiChCodedMode;
1730 
1731  if (uiChFinalMode == VER_IDX || uiChFinalMode == HOR_IDX)
1732  {
1733  rdpcmMode = (uiChFinalMode == VER_IDX) ? RDPCM_VER : RDPCM_HOR;
1734  applyForwardRDPCM( rTu, compID, pcResidual, uiStride, cQP, pcCoeff, uiAbsSum, rdpcmMode );
1735  }
1736  else
1737  {
1738  rdpcmMode = RDPCM_OFF;
1739  }
1740  }
1741  else // not intra, need to select the best mode
1742  {
1743  const UInt uiWidth = rTu.getRect(compID).width;
1744  const UInt uiHeight = rTu.getRect(compID).height;
1745 
1746  RDPCMMode bestMode = NUMBER_OF_RDPCM_MODES;
1747  TCoeff bestAbsSum = std::numeric_limits<TCoeff>::max();
1748  TCoeff bestCoefficients[MAX_TU_SIZE * MAX_TU_SIZE];
1749 
1750  for (UInt modeIndex = 0; modeIndex < NUMBER_OF_RDPCM_MODES; modeIndex++)
1751  {
1752  const RDPCMMode mode = RDPCMMode(modeIndex);
1753 
1754  TCoeff currAbsSum = 0;
1755 
1756  applyForwardRDPCM( rTu, compID, pcResidual, uiStride, cQP, pcCoeff, currAbsSum, mode );
1757 
1758  if (currAbsSum < bestAbsSum)
1759  {
1760  bestMode = mode;
1761  bestAbsSum = currAbsSum;
1762  if (mode != RDPCM_OFF)
1763  {
1764  memcpy(bestCoefficients, pcCoeff, (uiWidth * uiHeight * sizeof(TCoeff)));
1765  }
1766  }
1767  }
1768 
1769  rdpcmMode = bestMode;
1770  uiAbsSum = bestAbsSum;
1771 
1772  if (rdpcmMode != RDPCM_OFF) //the TU is re-transformed and quantised if DPCM_OFF is returned, so there is no need to preserve it here
1773  {
1774  memcpy(pcCoeff, bestCoefficients, (uiWidth * uiHeight * sizeof(TCoeff)));
1775  }
1776  }
1777 
1778  pcCU->setExplicitRdpcmModePartRange(rdpcmMode, compID, uiAbsPartIdx, rTu.GetAbsPartIdxNumParts(compID));
1779 }
1780 
1781 Void TComTrQuant::invRdpcmNxN( TComTU& rTu, const ComponentID compID, Pel* pcResidual, const UInt uiStride )
1782 {
1783  TComDataCU *pcCU=rTu.getCU();
1784  const UInt uiAbsPartIdx=rTu.GetAbsPartIdxTU();
1785 
1786  if (pcCU->isRDPCMEnabled( uiAbsPartIdx ) && ((pcCU->getTransformSkip(uiAbsPartIdx, compID ) != 0) || pcCU->getCUTransquantBypass(uiAbsPartIdx)))
1787  {
1788  const UInt uiWidth = rTu.getRect(compID).width;
1789  const UInt uiHeight = rTu.getRect(compID).height;
1790 
1791  RDPCMMode rdpcmMode = RDPCM_OFF;
1792 
1793  if ( pcCU->isIntra( uiAbsPartIdx ) )
1794  {
1795  const ChromaFormat chFmt = pcCU->getPic()->getPicYuvRec()->getChromaFormat();
1796  const ChannelType chType = toChannelType(compID);
1797  const UInt uiChPredMode = pcCU->getIntraDir( chType, uiAbsPartIdx );
1798  const UInt uiChCodedMode = (uiChPredMode==DM_CHROMA_IDX && isChroma(compID)) ? pcCU->getIntraDir(CHANNEL_TYPE_LUMA, getChromasCorrespondingPULumaIdx(uiAbsPartIdx, chFmt)) : uiChPredMode;
1799  const UInt uiChFinalMode = ((chFmt == CHROMA_422) && isChroma(compID)) ? g_chroma422IntraAngleMappingTable[uiChCodedMode] : uiChCodedMode;
1800 
1801  if (uiChFinalMode == VER_IDX || uiChFinalMode == HOR_IDX)
1802  {
1803  rdpcmMode = (uiChFinalMode == VER_IDX) ? RDPCM_VER : RDPCM_HOR;
1804  }
1805  }
1806  else // not intra case
1807  {
1808  rdpcmMode = RDPCMMode(pcCU->getExplicitRdpcmMode( compID, uiAbsPartIdx ));
1809  }
1810 
1811  static const TCoeff pelMin=(TCoeff) std::numeric_limits<Pel>::min();
1812  static const TCoeff pelMax=(TCoeff) std::numeric_limits<Pel>::max();
1813  if (rdpcmMode == RDPCM_VER)
1814  {
1815  for( UInt uiX = 0; uiX < uiWidth; uiX++ )
1816  {
1817  Pel *pcCurResidual = pcResidual+uiX;
1818  TCoeff accumulator = *pcCurResidual; // 32-bit accumulator
1819  pcCurResidual+=uiStride;
1820  for( UInt uiY = 1; uiY < uiHeight; uiY++, pcCurResidual+=uiStride )
1821  {
1822  accumulator += *(pcCurResidual);
1823  *pcCurResidual = (Pel)Clip3<TCoeff>(pelMin, pelMax, accumulator);
1824  }
1825  }
1826  }
1827  else if (rdpcmMode == RDPCM_HOR)
1828  {
1829  for( UInt uiY = 0; uiY < uiHeight; uiY++ )
1830  {
1831  Pel *pcCurResidual = pcResidual+uiY*uiStride;
1832  TCoeff accumulator = *pcCurResidual;
1833  pcCurResidual++;
1834  for( UInt uiX = 1; uiX < uiWidth; uiX++, pcCurResidual++ )
1835  {
1836  accumulator += *(pcCurResidual);
1837  *pcCurResidual = (Pel)Clip3<TCoeff>(pelMin, pelMax, accumulator);
1838  }
1839  }
1840  }
1841  }
1842 }
1843 
1844 // ------------------------------------------------------------------------------------------------
1845 // Logical transform
1846 // ------------------------------------------------------------------------------------------------
1847 
1857 Void TComTrQuant::xT( const ComponentID compID, Bool useDST, Pel* piBlkResi, UInt uiStride, TCoeff* psCoeff, Int iWidth, Int iHeight )
1858 {
1859 #if MATRIX_MULT
1860  if( iWidth == iHeight)
1861  {
1862  xTr(g_bitDepth[toChannelType(compID)], piBlkResi, psCoeff, uiStride, (UInt)iWidth, useDST, g_maxTrDynamicRange[toChannelType(compID)]);
1863  return;
1864  }
1865 #endif
1866 
1867  TCoeff block[ MAX_TU_SIZE * MAX_TU_SIZE ];
1868  TCoeff coeff[ MAX_TU_SIZE * MAX_TU_SIZE ];
1869 
1870  for (Int y = 0; y < iHeight; y++)
1871  {
1872  for (Int x = 0; x < iWidth; x++)
1873  {
1874  block[(y * iWidth) + x] = piBlkResi[(y * uiStride) + x];
1875  }
1876  }
1877 
1878  xTrMxN( g_bitDepth[toChannelType(compID)], block, coeff, iWidth, iHeight, useDST, g_maxTrDynamicRange[toChannelType(compID)] );
1879 
1880  memcpy(psCoeff, coeff, (iWidth * iHeight * sizeof(TCoeff)));
1881 }
1882 
1892 Void TComTrQuant::xIT( const ComponentID compID, Bool useDST, TCoeff* plCoef, Pel* pResidual, UInt uiStride, Int iWidth, Int iHeight )
1893 {
1894 #if MATRIX_MULT
1895  if( iWidth == iHeight )
1896  {
1897 #if O0043_BEST_EFFORT_DECODING
1898  xITr(g_bitDepthInStream[toChannelType(compID)], plCoef, pResidual, uiStride, (UInt)iWidth, useDST, g_maxTrDynamicRange[toChannelType(compID)]);
1899 #else
1900  xITr(g_bitDepth[toChannelType(compID)], plCoef, pResidual, uiStride, (UInt)iWidth, useDST, g_maxTrDynamicRange[toChannelType(compID)]);
1901 #endif
1902  return;
1903  }
1904 #endif
1905 
1906  TCoeff block[ MAX_TU_SIZE * MAX_TU_SIZE ];
1907  TCoeff coeff[ MAX_TU_SIZE * MAX_TU_SIZE ];
1908 
1909  memcpy(coeff, plCoef, (iWidth * iHeight * sizeof(TCoeff)));
1910 
1911 #if O0043_BEST_EFFORT_DECODING
1912  xITrMxN( g_bitDepthInStream[toChannelType(compID)], coeff, block, iWidth, iHeight, useDST, g_maxTrDynamicRange[toChannelType(compID)] );
1913 #else
1914  xITrMxN( g_bitDepth[toChannelType(compID)], coeff, block, iWidth, iHeight, useDST, g_maxTrDynamicRange[toChannelType(compID)] );
1915 #endif
1916 
1917  for (Int y = 0; y < iHeight; y++)
1918  {
1919  for (Int x = 0; x < iWidth; x++)
1920  {
1921  pResidual[(y * uiStride) + x] = Pel(block[(y * iWidth) + x]);
1922  }
1923  }
1924 }
1925 
1933 Void TComTrQuant::xTransformSkip( Pel* piBlkResi, UInt uiStride, TCoeff* psCoeff, TComTU &rTu, const ComponentID component )
1934 {
1935  const TComRectangle &rect = rTu.getRect(component);
1936  const Int width = rect.width;
1937  const Int height = rect.height;
1938 
1939  Int iTransformShift = getTransformShift(toChannelType(component), rTu.GetEquivalentLog2TrSize(component));
1940  if (rTu.getCU()->getSlice()->getSPS()->getUseExtendedPrecision())
1941  {
1942  iTransformShift = std::max<Int>(0, iTransformShift);
1943  }
1944 
1945  const Bool rotateResidual = rTu.isNonTransformedResidualRotated(component);
1946  const UInt uiSizeMinus1 = (width * height) - 1;
1947 
1948  if (iTransformShift >= 0)
1949  {
1950  for (UInt y = 0, coefficientIndex = 0; y < height; y++)
1951  {
1952  for (UInt x = 0; x < width; x++, coefficientIndex++)
1953  {
1954  psCoeff[rotateResidual ? (uiSizeMinus1 - coefficientIndex) : coefficientIndex] = TCoeff(piBlkResi[(y * uiStride) + x]) << iTransformShift;
1955  }
1956  }
1957  }
1958  else //for very high bit depths
1959  {
1960  iTransformShift = -iTransformShift;
1961  const TCoeff offset = 1 << (iTransformShift - 1);
1962 
1963  for (UInt y = 0, coefficientIndex = 0; y < height; y++)
1964  {
1965  for (UInt x = 0; x < width; x++, coefficientIndex++)
1966  {
1967  psCoeff[rotateResidual ? (uiSizeMinus1 - coefficientIndex) : coefficientIndex] = (TCoeff(piBlkResi[(y * uiStride) + x]) + offset) >> iTransformShift;
1968  }
1969  }
1970  }
1971 }
1972 
1980 Void TComTrQuant::xITransformSkip( TCoeff* plCoef, Pel* pResidual, UInt uiStride, TComTU &rTu, const ComponentID component )
1981 {
1982  const TComRectangle &rect = rTu.getRect(component);
1983  const Int width = rect.width;
1984  const Int height = rect.height;
1985 
1986  Int iTransformShift = getTransformShift(toChannelType(component), rTu.GetEquivalentLog2TrSize(component));
1987  if (rTu.getCU()->getSlice()->getSPS()->getUseExtendedPrecision())
1988  {
1989  iTransformShift = std::max<Int>(0, iTransformShift);
1990  }
1991 
1992  const Bool rotateResidual = rTu.isNonTransformedResidualRotated(component);
1993  const UInt uiSizeMinus1 = (width * height) - 1;
1994 
1995  if (iTransformShift >= 0)
1996  {
1997  const TCoeff offset = iTransformShift==0 ? 0 : (1 << (iTransformShift - 1));
1998 
1999  for (UInt y = 0, coefficientIndex = 0; y < height; y++)
2000  {
2001  for (UInt x = 0; x < width; x++, coefficientIndex++)
2002  {
2003  pResidual[(y * uiStride) + x] = Pel((plCoef[rotateResidual ? (uiSizeMinus1 - coefficientIndex) : coefficientIndex] + offset) >> iTransformShift);
2004  }
2005  }
2006  }
2007  else //for very high bit depths
2008  {
2009  iTransformShift = -iTransformShift;
2010 
2011  for (UInt y = 0, coefficientIndex = 0; y < height; y++)
2012  {
2013  for (UInt x = 0; x < width; x++, coefficientIndex++)
2014  {
2015  pResidual[(y * uiStride) + x] = Pel(plCoef[rotateResidual ? (uiSizeMinus1 - coefficientIndex) : coefficientIndex] << iTransformShift);
2016  }
2017  }
2018  }
2019 }
2020 
2034  TCoeff * plSrcCoeff,
2035  TCoeff * piDstCoeff,
2037  TCoeff * piArlDstCoeff,
2038 #endif
2039  TCoeff &uiAbsSum,
2040  const ComponentID compID,
2041  const QpParam &cQP )
2042 {
2043  const TComRectangle & rect = rTu.getRect(compID);
2044  const UInt uiWidth = rect.width;
2045  const UInt uiHeight = rect.height;
2046  TComDataCU * pcCU = rTu.getCU();
2047  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
2048  const ChannelType channelType = toChannelType(compID);
2049  const UInt uiLog2TrSize = rTu.GetEquivalentLog2TrSize(compID);
2050 
2051  const Bool extendedPrecision = pcCU->getSlice()->getSPS()->getUseExtendedPrecision();
2052 
2053  /* for 422 chroma blocks, the effective scaling applied during transformation is not a power of 2, hence it cannot be
2054  * implemented as a bit-shift (the quantised result will be sqrt(2) * larger than required). Alternatively, adjust the
2055  * uiLog2TrSize applied in iTransformShift, such that the result is 1/sqrt(2) the required result (i.e. smaller)
2056  * Then a QP+3 (sqrt(2)) or QP-3 (1/sqrt(2)) method could be used to get the required result
2057  */
2058 
2059  // Represents scaling through forward transform
2060  Int iTransformShift = getTransformShift(channelType, uiLog2TrSize);
2061  if ((pcCU->getTransformSkip(uiAbsPartIdx, compID) != 0) && pcCU->getSlice()->getSPS()->getUseExtendedPrecision())
2062  {
2063  iTransformShift = std::max<Int>(0, iTransformShift);
2064  }
2065 
2066  const Bool bUseGolombRiceParameterAdaptation = pcCU->getSlice()->getSPS()->getUseGolombRiceParameterAdaptation();
2068  UInt uiGoRiceParam = initialGolombRiceParameter;
2069  Double d64BlockUncodedCost = 0;
2070  const UInt uiLog2BlockWidth = g_aucConvertToBit[ uiWidth ] + 2;
2071  const UInt uiLog2BlockHeight = g_aucConvertToBit[ uiHeight ] + 2;
2072  const UInt uiMaxNumCoeff = uiWidth * uiHeight;
2073  assert(compID<MAX_NUM_COMPONENT);
2074 
2075  Int scalingListType = getScalingListType(pcCU->getPredictionMode(uiAbsPartIdx), compID);
2076  assert(scalingListType < SCALING_LIST_NUM);
2077 
2078 #if ADAPTIVE_QP_SELECTION
2079  memset(piArlDstCoeff, 0, sizeof(TCoeff) * uiMaxNumCoeff);
2080 #endif
2081 
2082  Double pdCostCoeff [ MAX_TU_SIZE * MAX_TU_SIZE ];
2083  Double pdCostSig [ MAX_TU_SIZE * MAX_TU_SIZE ];
2084  Double pdCostCoeff0[ MAX_TU_SIZE * MAX_TU_SIZE ];
2085  memset( pdCostCoeff, 0, sizeof(Double) * uiMaxNumCoeff );
2086  memset( pdCostSig, 0, sizeof(Double) * uiMaxNumCoeff );
2087  Int rateIncUp [ MAX_TU_SIZE * MAX_TU_SIZE ];
2088  Int rateIncDown [ MAX_TU_SIZE * MAX_TU_SIZE ];
2089  Int sigRateDelta[ MAX_TU_SIZE * MAX_TU_SIZE ];
2090  TCoeff deltaU [ MAX_TU_SIZE * MAX_TU_SIZE ];
2091  memset( rateIncUp, 0, sizeof(Int ) * uiMaxNumCoeff );
2092  memset( rateIncDown, 0, sizeof(Int ) * uiMaxNumCoeff );
2093  memset( sigRateDelta, 0, sizeof(Int ) * uiMaxNumCoeff );
2094  memset( deltaU, 0, sizeof(TCoeff) * uiMaxNumCoeff );
2095 
2096  const Int iQBits = QUANT_SHIFT + cQP.per + iTransformShift; // Right shift of non-RDOQ quantizer; level = (coeff*uiQ + offset)>>q_bits
2097  const Double *const pdErrScale = getErrScaleCoeff(scalingListType, (uiLog2TrSize-2), cQP.rem);
2098  const Int *const piQCoef = getQuantCoeff(scalingListType, cQP.rem, (uiLog2TrSize-2));
2099 
2100  const Bool enableScalingLists = getUseScalingList(uiWidth, uiHeight, (pcCU->getTransformSkip(uiAbsPartIdx, compID) != 0));
2101  const Int defaultQuantisationCoefficient = g_quantScales[cQP.rem];
2102  const Double defaultErrorScale = getErrScaleCoeffNoScalingList(scalingListType, (uiLog2TrSize-2), cQP.rem);
2103 
2104  const TCoeff entropyCodingMinimum = -(1 << g_maxTrDynamicRange[toChannelType(compID)]);
2105  const TCoeff entropyCodingMaximum = (1 << g_maxTrDynamicRange[toChannelType(compID)]) - 1;
2106 
2107 #if ADAPTIVE_QP_SELECTION
2108  Int iQBitsC = iQBits - ARL_C_PRECISION;
2109  Int iAddC = 1 << (iQBitsC-1);
2110 #endif
2111 
2112  TUEntropyCodingParameters codingParameters;
2113  getTUEntropyCodingParameters(codingParameters, rTu, compID);
2114  const UInt uiCGSize = (1 << MLS_CG_SIZE);
2115 
2116  Double pdCostCoeffGroupSig[ MLS_GRP_NUM ];
2117  UInt uiSigCoeffGroupFlag[ MLS_GRP_NUM ];
2118  Int iCGLastScanPos = -1;
2119 
2120  UInt uiCtxSet = 0;
2121  Int c1 = 1;
2122  Int c2 = 0;
2123  Double d64BaseCost = 0;
2124  Int iLastScanPos = -1;
2125 
2126  UInt c1Idx = 0;
2127  UInt c2Idx = 0;
2128  Int baseLevel;
2129 
2130  memset( pdCostCoeffGroupSig, 0, sizeof(Double) * MLS_GRP_NUM );
2131  memset( uiSigCoeffGroupFlag, 0, sizeof(UInt) * MLS_GRP_NUM );
2132 
2133  UInt uiCGNum = uiWidth * uiHeight >> MLS_CG_SIZE;
2134  Int iScanPos;
2135  coeffGroupRDStats rdStats;
2136 
2137  const UInt significanceMapContextOffset = getSignificanceMapContextOffset(compID);
2138 
2139  for (Int iCGScanPos = uiCGNum-1; iCGScanPos >= 0; iCGScanPos--)
2140  {
2141  UInt uiCGBlkPos = codingParameters.scanCG[ iCGScanPos ];
2142  UInt uiCGPosY = uiCGBlkPos / codingParameters.widthInGroups;
2143  UInt uiCGPosX = uiCGBlkPos - (uiCGPosY * codingParameters.widthInGroups);
2144 
2145  memset( &rdStats, 0, sizeof (coeffGroupRDStats));
2146 
2147  const Int patternSigCtx = TComTrQuant::calcPatternSigCtx(uiSigCoeffGroupFlag, uiCGPosX, uiCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups);
2148 
2149  for (Int iScanPosinCG = uiCGSize-1; iScanPosinCG >= 0; iScanPosinCG--)
2150  {
2151  iScanPos = iCGScanPos*uiCGSize + iScanPosinCG;
2152  //===== quantization =====
2153  UInt uiBlkPos = codingParameters.scan[iScanPos];
2154  // set coeff
2155 
2156  const Int quantisationCoefficient = (enableScalingLists) ? piQCoef [uiBlkPos] : defaultQuantisationCoefficient;
2157  const Double errorScale = (enableScalingLists) ? pdErrScale[uiBlkPos] : defaultErrorScale;
2158 
2159  const Int64 tmpLevel = Int64(abs(plSrcCoeff[ uiBlkPos ])) * quantisationCoefficient;
2160 
2161  const Intermediate_Int lLevelDouble = (Intermediate_Int)min<Int64>(tmpLevel, MAX_INTERMEDIATE_INT - (Intermediate_Int(1) << (iQBits - 1)));
2162 
2163 #if ADAPTIVE_QP_SELECTION
2164  if( m_bUseAdaptQpSelect )
2165  {
2166  piArlDstCoeff[uiBlkPos] = (TCoeff)(( lLevelDouble + iAddC) >> iQBitsC );
2167  }
2168 #endif
2169  const UInt uiMaxAbsLevel = std::min<UInt>(UInt(entropyCodingMaximum), UInt((lLevelDouble + (Intermediate_Int(1) << (iQBits - 1))) >> iQBits));
2170 
2171  const Double dErr = Double( lLevelDouble );
2172  pdCostCoeff0[ iScanPos ] = dErr * dErr * errorScale;
2173  d64BlockUncodedCost += pdCostCoeff0[ iScanPos ];
2174  piDstCoeff[ uiBlkPos ] = uiMaxAbsLevel;
2175 
2176  if ( uiMaxAbsLevel > 0 && iLastScanPos < 0 )
2177  {
2178  iLastScanPos = iScanPos;
2179  uiCtxSet = getContextSetIndex(compID, (iScanPos >> MLS_CG_SIZE), 0);
2180  iCGLastScanPos = iCGScanPos;
2181  }
2182 
2183  if ( iLastScanPos >= 0 )
2184  {
2185  //===== coefficient level estimation =====
2186  UInt uiLevel;
2187  UInt uiOneCtx = (NUM_ONE_FLAG_CTX_PER_SET * uiCtxSet) + c1;
2188  UInt uiAbsCtx = (NUM_ABS_FLAG_CTX_PER_SET * uiCtxSet) + c2;
2189 
2190  if( iScanPos == iLastScanPos )
2191  {
2192  uiLevel = xGetCodedLevel( pdCostCoeff[ iScanPos ], pdCostCoeff0[ iScanPos ], pdCostSig[ iScanPos ],
2193  lLevelDouble, uiMaxAbsLevel, significanceMapContextOffset, uiOneCtx, uiAbsCtx, uiGoRiceParam,
2194  c1Idx, c2Idx, iQBits, errorScale, 1, extendedPrecision, channelType
2195  );
2196  }
2197  else
2198  {
2199  UShort uiCtxSig = significanceMapContextOffset + getSigCtxInc( patternSigCtx, codingParameters, iScanPos, uiLog2BlockWidth, uiLog2BlockHeight, channelType );
2200 
2201  uiLevel = xGetCodedLevel( pdCostCoeff[ iScanPos ], pdCostCoeff0[ iScanPos ], pdCostSig[ iScanPos ],
2202  lLevelDouble, uiMaxAbsLevel, uiCtxSig, uiOneCtx, uiAbsCtx, uiGoRiceParam,
2203  c1Idx, c2Idx, iQBits, errorScale, 0, extendedPrecision, channelType
2204  );
2205 
2206  sigRateDelta[ uiBlkPos ] = m_pcEstBitsSbac->significantBits[ uiCtxSig ][ 1 ] - m_pcEstBitsSbac->significantBits[ uiCtxSig ][ 0 ];
2207  }
2208 
2209  deltaU[ uiBlkPos ] = TCoeff((lLevelDouble - (Intermediate_Int(uiLevel) << iQBits)) >> (iQBits-8));
2210 
2211  if( uiLevel > 0 )
2212  {
2213  Int rateNow = xGetICRate( uiLevel, uiOneCtx, uiAbsCtx, uiGoRiceParam, c1Idx, c2Idx, extendedPrecision, channelType );
2214  rateIncUp [ uiBlkPos ] = xGetICRate( uiLevel+1, uiOneCtx, uiAbsCtx, uiGoRiceParam, c1Idx, c2Idx, extendedPrecision, channelType ) - rateNow;
2215  rateIncDown [ uiBlkPos ] = xGetICRate( uiLevel-1, uiOneCtx, uiAbsCtx, uiGoRiceParam, c1Idx, c2Idx, extendedPrecision, channelType ) - rateNow;
2216  }
2217  else // uiLevel == 0
2218  {
2219  rateIncUp [ uiBlkPos ] = m_pcEstBitsSbac->m_greaterOneBits[ uiOneCtx ][ 0 ];
2220  }
2221  piDstCoeff[ uiBlkPos ] = uiLevel;
2222  d64BaseCost += pdCostCoeff [ iScanPos ];
2223 
2224  baseLevel = (c1Idx < C1FLAG_NUMBER) ? (2 + (c2Idx < C2FLAG_NUMBER)) : 1;
2225  if( uiLevel >= baseLevel )
2226  {
2227  if (uiLevel > 3*(1<<uiGoRiceParam))
2228  {
2229  uiGoRiceParam = bUseGolombRiceParameterAdaptation ? (uiGoRiceParam + 1) : (std::min<UInt>((uiGoRiceParam + 1), 4));
2230  }
2231  }
2232  if ( uiLevel >= 1)
2233  {
2234  c1Idx ++;
2235  }
2236 
2237  //===== update bin model =====
2238  if( uiLevel > 1 )
2239  {
2240  c1 = 0;
2241  c2 += (c2 < 2);
2242  c2Idx ++;
2243  }
2244  else if( (c1 < 3) && (c1 > 0) && uiLevel)
2245  {
2246  c1++;
2247  }
2248 
2249  //===== context set update =====
2250  if( ( iScanPos % uiCGSize == 0 ) && ( iScanPos > 0 ) )
2251  {
2252  uiCtxSet = getContextSetIndex(compID, ((iScanPos - 1) >> MLS_CG_SIZE), (c1 == 0)); //(iScanPos - 1) because we do this **before** entering the final group
2253  c1 = 1;
2254  c2 = 0;
2255  c1Idx = 0;
2256  c2Idx = 0;
2257  uiGoRiceParam = initialGolombRiceParameter;
2258  }
2259  }
2260  else
2261  {
2262  d64BaseCost += pdCostCoeff0[ iScanPos ];
2263  }
2264  rdStats.d64SigCost += pdCostSig[ iScanPos ];
2265  if (iScanPosinCG == 0 )
2266  {
2267  rdStats.d64SigCost_0 = pdCostSig[ iScanPos ];
2268  }
2269  if (piDstCoeff[ uiBlkPos ] )
2270  {
2271  uiSigCoeffGroupFlag[ uiCGBlkPos ] = 1;
2272  rdStats.d64CodedLevelandDist += pdCostCoeff[ iScanPos ] - pdCostSig[ iScanPos ];
2273  rdStats.d64UncodedDist += pdCostCoeff0[ iScanPos ];
2274  if ( iScanPosinCG != 0 )
2275  {
2276  rdStats.iNNZbeforePos0++;
2277  }
2278  }
2279  } //end for (iScanPosinCG)
2280 
2281  if (iCGLastScanPos >= 0)
2282  {
2283  if( iCGScanPos )
2284  {
2285  if (uiSigCoeffGroupFlag[ uiCGBlkPos ] == 0)
2286  {
2287  UInt uiCtxSig = getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, uiCGPosX, uiCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups );
2288  d64BaseCost += xGetRateSigCoeffGroup(0, uiCtxSig) - rdStats.d64SigCost;;
2289  pdCostCoeffGroupSig[ iCGScanPos ] = xGetRateSigCoeffGroup(0, uiCtxSig);
2290  }
2291  else
2292  {
2293  if (iCGScanPos < iCGLastScanPos) //skip the last coefficient group, which will be handled together with last position below.
2294  {
2295  if ( rdStats.iNNZbeforePos0 == 0 )
2296  {
2297  d64BaseCost -= rdStats.d64SigCost_0;
2298  rdStats.d64SigCost -= rdStats.d64SigCost_0;
2299  }
2300  // rd-cost if SigCoeffGroupFlag = 0, initialization
2301  Double d64CostZeroCG = d64BaseCost;
2302 
2303  // add SigCoeffGroupFlag cost to total cost
2304  UInt uiCtxSig = getSigCoeffGroupCtxInc( uiSigCoeffGroupFlag, uiCGPosX, uiCGPosY, codingParameters.widthInGroups, codingParameters.heightInGroups );
2305 
2306  if (iCGScanPos < iCGLastScanPos)
2307  {
2308  d64BaseCost += xGetRateSigCoeffGroup(1, uiCtxSig);
2309  d64CostZeroCG += xGetRateSigCoeffGroup(0, uiCtxSig);
2310  pdCostCoeffGroupSig[ iCGScanPos ] = xGetRateSigCoeffGroup(1, uiCtxSig);
2311  }
2312 
2313  // try to convert the current coeff group from non-zero to all-zero
2314  d64CostZeroCG += rdStats.d64UncodedDist; // distortion for resetting non-zero levels to zero levels
2315  d64CostZeroCG -= rdStats.d64CodedLevelandDist; // distortion and level cost for keeping all non-zero levels
2316  d64CostZeroCG -= rdStats.d64SigCost; // sig cost for all coeffs, including zero levels and non-zerl levels
2317 
2318  // if we can save cost, change this block to all-zero block
2319  if ( d64CostZeroCG < d64BaseCost )
2320  {
2321  uiSigCoeffGroupFlag[ uiCGBlkPos ] = 0;
2322  d64BaseCost = d64CostZeroCG;
2323  if (iCGScanPos < iCGLastScanPos)
2324  {
2325  pdCostCoeffGroupSig[ iCGScanPos ] = xGetRateSigCoeffGroup(0, uiCtxSig);
2326  }
2327  // reset coeffs to 0 in this block
2328  for (Int iScanPosinCG = uiCGSize-1; iScanPosinCG >= 0; iScanPosinCG--)
2329  {
2330  iScanPos = iCGScanPos*uiCGSize + iScanPosinCG;
2331  UInt uiBlkPos = codingParameters.scan[ iScanPos ];
2332 
2333  if (piDstCoeff[ uiBlkPos ])
2334  {
2335  piDstCoeff [ uiBlkPos ] = 0;
2336  pdCostCoeff[ iScanPos ] = pdCostCoeff0[ iScanPos ];
2337  pdCostSig [ iScanPos ] = 0;
2338  }
2339  }
2340  } // end if ( d64CostAllZeros < d64BaseCost )
2341  }
2342  } // end if if (uiSigCoeffGroupFlag[ uiCGBlkPos ] == 0)
2343  }
2344  else
2345  {
2346  uiSigCoeffGroupFlag[ uiCGBlkPos ] = 1;
2347  }
2348  }
2349  } //end for (iCGScanPos)
2350 
2351  //===== estimate last position =====
2352  if ( iLastScanPos < 0 )
2353  {
2354  return;
2355  }
2356 
2357  Double d64BestCost = 0;
2358  Int ui16CtxCbf = 0;
2359  Int iBestLastIdxP1 = 0;
2360  if( !pcCU->isIntra( uiAbsPartIdx ) && isLuma(compID) && pcCU->getTransformIdx( uiAbsPartIdx ) == 0 )
2361  {
2362  ui16CtxCbf = 0;
2363  d64BestCost = d64BlockUncodedCost + xGetICost( m_pcEstBitsSbac->blockRootCbpBits[ ui16CtxCbf ][ 0 ] );
2364  d64BaseCost += xGetICost( m_pcEstBitsSbac->blockRootCbpBits[ ui16CtxCbf ][ 1 ] );
2365  }
2366  else
2367  {
2368  ui16CtxCbf = pcCU->getCtxQtCbf( rTu, channelType );
2369  ui16CtxCbf += getCBFContextOffset(compID);
2370  d64BestCost = d64BlockUncodedCost + xGetICost( m_pcEstBitsSbac->blockCbpBits[ ui16CtxCbf ][ 0 ] );
2371  d64BaseCost += xGetICost( m_pcEstBitsSbac->blockCbpBits[ ui16CtxCbf ][ 1 ] );
2372  }
2373 
2374 
2375  Bool bFoundLast = false;
2376  for (Int iCGScanPos = iCGLastScanPos; iCGScanPos >= 0; iCGScanPos--)
2377  {
2378  UInt uiCGBlkPos = codingParameters.scanCG[ iCGScanPos ];
2379 
2380  d64BaseCost -= pdCostCoeffGroupSig [ iCGScanPos ];
2381  if (uiSigCoeffGroupFlag[ uiCGBlkPos ])
2382  {
2383  for (Int iScanPosinCG = uiCGSize-1; iScanPosinCG >= 0; iScanPosinCG--)
2384  {
2385  iScanPos = iCGScanPos*uiCGSize + iScanPosinCG;
2386 
2387  if (iScanPos > iLastScanPos)
2388  {
2389  continue;
2390  }
2391  UInt uiBlkPos = codingParameters.scan[iScanPos];
2392 
2393  if( piDstCoeff[ uiBlkPos ] )
2394  {
2395  UInt uiPosY = uiBlkPos >> uiLog2BlockWidth;
2396  UInt uiPosX = uiBlkPos - ( uiPosY << uiLog2BlockWidth );
2397 
2398  Double d64CostLast= codingParameters.scanType == SCAN_VER ? xGetRateLast( uiPosY, uiPosX, compID ) : xGetRateLast( uiPosX, uiPosY, compID );
2399  Double totalCost = d64BaseCost + d64CostLast - pdCostSig[ iScanPos ];
2400 
2401  if( totalCost < d64BestCost )
2402  {
2403  iBestLastIdxP1 = iScanPos + 1;
2404  d64BestCost = totalCost;
2405  }
2406  if( piDstCoeff[ uiBlkPos ] > 1 )
2407  {
2408  bFoundLast = true;
2409  break;
2410  }
2411  d64BaseCost -= pdCostCoeff[ iScanPos ];
2412  d64BaseCost += pdCostCoeff0[ iScanPos ];
2413  }
2414  else
2415  {
2416  d64BaseCost -= pdCostSig[ iScanPos ];
2417  }
2418  } //end for
2419  if (bFoundLast)
2420  {
2421  break;
2422  }
2423  } // end if (uiSigCoeffGroupFlag[ uiCGBlkPos ])
2424  } // end for
2425 
2426 
2427  for ( Int scanPos = 0; scanPos < iBestLastIdxP1; scanPos++ )
2428  {
2429  Int blkPos = codingParameters.scan[ scanPos ];
2430  TCoeff level = piDstCoeff[ blkPos ];
2431  uiAbsSum += level;
2432  piDstCoeff[ blkPos ] = ( plSrcCoeff[ blkPos ] < 0 ) ? -level : level;
2433  }
2434 
2435  //===== clean uncoded coefficients =====
2436  for ( Int scanPos = iBestLastIdxP1; scanPos <= iLastScanPos; scanPos++ )
2437  {
2438  piDstCoeff[ codingParameters.scan[ scanPos ] ] = 0;
2439  }
2440 
2441 
2442  if( pcCU->getSlice()->getPPS()->getSignHideFlag() && uiAbsSum>=2)
2443  {
2444  const Double inverseQuantScale = Double(g_invQuantScales[cQP.rem]);
2445  Int64 rdFactor = (Int64)(inverseQuantScale * inverseQuantScale * (1 << (2 * cQP.per))
2446  / m_dLambda / 16 / (1 << (2 * DISTORTION_PRECISION_ADJUSTMENT(g_bitDepth[channelType] - 8)))
2447  + 0.5);
2448 
2449  Int lastCG = -1;
2450  Int absSum = 0 ;
2451  Int n ;
2452 
2453  for( Int subSet = (uiWidth*uiHeight-1) >> MLS_CG_SIZE; subSet >= 0; subSet-- )
2454  {
2455  Int subPos = subSet << MLS_CG_SIZE;
2456  Int firstNZPosInCG=uiCGSize , lastNZPosInCG=-1 ;
2457  absSum = 0 ;
2458 
2459  for(n = uiCGSize-1; n >= 0; --n )
2460  {
2461  if( piDstCoeff[ codingParameters.scan[ n + subPos ]] )
2462  {
2463  lastNZPosInCG = n;
2464  break;
2465  }
2466  }
2467 
2468  for(n = 0; n <uiCGSize; n++ )
2469  {
2470  if( piDstCoeff[ codingParameters.scan[ n + subPos ]] )
2471  {
2472  firstNZPosInCG = n;
2473  break;
2474  }
2475  }
2476 
2477  for(n = firstNZPosInCG; n <=lastNZPosInCG; n++ )
2478  {
2479  absSum += Int(piDstCoeff[ codingParameters.scan[ n + subPos ]]);
2480  }
2481 
2482  if(lastNZPosInCG>=0 && lastCG==-1)
2483  {
2484  lastCG = 1;
2485  }
2486 
2487  if( lastNZPosInCG-firstNZPosInCG>=SBH_THRESHOLD )
2488  {
2489  UInt signbit = (piDstCoeff[codingParameters.scan[subPos+firstNZPosInCG]]>0?0:1);
2490  if( signbit!=(absSum&0x1) ) // hide but need tune
2491  {
2492  // calculate the cost
2493  Int64 minCostInc = MAX_INT64, curCost = MAX_INT64;
2494  Int minPos = -1, finalChange = 0, curChange = 0;
2495 
2496  for( n = (lastCG==1?lastNZPosInCG:uiCGSize-1) ; n >= 0; --n )
2497  {
2498  UInt uiBlkPos = codingParameters.scan[ n + subPos ];
2499  if(piDstCoeff[ uiBlkPos ] != 0 )
2500  {
2501  Int64 costUp = rdFactor * ( - deltaU[uiBlkPos] ) + rateIncUp[uiBlkPos];
2502  Int64 costDown = rdFactor * ( deltaU[uiBlkPos] ) + rateIncDown[uiBlkPos]
2503  - ((abs(piDstCoeff[uiBlkPos]) == 1) ? sigRateDelta[uiBlkPos] : 0);
2504 
2505  if(lastCG==1 && lastNZPosInCG==n && abs(piDstCoeff[uiBlkPos])==1)
2506  {
2507  costDown -= (4<<15);
2508  }
2509 
2510  if(costUp<costDown)
2511  {
2512  curCost = costUp;
2513  curChange = 1;
2514  }
2515  else
2516  {
2517  curChange = -1;
2518  if(n==firstNZPosInCG && abs(piDstCoeff[uiBlkPos])==1)
2519  {
2520  curCost = MAX_INT64;
2521  }
2522  else
2523  {
2524  curCost = costDown;
2525  }
2526  }
2527  }
2528  else
2529  {
2530  curCost = rdFactor * ( - (abs(deltaU[uiBlkPos])) ) + (1<<15) + rateIncUp[uiBlkPos] + sigRateDelta[uiBlkPos] ;
2531  curChange = 1 ;
2532 
2533  if(n<firstNZPosInCG)
2534  {
2535  UInt thissignbit = (plSrcCoeff[uiBlkPos]>=0?0:1);
2536  if(thissignbit != signbit )
2537  {
2538  curCost = MAX_INT64;
2539  }
2540  }
2541  }
2542 
2543  if( curCost<minCostInc)
2544  {
2545  minCostInc = curCost;
2546  finalChange = curChange;
2547  minPos = uiBlkPos;
2548  }
2549  }
2550 
2551  if(piDstCoeff[minPos] == entropyCodingMaximum || piDstCoeff[minPos] == entropyCodingMinimum)
2552  {
2553  finalChange = -1;
2554  }
2555 
2556  if(plSrcCoeff[minPos]>=0)
2557  {
2558  piDstCoeff[minPos] += finalChange ;
2559  }
2560  else
2561  {
2562  piDstCoeff[minPos] -= finalChange ;
2563  }
2564  }
2565  }
2566 
2567  if(lastCG==1)
2568  {
2569  lastCG=0 ;
2570  }
2571  }
2572  }
2573 }
2574 
2575 
2584 Int TComTrQuant::calcPatternSigCtx( const UInt* sigCoeffGroupFlag, UInt uiCGPosX, UInt uiCGPosY, UInt widthInGroups, UInt heightInGroups )
2585 {
2586  if ((widthInGroups <= 1) && (heightInGroups <= 1))
2587  {
2588  return 0;
2589  }
2590 
2591  const Bool rightAvailable = uiCGPosX < (widthInGroups - 1);
2592  const Bool belowAvailable = uiCGPosY < (heightInGroups - 1);
2593 
2594  UInt sigRight = 0;
2595  UInt sigLower = 0;
2596 
2597  if (rightAvailable)
2598  {
2599  sigRight = ((sigCoeffGroupFlag[ (uiCGPosY * widthInGroups) + uiCGPosX + 1 ] != 0) ? 1 : 0);
2600  }
2601  if (belowAvailable)
2602  {
2603  sigLower = ((sigCoeffGroupFlag[ (uiCGPosY + 1) * widthInGroups + uiCGPosX ] != 0) ? 1 : 0);
2604  }
2605 
2606  return sigRight + (sigLower << 1);
2607 }
2608 
2609 
2620  const TUEntropyCodingParameters &codingParameters,
2621  const Int scanPosition,
2622  const Int log2BlockWidth,
2623  const Int log2BlockHeight,
2624  const ChannelType chanType)
2625 {
2627  {
2628  //single context mode
2630  }
2631 
2632  const UInt rasterPosition = codingParameters.scan[scanPosition];
2633  const UInt posY = rasterPosition >> log2BlockWidth;
2634  const UInt posX = rasterPosition - (posY << log2BlockWidth);
2635 
2636  if ((posX + posY) == 0)
2637  {
2638  return 0; //special case for the DC context variable
2639  }
2640 
2641  Int offset = MAX_INT;
2642 
2643  if ((log2BlockWidth == 2) && (log2BlockHeight == 2)) //4x4
2644  {
2645  offset = ctxIndMap4x4[ (4 * posY) + posX ];
2646  }
2647  else
2648  {
2649  Int cnt = 0;
2650 
2651  switch (patternSigCtx)
2652  {
2653  //------------------
2654 
2655  case 0: //neither neighbouring group is significant
2656  {
2657  const Int posXinSubset = posX & ((1 << MLS_CG_LOG2_WIDTH) - 1);
2658  const Int posYinSubset = posY & ((1 << MLS_CG_LOG2_HEIGHT) - 1);
2659  const Int posTotalInSubset = posXinSubset + posYinSubset;
2660 
2661  //first N coefficients in scan order use 2; the next few use 1; the rest use 0.
2662  const UInt context1Threshold = NEIGHBOURHOOD_00_CONTEXT_1_THRESHOLD_4x4;
2663  const UInt context2Threshold = NEIGHBOURHOOD_00_CONTEXT_2_THRESHOLD_4x4;
2664 
2665  cnt = (posTotalInSubset >= context1Threshold) ? 0 : ((posTotalInSubset >= context2Threshold) ? 1 : 2);
2666  }
2667  break;
2668 
2669  //------------------
2670 
2671  case 1: //right group is significant, below is not
2672  {
2673  const Int posYinSubset = posY & ((1 << MLS_CG_LOG2_HEIGHT) - 1);
2674  const Int groupHeight = 1 << MLS_CG_LOG2_HEIGHT;
2675 
2676  cnt = (posYinSubset >= (groupHeight >> 1)) ? 0 : ((posYinSubset >= (groupHeight >> 2)) ? 1 : 2); //top quarter uses 2; second-from-top quarter uses 1; bottom half uses 0
2677  }
2678  break;
2679 
2680  //------------------
2681 
2682  case 2: //below group is significant, right is not
2683  {
2684  const Int posXinSubset = posX & ((1 << MLS_CG_LOG2_WIDTH) - 1);
2685  const Int groupWidth = 1 << MLS_CG_LOG2_WIDTH;
2686 
2687  cnt = (posXinSubset >= (groupWidth >> 1)) ? 0 : ((posXinSubset >= (groupWidth >> 2)) ? 1 : 2); //left quarter uses 2; second-from-left quarter uses 1; right half uses 0
2688  }
2689  break;
2690 
2691  //------------------
2692 
2693  case 3: //both neighbouring groups are significant
2694  {
2695  cnt = 2;
2696  }
2697  break;
2698 
2699  //------------------
2700 
2701  default:
2702  std::cerr << "ERROR: Invalid patternSigCtx \"" << Int(patternSigCtx) << "\" in getSigCtxInc" << std::endl;
2703  exit(1);
2704  break;
2705  }
2706 
2707  //------------------------------------------------
2708 
2709  const Bool notFirstGroup = ((posX >> MLS_CG_LOG2_WIDTH) + (posY >> MLS_CG_LOG2_HEIGHT)) > 0;
2710 
2711  offset = (notFirstGroup ? notFirstGroupNeighbourhoodContextOffset[chanType] : 0) + cnt;
2712  }
2713 
2714  return codingParameters.firstSignificanceMapContext + offset;
2715 }
2716 
2717 
2738 __inline UInt TComTrQuant::xGetCodedLevel ( Double& rd64CodedCost,
2739  Double& rd64CodedCost0,
2740  Double& rd64CodedCostSig,
2741  Intermediate_Int lLevelDouble,
2742  UInt uiMaxAbsLevel,
2743  UShort ui16CtxNumSig,
2744  UShort ui16CtxNumOne,
2745  UShort ui16CtxNumAbs,
2746  UShort ui16AbsGoRice,
2747  UInt c1Idx,
2748  UInt c2Idx,
2749  Int iQBits,
2750  Double errorScale,
2751  Bool bLast,
2752  Bool useLimitedPrefixLength,
2753  ChannelType channelType
2754  ) const
2755 {
2756  Double dCurrCostSig = 0;
2757  UInt uiBestAbsLevel = 0;
2758 
2759  if( !bLast && uiMaxAbsLevel < 3 )
2760  {
2761  rd64CodedCostSig = xGetRateSigCoef( 0, ui16CtxNumSig );
2762  rd64CodedCost = rd64CodedCost0 + rd64CodedCostSig;
2763  if( uiMaxAbsLevel == 0 )
2764  {
2765  return uiBestAbsLevel;
2766  }
2767  }
2768  else
2769  {
2770  rd64CodedCost = MAX_DOUBLE;
2771  }
2772 
2773  if( !bLast )
2774  {
2775  dCurrCostSig = xGetRateSigCoef( 1, ui16CtxNumSig );
2776  }
2777 
2778  UInt uiMinAbsLevel = ( uiMaxAbsLevel > 1 ? uiMaxAbsLevel - 1 : 1 );
2779  for( Int uiAbsLevel = uiMaxAbsLevel; uiAbsLevel >= uiMinAbsLevel ; uiAbsLevel-- )
2780  {
2781  Double dErr = Double( lLevelDouble - ( Intermediate_Int(uiAbsLevel) << iQBits ) );
2782  Double dCurrCost = dErr * dErr * errorScale + xGetICost( xGetICRate( uiAbsLevel, ui16CtxNumOne, ui16CtxNumAbs, ui16AbsGoRice, c1Idx, c2Idx, useLimitedPrefixLength, channelType ) );
2783  dCurrCost += dCurrCostSig;
2784 
2785  if( dCurrCost < rd64CodedCost )
2786  {
2787  uiBestAbsLevel = uiAbsLevel;
2788  rd64CodedCost = dCurrCost;
2789  rd64CodedCostSig = dCurrCostSig;
2790  }
2791  }
2792 
2793  return uiBestAbsLevel;
2794 }
2795 
2807 __inline Int TComTrQuant::xGetICRate ( UInt uiAbsLevel,
2808  UShort ui16CtxNumOne,
2809  UShort ui16CtxNumAbs,
2810  UShort ui16AbsGoRice,
2811  UInt c1Idx,
2812  UInt c2Idx,
2813  Bool useLimitedPrefixLength,
2814  ChannelType channelType
2815  ) const
2816 {
2817  Int iRate = Int(xGetIEPRate()); // cost of sign bit
2818  UInt baseLevel = (c1Idx < C1FLAG_NUMBER) ? (2 + (c2Idx < C2FLAG_NUMBER)) : 1;
2819 
2820  if ( uiAbsLevel >= baseLevel )
2821  {
2822  UInt symbol = uiAbsLevel - baseLevel;
2823  UInt length;
2824  if (symbol < (COEF_REMAIN_BIN_REDUCTION << ui16AbsGoRice))
2825  {
2826  length = symbol>>ui16AbsGoRice;
2827  iRate += (length+1+ui16AbsGoRice)<< 15;
2828  }
2829  else if (useLimitedPrefixLength)
2830  {
2831  const UInt maximumPrefixLength = (32 - (COEF_REMAIN_BIN_REDUCTION + g_maxTrDynamicRange[channelType]));
2832 
2833  UInt prefixLength = 0;
2834  UInt suffix = (symbol >> ui16AbsGoRice) - COEF_REMAIN_BIN_REDUCTION;
2835 
2836  while ((prefixLength < maximumPrefixLength) && (suffix > ((2 << prefixLength) - 2)))
2837  {
2838  prefixLength++;
2839  }
2840 
2841  const UInt suffixLength = (prefixLength == maximumPrefixLength) ? (g_maxTrDynamicRange[channelType] - ui16AbsGoRice) : (prefixLength + 1/*separator*/);
2842 
2843  iRate += (COEF_REMAIN_BIN_REDUCTION + prefixLength + suffixLength + ui16AbsGoRice) << 15;
2844  }
2845  else
2846  {
2847  length = ui16AbsGoRice;
2848  symbol = symbol - ( COEF_REMAIN_BIN_REDUCTION << ui16AbsGoRice);
2849  while (symbol >= (1<<length))
2850  {
2851  symbol -= (1<<(length++));
2852  }
2853  iRate += (COEF_REMAIN_BIN_REDUCTION+length+1-ui16AbsGoRice+length)<< 15;
2854  }
2855 
2856  if (c1Idx < C1FLAG_NUMBER)
2857  {
2858  iRate += m_pcEstBitsSbac->m_greaterOneBits[ ui16CtxNumOne ][ 1 ];
2859 
2860  if (c2Idx < C2FLAG_NUMBER)
2861  {
2862  iRate += m_pcEstBitsSbac->m_levelAbsBits[ ui16CtxNumAbs ][ 1 ];
2863  }
2864  }
2865  }
2866  else if( uiAbsLevel == 1 )
2867  {
2868  iRate += m_pcEstBitsSbac->m_greaterOneBits[ ui16CtxNumOne ][ 0 ];
2869  }
2870  else if( uiAbsLevel == 2 )
2871  {
2872  iRate += m_pcEstBitsSbac->m_greaterOneBits[ ui16CtxNumOne ][ 1 ];
2873  iRate += m_pcEstBitsSbac->m_levelAbsBits[ ui16CtxNumAbs ][ 0 ];
2874  }
2875  else
2876  {
2877  iRate = 0;
2878  }
2879 
2880  return iRate;
2881 }
2882 
2883 __inline Double TComTrQuant::xGetRateSigCoeffGroup ( UShort uiSignificanceCoeffGroup,
2884  UShort ui16CtxNumSig ) const
2885 {
2886  return xGetICost( m_pcEstBitsSbac->significantCoeffGroupBits[ ui16CtxNumSig ][ uiSignificanceCoeffGroup ] );
2887 }
2888 
2895 /*
2896  * \param uiWidth width of the transform unit (TU)
2897 */
2898 __inline Double TComTrQuant::xGetRateLast ( const UInt uiPosX,
2899  const UInt uiPosY,
2900  const ComponentID component ) const
2901 {
2902  UInt uiCtxX = g_uiGroupIdx[uiPosX];
2903  UInt uiCtxY = g_uiGroupIdx[uiPosY];
2904 
2905  Double uiCost = m_pcEstBitsSbac->lastXBits[toChannelType(component)][ uiCtxX ] + m_pcEstBitsSbac->lastYBits[toChannelType(component)][ uiCtxY ];
2906 
2907  if( uiCtxX > 3 )
2908  {
2909  uiCost += xGetIEPRate() * ((uiCtxX-2)>>1);
2910  }
2911  if( uiCtxY > 3 )
2912  {
2913  uiCost += xGetIEPRate() * ((uiCtxY-2)>>1);
2914  }
2915  return xGetICost( uiCost );
2916 }
2917 
2918 __inline Double TComTrQuant::xGetRateSigCoef ( UShort uiSignificance,
2919  UShort ui16CtxNumSig ) const
2920 {
2921  return xGetICost( m_pcEstBitsSbac->significantBits[ ui16CtxNumSig ][ uiSignificance ] );
2922 }
2923 
2928 __inline Double TComTrQuant::xGetICost ( Double dRate ) const
2929 {
2930  return m_dLambda * dRate;
2931 }
2932 
2937 {
2938  return 32768;
2939 }
2940 
2949 UInt TComTrQuant::getSigCoeffGroupCtxInc (const UInt* uiSigCoeffGroupFlag,
2950  const UInt uiCGPosX,
2951  const UInt uiCGPosY,
2952  const UInt widthInGroups,
2953  const UInt heightInGroups)
2954 {
2955  UInt sigRight = 0;
2956  UInt sigLower = 0;
2957 
2958  if (uiCGPosX < (widthInGroups - 1))
2959  {
2960  sigRight = ((uiSigCoeffGroupFlag[ (uiCGPosY * widthInGroups) + uiCGPosX + 1 ] != 0) ? 1 : 0);
2961  }
2962  if (uiCGPosY < (heightInGroups - 1))
2963  {
2964  sigLower = ((uiSigCoeffGroupFlag[ (uiCGPosY + 1) * widthInGroups + uiCGPosX ] != 0) ? 1 : 0);
2965  }
2966 
2967  return ((sigRight + sigLower) != 0) ? 1 : 0;
2968 }
2969 
2970 
2976 {
2977  const Int minimumQp = 0;
2978  const Int maximumQp = SCALING_LIST_REM_NUM;
2979 
2980  for(UInt size = 0; size < SCALING_LIST_SIZE_NUM; size++)
2981  {
2982  for(UInt list = 0; list < SCALING_LIST_NUM; list++)
2983  {
2984  for(Int qp = minimumQp; qp < maximumQp; qp++)
2985  {
2986  xSetScalingListEnc(scalingList,list,size,qp,format);
2987  xSetScalingListDec(*scalingList,list,size,qp,format);
2988  setErrScaleCoeff(list,size,qp);
2989  }
2990  }
2991  }
2992 }
2998 {
2999  const Int minimumQp = 0;
3000  const Int maximumQp = SCALING_LIST_REM_NUM;
3001 
3002  for(UInt size = 0; size < SCALING_LIST_SIZE_NUM; size++)
3003  {
3004  for(UInt list = 0; list < SCALING_LIST_NUM; list++)
3005  {
3006  for(Int qp = minimumQp; qp < maximumQp; qp++)
3007  {
3008  xSetScalingListDec(scalingList,list,size,qp,format);
3009  }
3010  }
3011  }
3012 }
3019 {
3020  const UInt uiLog2TrSize = g_aucConvertToBit[ g_scalingListSizeX[size] ] + 2;
3021  const ChannelType channelType = ((list == 0) || (list == MAX_NUM_COMPONENT)) ? CHANNEL_TYPE_LUMA : CHANNEL_TYPE_CHROMA;
3022 
3023  const Int iTransformShift = getTransformShift(channelType, uiLog2TrSize); // Represents scaling through forward transform
3024 
3025  UInt i,uiMaxNumCoeff = g_scalingListSize[size];
3026  Int *piQuantcoeff;
3027  Double *pdErrScale;
3028  piQuantcoeff = getQuantCoeff(list, qp,size);
3029  pdErrScale = getErrScaleCoeff(list, size, qp);
3030 
3031  Double dErrScale = (Double)(1<<SCALE_BITS); // Compensate for scaling of bitcount in Lagrange cost function
3032  dErrScale = dErrScale*pow(2.0,(-2.0*iTransformShift)); // Compensate for scaling through forward transform
3033 
3034  for(i=0;i<uiMaxNumCoeff;i++)
3035  {
3036  pdErrScale[i] = dErrScale / piQuantcoeff[i] / piQuantcoeff[i] / (1 << DISTORTION_PRECISION_ADJUSTMENT(2 * (g_bitDepth[channelType] - 8)));
3037  }
3038 
3039  getErrScaleCoeffNoScalingList(list, size, qp) = dErrScale / g_quantScales[qp] / g_quantScales[qp] / (1 << DISTORTION_PRECISION_ADJUSTMENT(2 * (g_bitDepth[channelType] - 8)));
3040 }
3041 
3049 Void TComTrQuant::xSetScalingListEnc(TComScalingList *scalingList, UInt listId, UInt sizeId, Int qp, const ChromaFormat format)
3050 {
3051  UInt width = g_scalingListSizeX[sizeId];
3052  UInt height = g_scalingListSizeX[sizeId];
3053  UInt ratio = g_scalingListSizeX[sizeId]/min(MAX_MATRIX_SIZE_NUM,(Int)g_scalingListSizeX[sizeId]);
3054  Int *quantcoeff;
3055  Int *coeff = scalingList->getScalingListAddress(sizeId,listId);
3056  quantcoeff = getQuantCoeff(listId, qp, sizeId);
3057 
3058  Int quantScales = g_quantScales[qp];
3059 
3060  processScalingListEnc(coeff,
3061  quantcoeff,
3062  (quantScales << LOG2_SCALING_LIST_NEUTRAL_VALUE),
3063  height, width, ratio,
3065  scalingList->getScalingListDC(sizeId,listId));
3066 }
3067 
3075 Void TComTrQuant::xSetScalingListDec(const TComScalingList &scalingList, UInt listId, UInt sizeId, Int qp, const ChromaFormat format)
3076 {
3077  UInt width = g_scalingListSizeX[sizeId];
3078  UInt height = g_scalingListSizeX[sizeId];
3079  UInt ratio = g_scalingListSizeX[sizeId]/min(MAX_MATRIX_SIZE_NUM,(Int)g_scalingListSizeX[sizeId]);
3080  Int *dequantcoeff;
3081  const Int *coeff = scalingList.getScalingListAddress(sizeId,listId);
3082 
3083  dequantcoeff = getDequantCoeff(listId, qp, sizeId);
3084 
3085  Int invQuantScale = g_invQuantScales[qp];
3086 
3087  processScalingListDec(coeff,
3088  dequantcoeff,
3089  invQuantScale,
3090  height, width, ratio,
3092  scalingList.getScalingListDC(sizeId,listId));
3093 }
3094 
3098 {
3099  const Int minimumQp = 0;
3100  const Int maximumQp = SCALING_LIST_REM_NUM;
3101 
3102  for(UInt size = 0; size < SCALING_LIST_SIZE_NUM; size++)
3103  {
3104  for(UInt list = 0; list < SCALING_LIST_NUM; list++)
3105  {
3106  for(Int qp = minimumQp; qp < maximumQp; qp++)
3107  {
3108  xsetFlatScalingList(list,size,qp,format);
3109  setErrScaleCoeff(list,size,qp);
3110  }
3111  }
3112  }
3113 }
3114 
3122 {
3123  UInt i,num = g_scalingListSize[size];
3124  Int *quantcoeff;
3125  Int *dequantcoeff;
3126 
3127  Int quantScales = g_quantScales [qp];
3128  Int invQuantScales = g_invQuantScales[qp] << 4;
3129 
3130  quantcoeff = getQuantCoeff(list, qp, size);
3131  dequantcoeff = getDequantCoeff(list, qp, size);
3132 
3133  for(i=0;i<num;i++)
3134  {
3135  *quantcoeff++ = quantScales;
3136  *dequantcoeff++ = invQuantScales;
3137  }
3138 }
3139 
3150 Void TComTrQuant::processScalingListEnc( Int *coeff, Int *quantcoeff, Int quantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc)
3151 {
3152  for(UInt j=0;j<height;j++)
3153  {
3154  for(UInt i=0;i<width;i++)
3155  {
3156  quantcoeff[j*width + i] = quantScales / coeff[sizuNum * (j / ratio) + i / ratio];
3157  }
3158  }
3159 
3160  if(ratio > 1)
3161  {
3162  quantcoeff[0] = quantScales / dc;
3163  }
3164 }
3165 
3176 Void TComTrQuant::processScalingListDec( const Int *coeff, Int *dequantcoeff, Int invQuantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc)
3177 {
3178  for(UInt j=0;j<height;j++)
3179  {
3180  for(UInt i=0;i<width;i++)
3181  {
3182  dequantcoeff[j*width + i] = invQuantScales * coeff[sizuNum * (j / ratio) + i / ratio];
3183  }
3184  }
3185 
3186  if(ratio > 1)
3187  {
3188  dequantcoeff[0] = invQuantScales * dc;
3189  }
3190 }
3191 
3195 {
3196  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3197  {
3198  for(UInt qp = 0; qp < SCALING_LIST_REM_NUM; qp++)
3199  {
3200  for(UInt listId = 0; listId < SCALING_LIST_NUM; listId++)
3201  {
3202  m_quantCoef [sizeId][listId][qp] = new Int [g_scalingListSize[sizeId]];
3203  m_dequantCoef [sizeId][listId][qp] = new Int [g_scalingListSize[sizeId]];
3204  m_errScale [sizeId][listId][qp] = new Double [g_scalingListSize[sizeId]];
3205  } // listID loop
3206  }
3207  }
3208 }
3209 
3213 {
3214  for(UInt sizeId = 0; sizeId < SCALING_LIST_SIZE_NUM; sizeId++)
3215  {
3216  for(UInt listId = 0; listId < SCALING_LIST_NUM; listId++)
3217  {
3218  for(UInt qp = 0; qp < SCALING_LIST_REM_NUM; qp++)
3219  {
3220  if(m_quantCoef[sizeId][listId][qp])
3221  {
3222  delete [] m_quantCoef[sizeId][listId][qp];
3223  }
3224  if(m_dequantCoef[sizeId][listId][qp])
3225  {
3226  delete [] m_dequantCoef[sizeId][listId][qp];
3227  }
3228  if(m_errScale[sizeId][listId][qp])
3229  {
3230  delete [] m_errScale[sizeId][listId][qp];
3231  }
3232  }
3233  }
3234  }
3235 }
3236 
3237 Void TComTrQuant::transformSkipQuantOneSample(TComTU &rTu, const ComponentID compID, const TCoeff resiDiff, TCoeff* pcCoeff, const UInt uiPos, const QpParam &cQP, const Bool bUseHalfRoundingPoint)
3238 {
3239  TComDataCU *pcCU = rTu.getCU();
3240  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
3241  const TComRectangle &rect = rTu.getRect(compID);
3242  const UInt uiWidth = rect.width;
3243  const UInt uiHeight = rect.height;
3244  const Int iTransformShift = getTransformShift(toChannelType(compID), rTu.GetEquivalentLog2TrSize(compID));
3245  const Int scalingListType = getScalingListType(pcCU->getPredictionMode(uiAbsPartIdx), compID);
3246  const Bool enableScalingLists = getUseScalingList(uiWidth, uiHeight, true);
3247  const Int defaultQuantisationCoefficient = g_quantScales[cQP.rem];
3248 
3249  assert( scalingListType < SCALING_LIST_NUM );
3250  const Int *const piQuantCoeff = getQuantCoeff( scalingListType, cQP.rem, (rTu.GetEquivalentLog2TrSize(compID)-2) );
3251 
3252 
3253  /* for 422 chroma blocks, the effective scaling applied during transformation is not a power of 2, hence it cannot be
3254  * implemented as a bit-shift (the quantised result will be sqrt(2) * larger than required). Alternatively, adjust the
3255  * uiLog2TrSize applied in iTransformShift, such that the result is 1/sqrt(2) the required result (i.e. smaller)
3256  * Then a QP+3 (sqrt(2)) or QP-3 (1/sqrt(2)) method could be used to get the required result
3257  */
3258 
3259  const Int iQBits = QUANT_SHIFT + cQP.per + iTransformShift;
3260  // QBits will be OK for any internal bit depth as the reduction in transform shift is balanced by an increase in Qp_per due to QpBDOffset
3261 
3262  const Int iAdd = ( bUseHalfRoundingPoint ? 256 : (pcCU->getSlice()->getSliceType() == I_SLICE ? 171 : 85) ) << (iQBits - 9);
3263 
3264  TCoeff transformedCoefficient;
3265 
3266  // transform-skip
3267  if (iTransformShift >= 0)
3268  {
3269  transformedCoefficient = resiDiff << iTransformShift;
3270  }
3271  else // for very high bit depths
3272  {
3273  const Int iTrShiftNeg = -iTransformShift;
3274  const Int offset = 1 << (iTrShiftNeg - 1);
3275  transformedCoefficient = ( resiDiff + offset ) >> iTrShiftNeg;
3276  }
3277 
3278  // quantization
3279  const TCoeff iSign = (transformedCoefficient < 0 ? -1: 1);
3280 
3281  const Int quantisationCoefficient = enableScalingLists ? piQuantCoeff[uiPos] : defaultQuantisationCoefficient;
3282 
3283  const Int64 tmpLevel = (Int64)abs(transformedCoefficient) * quantisationCoefficient;
3284 
3285  const TCoeff quantisedCoefficient = (TCoeff((tmpLevel + iAdd ) >> iQBits)) * iSign;
3286 
3287  const TCoeff entropyCodingMinimum = -(1 << g_maxTrDynamicRange[toChannelType(compID)]);
3288  const TCoeff entropyCodingMaximum = (1 << g_maxTrDynamicRange[toChannelType(compID)]) - 1;
3289  pcCoeff[ uiPos ] = Clip3<TCoeff>( entropyCodingMinimum, entropyCodingMaximum, quantisedCoefficient );
3290 }
3291 
3292 
3293 Void TComTrQuant::invTrSkipDeQuantOneSample( TComTU &rTu, ComponentID compID, TCoeff inSample, Pel &reconSample, const QpParam &cQP, UInt uiPos )
3294 {
3295  TComDataCU *pcCU = rTu.getCU();
3296  const UInt uiAbsPartIdx = rTu.GetAbsPartIdxTU();
3297  const TComRectangle &rect = rTu.getRect(compID);
3298  const UInt uiWidth = rect.width;
3299  const UInt uiHeight = rect.height;
3300  const Int QP_per = cQP.per;
3301  const Int QP_rem = cQP.rem;
3302  const Int iTransformShift = getTransformShift(toChannelType(compID), rTu.GetEquivalentLog2TrSize(compID));
3303  const Int scalingListType = getScalingListType(pcCU->getPredictionMode(uiAbsPartIdx), compID);
3304  const Bool enableScalingLists = getUseScalingList(uiWidth, uiHeight, true);
3305  const UInt uiLog2TrSize = rTu.GetEquivalentLog2TrSize(compID);
3306 
3307  assert( scalingListType < SCALING_LIST_NUM );
3308 
3309  const Int rightShift = (IQUANT_SHIFT - (iTransformShift + QP_per)) + (enableScalingLists ? LOG2_SCALING_LIST_NEUTRAL_VALUE : 0);
3310 
3311  const TCoeff transformMinimum = -(1 << g_maxTrDynamicRange[toChannelType(compID)]);
3312  const TCoeff transformMaximum = (1 << g_maxTrDynamicRange[toChannelType(compID)]) - 1;
3313 
3314  // Dequantisation
3315 
3316  TCoeff dequantisedSample;
3317 
3318  if(enableScalingLists)
3319  {
3320  const UInt dequantCoefBits = 1 + IQUANT_SHIFT + SCALING_LIST_BITS;
3321  const UInt targetInputBitDepth = std::min<UInt>((g_maxTrDynamicRange[toChannelType(compID)] + 1), (((sizeof(Intermediate_Int) * 8) + rightShift) - dequantCoefBits));
3322 
3323  const Intermediate_Int inputMinimum = -(1 << (targetInputBitDepth - 1));
3324  const Intermediate_Int inputMaximum = (1 << (targetInputBitDepth - 1)) - 1;
3325 
3326  Int *piDequantCoef = getDequantCoeff(scalingListType,QP_rem,uiLog2TrSize-2);
3327 
3328  if(rightShift > 0)
3329  {
3330  const Intermediate_Int iAdd = 1 << (rightShift - 1);
3331  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, inSample));
3332  const Intermediate_Int iCoeffQ = ((Intermediate_Int(clipQCoef) * piDequantCoef[uiPos]) + iAdd ) >> rightShift;
3333 
3334  dequantisedSample = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
3335  }
3336  else
3337  {
3338  const Int leftShift = -rightShift;
3339  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, inSample));
3340  const Intermediate_Int iCoeffQ = (Intermediate_Int(clipQCoef) * piDequantCoef[uiPos]) << leftShift;
3341 
3342  dequantisedSample = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
3343  }
3344  }
3345  else
3346  {
3347  const Int scale = g_invQuantScales[QP_rem];
3348  const Int scaleBits = (IQUANT_SHIFT + 1) ;
3349 
3350  const UInt targetInputBitDepth = std::min<UInt>((g_maxTrDynamicRange[toChannelType(compID)] + 1), (((sizeof(Intermediate_Int) * 8) + rightShift) - scaleBits));
3351  const Intermediate_Int inputMinimum = -(1 << (targetInputBitDepth - 1));
3352  const Intermediate_Int inputMaximum = (1 << (targetInputBitDepth - 1)) - 1;
3353 
3354  if (rightShift > 0)
3355  {
3356  const Intermediate_Int iAdd = 1 << (rightShift - 1);
3357  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, inSample));
3358  const Intermediate_Int iCoeffQ = (Intermediate_Int(clipQCoef) * scale + iAdd) >> rightShift;
3359 
3360  dequantisedSample = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
3361  }
3362  else
3363  {
3364  const Int leftShift = -rightShift;
3365  const TCoeff clipQCoef = TCoeff(Clip3<Intermediate_Int>(inputMinimum, inputMaximum, inSample));
3366  const Intermediate_Int iCoeffQ = (Intermediate_Int(clipQCoef) * scale) << leftShift;
3367 
3368  dequantisedSample = TCoeff(Clip3<Intermediate_Int>(transformMinimum,transformMaximum,iCoeffQ));
3369  }
3370  }
3371 
3372  // Inverse transform-skip
3373 
3374  if (iTransformShift >= 0)
3375  {
3376  const TCoeff offset = iTransformShift==0 ? 0 : (1 << (iTransformShift - 1));
3377  reconSample = Pel(( dequantisedSample + offset ) >> iTransformShift);
3378  }
3379  else //for very high bit depths
3380  {
3381  const Int iTrShiftNeg = -iTransformShift;
3382  reconSample = Pel(dequantisedSample << iTrShiftNeg);
3383  }
3384 }
3385 
3386 
3388  const ComponentID compID,
3389  const Pel * piResiL,
3390  const Pel * piResiC,
3391  Pel * piResiT,
3392  const Int width,
3393  const Int height,
3394  const Int strideL,
3395  const Int strideC,
3396  const Int strideT,
3397  const Bool reverse )
3398 {
3399  const Pel *pResiL = piResiL;
3400  const Pel *pResiC = piResiC;
3401  Pel *pResiT = piResiT;
3402 
3403  TComDataCU *pCU = rTu.getCU();
3404  const Int alpha = pCU->getCrossComponentPredictionAlpha( rTu.GetAbsPartIdxTU( compID ), compID );
3405  const Int diffBitDepth = pCU->getSlice()->getSPS()->getDifferentialLumaChromaBitDepth();
3406 
3407  for( Int y = 0; y < height; y++ )
3408  {
3409  if (reverse)
3410  {
3411  // A constraint is to be added to the HEVC Standard to limit the size of pResiL and pResiC at this point.
3412  // The likely form of the constraint is to either restrict the values to CoeffMin to CoeffMax,
3413  // or to be representable in a bitDepthY+4 or bitDepthC+4 signed integer.
3414  // The result of the constraint is that for 8/10/12bit profiles, the input values
3415  // can be represented within a 16-bit Pel-type.
3416 #if RExt__HIGH_BIT_DEPTH_SUPPORT
3417  for( Int x = 0; x < width; x++ )
3418  {
3419  pResiT[x] = pResiC[x] + (( alpha * rightShift( pResiL[x], diffBitDepth) ) >> 3);
3420  }
3421 #else
3422  const Int minPel=std::numeric_limits<Pel>::min();
3423  const Int maxPel=std::numeric_limits<Pel>::max();
3424  for( Int x = 0; x < width; x++ )
3425  {
3426  pResiT[x] = Clip3<Int>(minPel, maxPel, pResiC[x] + (( alpha * rightShift<Int>(Int(pResiL[x]), diffBitDepth) ) >> 3));
3427  }
3428 #endif
3429  }
3430  else
3431  {
3432  // Forward does not need clipping. Pel type should always be big enough.
3433  for( Int x = 0; x < width; x++ )
3434  {
3435  pResiT[x] = pResiC[x] - (( alpha * rightShift<Int>(Int(pResiL[x]), diffBitDepth) ) >> 3);
3436  }
3437  }
3438 
3439  pResiL += strideL;
3440  pResiC += strideC;
3441  pResiT += strideT;
3442  }
3443 }
3444 
Int golombRiceAdaptationStatistics[4]
Definition: TComTrQuant.h:72
#define MAX_CU_SIZE
Definition: TComRom.h:54
Void setScalingList(TComScalingList *scalingList, const ChromaFormat format)
Bool getUseGolombRiceParameterAdaptation() const
Definition: TComSlice.h:901
static Int getScalingListType(const PredMode predMode, const ComponentID compID)
Void partialButterflyInverse8(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
Char * getQP()
Definition: TComDataCU.h:255
UInt m_uiMaxTrSize
Definition: TComTrQuant.h:214
ValueType leftShift(const ValueType value, const Int shift)
Definition: CommonDef.h:166
static Int getScaledChromaQP(Int unscaledChromaQP, const ChromaFormat chFmt)
unsigned short UShort
Definition: TypeDef.h:295
static Int getSigCtxInc(Int patternSigCtx, const TUEntropyCodingParameters &codingParameters, const Int scanPosition, const Int log2BlockWidth, const Int log2BlockHeight, const ChannelType chanType)
Bool m_useRDOQ
Definition: TComTrQuant.h:216
static const UInt notFirstGroupNeighbourhoodContextOffset[MAX_NUM_CHANNEL_TYPE]
Definition: ContextTables.h:88
Int * getScalingListAddress(UInt sizeId, UInt listId)
get matrix coefficient
Definition: TComSlice.h:155
Bool getUseExtendedPrecision() const
Definition: TComSlice.h:887
SliceType getSliceType() const
Definition: TComSlice.h:1337
CU data structure class.
Definition: TComDataCU.h:64
Void destroyScalingList()
#define MAX_INT
max. value of signed 32-bit integer
Definition: CommonDef.h:116
ChannelType
Definition: TypeDef.h:361
Bool useDST(const ComponentID compID)
Definition: TComTU.cpp:227
const TMatrixCoeff g_aiT16[TRANSFORM_NUMBER_OF_DIRECTIONS][16][16]
Definition: TComRom.cpp:474
#define NEIGHBOURHOOD_00_CONTEXT_2_THRESHOLD_4x4
Definition: ContextTables.h:93
void Void
Definition: TypeDef.h:285
Void xT(const ComponentID compID, Bool useDST, Pel *piBlkResi, UInt uiStride, TCoeff *psCoeff, Int iWidth, Int iHeight)
UChar * getExplicitRdpcmMode(ComponentID component)
Definition: TComDataCU.h:283
#define DEBUG_STRING_FN_DECLAREP(name)
Definition: TypeDef.h:72
estBitsSbacStruct * m_pcEstBitsSbac
Definition: TComTrQuant.h:154
static Void crossComponentPrediction(TComTU &rTu, const ComponentID compID, const Pel *piResiL, const Pel *piResiC, Pel *piResiT, const Int width, const Int height, const Int strideL, const Int strideC, const Int strideT, const Bool reverse)
#define QUANT_SHIFT
Definition: TComRom.h:95
UChar * getTransformIdx()
Definition: TComDataCU.h:274
const TMatrixCoeff g_aiT4[TRANSFORM_NUMBER_OF_DIRECTIONS][4][4]
Definition: TComRom.cpp:462
__inline Double xGetICost(Double dRate) const
Void applyForwardRDPCM(TComTU &rTu, const ComponentID compID, Pel *pcResidual, const UInt uiStride, const QpParam &cQP, TCoeff *pcCoeff, TCoeff &uiAbsSum, const RDPCMMode mode)
Double d64CodedLevelandDist
Definition: TComTrQuant.cpp:51
#define NULL
Definition: CommonDef.h:100
Bool m_useTransformSkipFast
Definition: TComTrQuant.h:221
QpParam(const Int qpy, const ChannelType chType, const Int qpBdOffset, const Int chromaQPOffset, const ChromaFormat chFmt)
Definition: TComTrQuant.cpp:71
Int blockCbpBits[2 *5][2]
Definition: TComTrQuant.h:69
UInt g_scalingListSizeX[SCALING_LIST_SIZE_NUM]
Definition: TComRom.cpp:679
Void xSetScalingListEnc(TComScalingList *scalingList, UInt list, UInt size, Int qp, const ChromaFormat format)
Void partialButterfly16(TCoeff *src, TCoeff *dst, Int shift, Int line)
Char g_aucConvertToBit[(1<<(6))+1]
Definition: TComRom.cpp:558
unsigned int UInt
Definition: TypeDef.h:297
#define C1FLAG_NUMBER
Definition: TypeDef.h:119
Void xQuant(TComTU &rTu, TCoeff *pSrc, TCoeff *pDes, TCoeff *pArlDes, TCoeff &uiAbsSum, const ComponentID compID, const QpParam &cQP)
Void partialButterflyInverse32(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
Void setErrScaleCoeff(UInt list, UInt size, Int qp)
Int m_sliceNsamples[30+1]
Definition: TComTrQuant.h:203
Bool isRDPCMEnabled(UInt uiAbsPartIdx)
Definition: TComDataCU.h:287
#define VER_IDX
Definition: TypeDef.h:181
Short Pel
pixel type
Definition: TypeDef.h:692
static UInt getChromasCorrespondingPULumaIdx(const UInt lumaZOrderIdxInCtu, const ChromaFormat chFmt)
SCALING_LIST class.
Definition: TComSlice.h:150
Void xITransformSkip(TCoeff *plCoef, Pel *pResidual, UInt uiStride, TComTU &rTu, const ComponentID component)
Int blockRootCbpBits[4][2]
Definition: TComTrQuant.h:70
Int * getDequantCoeff(UInt list, Int qp, UInt size)
get Quant Coefficent
Definition: TComTrQuant.h:178
static const Int chromaQPMappingTableSize
Definition: TComRom.h:128
UInt getStride(const ComponentID id) const
Definition: TComYuv.h:197
Int * getQuantCoeff(UInt list, Int qp, UInt size)
get Error Scale Coefficent
Definition: TComTrQuant.h:177
UInt GetTransformDepthRel() const
Definition: TComTU.h:108
picture class (header)
Double * m_errScale[SCALING_LIST_SIZE_NUM][(MAX_NUM_COMPONENT *NUMBER_OF_PREDICTION_MODES)][6]
array of quantization matrix coefficient 4x4
Definition: TComTrQuant.h:227
QP struct.
Definition: TComTrQuant.h:80
Bool nextSection(const TComTU &parent)
Definition: TComTU.cpp:178
Int getScalingListDC(UInt sizeId, UInt listId) const
get DC value
Definition: TComSlice.h:166
Void xDeQuant(TComTU &rTu, const TCoeff *pSrc, TCoeff *pDes, const ComponentID compID, const QpParam &cQP)
#define MAX_QP
Definition: CommonDef.h:127
Int rem
Definition: TComTrQuant.h:84
Void setScalingListDec(const TComScalingList &scalingList, const ChromaFormat format)
const UInt g_uiGroupIdx[32]
Definition: TComRom.cpp:584
UChar getCbf(UInt uiIdx, ComponentID eType)
Definition: TComDataCU.h:303
UInt getCtxQtCbf(TComTU &rTu, const ChannelType chType)
UInt GetSectionNumber() const
Definition: TComTU.h:101
#define RExt__GOLOMB_RICE_INCREMENT_DIVISOR
Definition: TypeDef.h:248
UInt getMaxTrSize() const
Definition: TComSlice.h:879
static UInt getSigCoeffGroupCtxInc(const UInt *uiSigCoeffGroupFlag, const UInt uiCGPosX, const UInt uiCGPosY, const UInt widthInGroups, const UInt heightInGroups)
Int m_greaterOneBits[((4 *4)+(4 *2))][2]
Definition: TComTrQuant.h:66
#define LEVEL_RANGE
G382: max coefficient level in statistics collection.
Definition: TypeDef.h:151
Bool isInter(UInt uiPartIdx) const
Definition: TComDataCU.h:444
TComPicYuv * getPicYuvRec()
Definition: TComPic.h:109
UInt GetAbsPartIdxTU() const
Definition: TComTU.h:119
const UInt * scanCG
Definition: TypeDef.h:753
RDPCMMode
Definition: TypeDef.h:327
#define NUM_ONE_FLAG_CTX_PER_SET
number of context models for greater than 1 flag in a set
Void setCbfPartRange(UInt uiCbf, ComponentID compID, UInt uiAbsPartIdx, UInt uiCoveredPartIdxes)
Short TMatrixCoeff
transform matrix coefficient
Definition: TypeDef.h:694
Void setFlatScalingList(const ChromaFormat format)
Void partialButterfly8(TCoeff *src, TCoeff *dst, Int shift, Int line)
#define NEIGHBOURHOOD_00_CONTEXT_1_THRESHOLD_4x4
Definition: ContextTables.h:92
Int significantBits[(28+16)][2]
Definition: TComTrQuant.h:63
TCoeff * m_plTempCoeff
Definition: TComTrQuant.h:206
#define SCALING_LIST_NUM
list number for quantization matrix
Definition: TComRom.h:215
Void printBlockToStream(std::ostream &ss, const Char *pLinePrefix, TComYuv &src, const UInt numSubBlocksAcross, const UInt numSubBlocksUp, const UInt defWidth)
Definition: Debug.cpp:424
Void storeSliceQpNext(TComSlice *pcSlice)
const UChar g_chroma422IntraAngleMappingTable[36]
Definition: TComRom.cpp:540
Double m_dLambda
Definition: TComTrQuant.h:212
UChar * getIntraDir(const ChannelType channelType) const
Definition: TComDataCU.h:338
Int Intermediate_Int
used as intermediate value in calculations
Definition: TypeDef.h:696
static UInt getCBFContextOffset(const ComponentID component)
#define MAX_MATRIX_SIZE_NUM
max size number for quantization matrix
Definition: TComRom.h:219
Int offset[2]
Definition: TComSlice.h:430
UInt getGolombRiceStatisticsIndex(const ComponentID compID)
Definition: TComTU.cpp:245
#define IQUANT_SHIFT
Definition: TComRom.h:96
general YUV buffer class
Definition: TComYuv.h:54
Int getQpBDOffset(ChannelType type) const
Definition: TComSlice.h:885
__inline UInt xGetCodedLevel(Double &rd64CodedCost, Double &rd64CodedCost0, Double &rd64CodedCostSig, Intermediate_Int lLevelDouble, UInt uiMaxAbsLevel, UShort ui16CtxNumSig, UShort ui16CtxNumOne, UShort ui16CtxNumAbs, UShort ui16AbsGoRice, UInt c1Idx, UInt c2Idx, Int iQBits, Double errorScale, Bool bLast, Bool useLimitedPrefixLength, ChannelType channelType) const
#define C2FLAG_NUMBER
Definition: TypeDef.h:120
Double & getErrScaleCoeffNoScalingList(UInt list, UInt size, Int qp)
get Error Scale Coefficent
Definition: TComTrQuant.h:176
Double * getErrScaleCoeff(UInt list, UInt size, Int qp)
Definition: TComTrQuant.h:175
Int * m_quantCoef[SCALING_LIST_SIZE_NUM][(MAX_NUM_COMPONENT *NUMBER_OF_PREDICTION_MODES)][6]
array of quantization matrix coefficient 4x4
Definition: TComTrQuant.h:225
bool Bool
Definition: TypeDef.h:286
Void transformSkipQuantOneSample(TComTU &rTu, const ComponentID compID, const TCoeff resiDiff, TCoeff *pcCoeff, const UInt uiPos, const QpParam &cQP, const Bool bUseHalfRoundingPoint)
Bool isIntra(UInt uiPartIdx) const
Definition: TComDataCU.h:443
Int g_maxTrDynamicRange[MAX_NUM_CHANNEL_TYPE]
Definition: TComRom.cpp:325
Void invRdpcmNxN(TComTU &rTu, const ComponentID compID, Pel *pcResidual, const UInt uiStride)
long long Int64
Definition: TypeDef.h:317
Void initScalingList()
#define ADAPTIVE_QP_SELECTION
G382: Adaptive reconstruction levels, non-normative part for adaptive QP selection.
Definition: TypeDef.h:148
#define MLS_CG_LOG2_HEIGHT
Definition: TypeDef.h:145
Bool m_bUseAdaptQpSelect
Definition: TComTrQuant.h:219
__inline Double xGetRateLast(const UInt uiPosX, const UInt uiPosY, const ComponentID component) const
Void signBitHidingHDQ(const ComponentID compID, TCoeff *pQCoef, TCoeff *pCoef, TCoeff *deltaU, const TUEntropyCodingParameters &codingParameters)
Void processScalingListDec(const Int *coeff, Int *dequantcoeff, Int invQuantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc)
UInt GetEquivalentLog2TrSize(const ComponentID compID) const
Definition: TComTU.cpp:221
Void xSetScalingListDec(const TComScalingList &scalingList, UInt list, UInt size, Int qp, const ChromaFormat format)
#define COEF_REMAIN_BIN_REDUCTION
transitions from Golomb-Rice to TU+EG(k)
Definition: TypeDef.h:109
Void getTUEntropyCodingParameters(TUEntropyCodingParameters &result, TComTU &rTu, const ComponentID component)
static ChannelType toChannelType(const ComponentID id)
Void partialButterfly32(TCoeff *src, TCoeff *dst, Int shift, Int line)
T Clip3(const T minVal, const T maxVal, const T a)
general min/max clip
Definition: CommonDef.h:137
Double m_sliceSumC[30+1]
Definition: TComTrQuant.h:204
Defines constants and tables for SBAC.
#define DEBUG_STRING_APPEND(str1, str2)
Definition: TypeDef.h:75
TComPicYuv * getPicYuvOrg()
Definition: TComPic.h:108
static const Int g_transformMatrixShift[TRANSFORM_NUMBER_OF_DIRECTIONS]
Definition: TComRom.h:116
ValueType rightShift(const ValueType value, const Int shift)
Definition: CommonDef.h:167
Int TCoeff
transform coefficient
Definition: TypeDef.h:693
Int getDifferentialLumaChromaBitDepth() const
Definition: TComSlice.h:884
Pel * getAddr(const ComponentID id)
Definition: TComYuv.h:148
__inline Double xGetRateSigCoeffGroup(UShort uiSignificanceCoeffGroup, UShort ui16CtxNumSig) const
Void partialButterflyInverse4(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
Int getSliceQpBase() const
Definition: TComSlice.h:1343
Void xITrMxN(Int bitDepth, TCoeff *coeff, TCoeff *block, Int iWidth, Int iHeight, Bool useDST, const Int maxTrDynamicRange)
Void fastInverseDst(TCoeff *tmp, TCoeff *block, Int shift, const TCoeff outputMinimum, const TCoeff outputMaximum)
#define SCALING_LIST_BITS
bit depth of scaling list entries
Definition: TComRom.h:220
ChromaFormat
chroma formats (according to semantics of chroma_format_idc)
Definition: TypeDef.h:352
UInt getCoefficientOffset(const ComponentID compID) const
Definition: TComTU.h:94
Int per
Definition: TComTrQuant.h:83
__inline Double xGetRateSigCoef(UShort uiSignificance, UShort ui16CtxNumSig) const
const TComRectangle & getRect(const ComponentID compID) const
Definition: TComTU.h:96
Char * getCrossComponentPredictionAlpha(ComponentID compID)
Definition: TComDataCU.h:239
Void partialButterflyInverse16(TCoeff *src, TCoeff *dst, Int shift, Int line, const TCoeff outputMinimum, const TCoeff outputMaximum)
const TComPPS * getPPS() const
Definition: TComSlice.h:1309
static const UInt significanceMapContextSetStart[MAX_NUM_CHANNEL_TYPE][CONTEXT_NUMBER_OF_TYPES]
Definition: ContextTables.h:85
#define DEBUG_STRING_PASS_INTO(name)
Definition: TypeDef.h:69
#define SCALE_BITS
Definition: TComRom.h:97
Bool getUseScalingList(const UInt width, const UInt height, const Bool isTransformSkip)
Definition: TComTrQuant.h:180
#define MLS_GRP_NUM
G644 : Max number of coefficient groups, max(16, 64)
Definition: TypeDef.h:143
UChar * getTransformSkip(ComponentID compID)
Definition: TComDataCU.h:278
#define SBH_THRESHOLD
I0156: value of the fixed SBH controlling threshold.
Definition: TypeDef.h:115
ChromaFormat getChromaFormat() const
Definition: TComPic.h:128
__inline Double xGetIEPRate() const
TComDataCU * getCU()
Definition: TComTU.h:126
#define ARL_C_PRECISION
G382: 7-bit arithmetic precision.
Definition: TypeDef.h:150
Void xTrMxN(Int bitDepth, TCoeff *block, TCoeff *coeff, Int iWidth, Int iHeight, Bool useDST, const Int maxTrDynamicRange)
Int g_quantScales[6]
Definition: TComRom.cpp:327
Void rdpcmNxN(TComTU &rTu, const ComponentID compID, Pel *pcResidual, const UInt uiStride, const QpParam &cQP, TCoeff *pcCoeff, TCoeff &uiAbsSum, RDPCMMode &rdpcmMode)
Bool ProcessComponentSection(const ComponentID compID) const
Definition: TComTU.h:99
#define DM_CHROMA_IDX
Definition: TypeDef.h:185
#define HOR_IDX
Definition: TypeDef.h:182
Void invTrSkipDeQuantOneSample(TComTU &rTu, ComponentID compID, TCoeff pcCoeff, Pel &reconSample, const QpParam &cQP, UInt uiPos)
Void invTransformNxN(TComTU &rTu, const ComponentID compID, Pel *pcResidual, const UInt uiStride, TCoeff *pcCoeff, const QpParam &cQP)
const TMatrixCoeff g_aiT32[TRANSFORM_NUMBER_OF_DIRECTIONS][32][32]
Definition: TComRom.cpp:480
COEFF_SCAN_TYPE scanType
Definition: TypeDef.h:754
const TMatrixCoeff g_aiT8[TRANSFORM_NUMBER_OF_DIRECTIONS][8][8]
Definition: TComRom.cpp:468
Int lastYBits[MAX_NUM_CHANNEL_TYPE][10]
Definition: TComTrQuant.h:65
Bool * getCUTransquantBypass()
Definition: TComDataCU.h:242
Void xTransformSkip(Pel *piBlkResi, UInt uiStride, TCoeff *psCoeff, TComTU &rTu, const ComponentID component)
Int * m_dequantCoef[SCALING_LIST_SIZE_NUM][(MAX_NUM_COMPONENT *NUMBER_OF_PREDICTION_MODES)][6]
array of dequantization matrix coefficient 4x4
Definition: TComTrQuant.h:226
UInt GetAbsPartIdxNumParts() const
Definition: TComTU.h:121
TComPic * getPic()
Definition: TComDataCU.h:203
Int g_bitDepth[MAX_NUM_CHANNEL_TYPE]
Definition: TComRom.cpp:548
Int g_invQuantScales[6]
Definition: TComRom.cpp:332
Void initSliceQpDelta()
Definition: TComTU.h:48
#define RDOQ_CHROMA
use of RDOQ in chroma
Definition: TComTrQuant.cpp:64
Void processScalingListEnc(Int *coeff, Int *quantcoeff, Int quantScales, UInt height, UInt width, UInt ratio, Int sizuNum, UInt dc)
#define MAX_INTERMEDIATE_INT
Definition: CommonDef.h:121
Void init(UInt uiMaxTrSize, Bool useRDOQ=false, Bool useRDOQTS=false, Bool bEnc=false, Bool useTransformSkipFast=false, Bool bUseAdaptQpSelect=false)
const UInt ctxIndMap4x4[4 *4]
Definition: TComRom.cpp:575
Char * getPredictionMode()
Definition: TComDataCU.h:234
Int getQpOffset(ComponentID compID) const
Definition: TComSlice.h:1073
const TMatrixCoeff g_as_DST_MAT_4[TRANSFORM_NUMBER_OF_DIRECTIONS][4][4]
Definition: TComRom.cpp:486
static Int getTransformShift(const ChannelType type, const UInt uiLog2TrSize)
static Int calcPatternSigCtx(const UInt *sigCoeffGroupFlag, UInt uiCGPosX, UInt uiCGPosY, UInt widthInGroups, UInt heightInGroups)
Int significantCoeffGroupBits[2][2]
Definition: TComTrQuant.h:62
#define MAX_INT64
max. value of signed 64-bit integer
Definition: CommonDef.h:117
static UInt getSignificanceMapContextOffset(const ComponentID component)
Void xIT(const ComponentID compID, Bool useDST, TCoeff *plCoef, Pel *pResidual, UInt uiStride, Int iWidth, Int iHeight)
#define DISTORTION_PRECISION_ADJUSTMENT(x)
Definition: TypeDef.h:269
Void clearSliceARLCnt()
UInt g_debugCounter
Definition: Debug.cpp:193
UInt g_scalingListSize[SCALING_LIST_SIZE_NUM]
Definition: TComRom.cpp:678
TCoeff * getCoeff(ComponentID component)
Definition: TComDataCU.h:296
int Int
Definition: TypeDef.h:296
Int m_levelAbsBits[((1 *4)+(1 *2))][2]
Definition: TComTrQuant.h:67
static Bool isChroma(const ComponentID id)
Void xsetFlatScalingList(UInt list, UInt size, Int qp, const ChromaFormat format)
Void partialButterfly4(TCoeff *src, TCoeff *dst, Int shift, Int line)
Int lastXBits[MAX_NUM_CHANNEL_TYPE][10]
Definition: TComTrQuant.h:64
ComponentID
Definition: TypeDef.h:368
Int getSliceChromaQpDelta(ComponentID compID) const
Definition: TComSlice.h:1346
Int getSliceQp() const
Definition: TComSlice.h:1339
Void xRateDistOptQuant(TComTU &rTu, TCoeff *plSrcCoeff, TCoeff *piDstCoeff, TCoeff *piArlDstCoeff, TCoeff &uiAbsSum, const ComponentID compID, const QpParam &cQP)
transform and quantization class (header)
#define MAX_DOUBLE
max. value of Double-type value
Definition: CommonDef.h:124
__inline Int xGetICRate(UInt uiAbsLevel, UShort ui16CtxNumOne, UShort ui16CtxNumAbs, UShort ui16AbsGoRice, UInt c1Idx, UInt c2Idx, Bool useLimitedPrefixLength, ChannelType channelType) const
Bool getSignHideFlag() const
Definition: TComSlice.h:1144
#define MLS_CG_SIZE
G644 : Coefficient group size of 4x4.
Definition: TypeDef.h:146
Bool isNonTransformedResidualRotated(const ComponentID compID)
Definition: TComTU.cpp:236
Bool getUseCrossComponentPrediction() const
Definition: TComSlice.h:1107
Void invRecurTransformNxN(const ComponentID compID, TComYuv *pResidual, TComTU &rTu)
const ChromaQpAdj & getChromaQpAdjTableAt(Int cuChromaQpOffsetIdxPlus1) const
Definition: TComSlice.h:1083
Void printBlock(const ValueType *const source, const UInt width, const UInt height, const UInt stride, const UInt outputValueWidth=0, const Bool onlyPrintEdges=false, const Bool printInZScan=false, const Int shiftLeftBy=0, const Bool printAverage=false, std::ostream &stream=std::cout)
Definition: Debug.h:161
#define SCALING_LIST_REM_NUM
Definition: TComRom.h:60
Int m_qpDelta[51+1]
Definition: TComTrQuant.h:202
double Double
Definition: TypeDef.h:298
static UInt getContextSetIndex(const ComponentID component, const UInt subsetIndex, const Bool foundACoefficientGreaterThan1)
vertical first scan
Definition: TypeDef.h:525
slice header class
Definition: TComSlice.h:1198
#define LOG2_SCALING_LIST_NEUTRAL_VALUE
log2 of the value that, when used in a scaling list, has no effect on quantisation ...
Definition: TComRom.h:221
Void transformNxN(TComTU &rTu, const ComponentID compID, Pel *pcResidual, const UInt uiStride, TCoeff *rpcCoeff, TCoeff *rpcArlCoeff, TCoeff &uiAbsSum, const QpParam &cQP)
#define MAX_TU_SIZE
Definition: TComRom.h:57
UChar * getChromaQpAdj()
array of chroma QP adjustments (indexed). when value = 0, cu_chroma_qp_offset_flag=0; when value>0...
Definition: TComDataCU.h:265
Void fastForwardDst(TCoeff *block, TCoeff *coeff, Int shift)
Int Qp
Definition: TComTrQuant.h:82
#define NUM_ABS_FLAG_CTX_PER_SET
number of context models for greater than 2 flag in a set
#define DEBUG_STRING_NEW(name)
Definition: TypeDef.h:73
Defines types and objects for environment-variable-based debugging and feature control.
static Bool isLuma(const ComponentID id)
TComSlice * getSlice()
Definition: TComDataCU.h:205
Bool m_useRDOQTS
Definition: TComTrQuant.h:217
const TComSPS * getSPS() const
Definition: TComSlice.h:1306
ChromaFormat getChromaFormat() const
Definition: TComPicYuv.h:110
Void setExplicitRdpcmModePartRange(UInt rdpcmMode, ComponentID compID, UInt uiAbsPartIdx, UInt uiCoveredPartIdxes)
#define MLS_CG_LOG2_WIDTH
Definition: TypeDef.h:144