CodingStatistics.h 27.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/* The copyright in this software is being made available under the BSD
 * License, included below. This software may be subject to other third party
 * and contributor rights, including patent rights, and no such rights are
 * granted under this license.
 *
 * Copyright (c) 2010-2018, ITU/ISO/IEC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __CODINGSTATISTICS__
#define __CODINGSTATISTICS__

#include "CommonDef.h"
#include <stdio.h>
#include <string>
#include <map>
#include <math.h>
#include <cinttypes>
#include "ChromaFormat.h"

static const int64_t CODINGSTATISTICS_ENTROPYSCALE = 32768;


enum CodingStatisticsType
{
  STATS__NAL_UNIT_TOTAL_BODY,// This is a special case and is not included in the total sums.
  STATS__NAL_UNIT_PACKING,
  STATS__EMULATION_PREVENTION_3_BYTES,
  STATS__NAL_UNIT_HEADER_BITS,
  STATS__CABAC_INITIALISATION,
  STATS__CABAC_BITS__TQ_BYPASS_FLAG,
  STATS__CABAC_BITS__SKIP_FLAG,
  STATS__CABAC_BITS__MERGE_FLAG,
  STATS__CABAC_BITS__MERGE_INDEX,
  STATS__CABAC_BITS__MVP_IDX,
  STATS__CABAC_BITS__SPLIT_FLAG,
  STATS__CABAC_BITS__PART_SIZE,
  STATS__CABAC_BITS__PRED_MODE,
  STATS__CABAC_BITS__INTRA_DIR_ANG,
  STATS__CABAC_BITS__INTRA_PDPC_FLAG,
  STATS__CABAC_BITS__INTER_DIR,
  STATS__CABAC_BITS__REF_FRM_IDX,
  STATS__CABAC_BITS__MVD,
  STATS__CABAC_BITS__MVD_EP,
  STATS__CABAC_BITS__TRANSFORM_SUBDIV_FLAG,
  STATS__CABAC_BITS__QT_ROOT_CBF,
  STATS__CABAC_BITS__DELTA_QP_EP,
  STATS__CABAC_BITS__CHROMA_QP_ADJUSTMENT,
  STATS__CABAC_BITS__QT_CBF,
  STATS__CABAC_BITS__CROSS_COMPONENT_PREDICTION,
  STATS__CABAC_BITS__TRANSFORM_SKIP_FLAGS,
  STATS__CABAC_BITS__LAST_SIG_X_Y,
  STATS__CABAC_BITS__SIG_COEFF_GROUP_FLAG,
  STATS__CABAC_BITS__SIG_COEFF_MAP_FLAG,
  STATS__CABAC_BITS__GT1_FLAG,
  STATS__CABAC_BITS__GT2_FLAG,
  STATS__CABAC_BITS__SIGN_BIT,
  STATS__CABAC_BITS__ESCAPE_BITS,
  STATS__CABAC_BITS__SAO,
  STATS__CABAC_TRM_BITS,
  STATS__CABAC_FIXED_BITS,
  STATS__CABAC_PCM_ALIGN_BITS,
  STATS__CABAC_PCM_CODE_BITS,
  STATS__BYTE_ALIGNMENT_BITS,
  STATS__TRAILING_BITS,
  STATS__EXPLICIT_RDPCM_BITS,
  STATS__CABAC_EP_BIT_ALIGNMENT,
  STATS__CABAC_BITS__ALIGNED_SIGN_BIT,
  STATS__CABAC_BITS__ALIGNED_ESCAPE_BITS,
94 95 96 97 98 99 100 101
  STATS__CABAC_BITS__OTHER,
  STATS__CABAC_BITS__INVALID,
  STATS__TOOL_TOTAL_FRAME,// This is a special case and is not included in the report.
  STATS__CABAC_BITS__AFFINE_FLAG,
  STATS__TOOL_AFF,
  STATS__CABAC_BITS__AFFINE_TYPE,
  STATS__CABAC_BITS__PAR_FLAG,
  STATS__CABAC_BITS__ALF,
102
  STATS__CABAC_BITS__IMV_FLAG,
103 104 105
#if JVET_L0646_GBI
  STATS__CABAC_BITS__GBI_IDX,
#endif
106 107
  STATS__CABAC_BITS__EMT_CU_FLAG,
  STATS__CABAC_BITS__EMT_TU_INDEX,
108
  STATS__TOOL_EMT,
Shelly Chiang's avatar
Shelly Chiang committed
109 110 111
#if JVET_L0100_MULTI_HYPOTHESIS_INTRA
  STATS__CABAC_BITS__MH_INTRA_FLAG,
#endif
112 113 114 115
  STATS__TOOL_TOTAL,
  STATS__NUM_STATS
};

116 117 118 119 120 121 122 123 124 125
enum CodingStatisticsMode
{
  STATS__MODE_NONE  = 0,
  STATS__MODE_BITS  = 1,
  STATS__MODE_TOOLS = 2,

  STATS__MODE_ALL   =
    STATS__MODE_BITS | STATS__MODE_TOOLS
};

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static inline const char* getName(CodingStatisticsType name)
{
  static const char *statNames[]=
  {
    "NAL_UNIT_TOTAL_BODY", // This is a special case and is not included in the total sums.
    "NAL_UNIT_PACKING",
    "EMULATION_PREVENTION_3_BYTES",
    "NAL_UNIT_HEADER_BITS",
    "CABAC_INITIALISATION-and-rounding",
    "CABAC_BITS__TQ_BYPASS_FLAG",
    "CABAC_BITS__SKIP_FLAG",
    "CABAC_BITS__MERGE_FLAG",
    "CABAC_BITS__MERGE_INDEX",
    "CABAC_BITS__MVP_IDX",
    "CABAC_BITS__SPLIT_FLAG",
    "CABAC_BITS__PART_SIZE",
    "CABAC_BITS__PRED_MODE",
    "CABAC_BITS__INTRA_DIR_ANG",
    "CABAC_BITS__INTRA_PDPC_FLAG",
    "CABAC_BITS__INTER_DIR",
    "CABAC_BITS__REF_FRM_IDX",
    "CABAC_BITS__MVD",
    "CABAC_BITS__MVD_EP",
    "CABAC_BITS__AFFINE_FLAG",
    "CABAC_BITS__AFFINE_TYPE",
    "CABAC_BITS__TRANSFORM_SUBDIV_FLAG",
    "CABAC_BITS__QT_ROOT_CBF",
    "CABAC_BITS__DELTA_QP_EP",
    "CABAC_BITS__CHROMA_QP_ADJUSTMENT",
    "CABAC_BITS__QT_CBF",
    "CABAC_BITS__CROSS_COMPONENT_PREDICTION",
    "CABAC_BITS__TRANSFORM_SKIP_FLAGS",
    "CABAC_BITS__LAST_SIG_X_Y",
    "CABAC_BITS__SIG_COEFF_GROUP_FLAG",
    "CABAC_BITS__SIG_COEFF_MAP_FLAG",
    "CABAC_BITS__PAR_FLAG",
    "CABAC_BITS__GT1_FLAG",
    "CABAC_BITS__GT2_FLAG",
    "CABAC_BITS__SIGN_BIT",
    "CABAC_BITS__ESCAPE_BITS",
    "CABAC_BITS__SAO",
    "CABAC_BITS__ALF",
    "CABAC_TRM_BITS",
    "CABAC_FIXED_BITS",
    "CABAC_PCM_ALIGN_BITS",
    "CABAC_PCM_CODE_BITS",
    "BYTE_ALIGNMENT_BITS",
    "TRAILING_BITS",
    "EXPLICIT_RDPCM_BITS",
    "CABAC_EP_BIT_ALIGNMENT",
    "CABAC_BITS__ALIGNED_SIGN_BIT",
    "CABAC_BITS__ALIGNED_ESCAPE_BITS",
    "CABAC_BITS__IMV_FLAG",
179 180 181
#if JVET_L0646_GBI
    "CABAC_BITS__GBI_IDX",
#endif
182 183
    "CABAC_BITS__EMT_CU_FLAG",
    "CABAC_BITS__EMT_TU_INDX",
Shelly Chiang's avatar
Shelly Chiang committed
184 185 186
#if JVET_L0100_MULTI_HYPOTHESIS_INTRA
    "CABAC_BITS__MH_INTRA_FLAG",
#endif
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    "CABAC_BITS__OTHER",
    "CABAC_BITS__INVALID",
    "TOOL_FRAME",
    "TOOL_TOTAL"
  };
  CHECK( STATS__NUM_STATS != sizeof( statNames ) / sizeof( char* ) || name >= STATS__NUM_STATS, "stats out of range" );
  return statNames[name];
}

static inline bool isAlignedBins( CodingStatisticsType statT ) { return statT == STATS__CABAC_BITS__ALIGNED_SIGN_BIT || statT == STATS__CABAC_BITS__ALIGNED_ESCAPE_BITS; }

static const uint32_t CODING_STATS_NUM_WIDTHS     = 20; // just define the number of widths and heigts as 15
static const uint32_t CODING_STATS_NUM_HEIGHTS    = 20;
static const uint32_t CODING_STATS_NUM_SIZES      = CODING_STATS_NUM_HEIGHTS * CODING_STATS_NUM_WIDTHS;
static const uint32_t CODING_STATS_NUM_SUBCLASSES = CODING_STATS_NUM_SIZES * (1 + MAX_NUM_COMPONENT + MAX_NUM_CHANNEL_TYPE);

class CodingStatisticsClassType
{
public:

  CodingStatisticsClassType( const CodingStatisticsType t ) : type( t ), subClass( 0 )
  {
  }

  CodingStatisticsClassType( const CodingStatisticsType t, const uint32_t width, const uint32_t height ) : type( t ), subClass( gp_sizeIdxInfo->idxFrom( height ) * CODING_STATS_NUM_WIDTHS + gp_sizeIdxInfo->idxFrom( width ) )
  {
  }

  CodingStatisticsClassType( const CodingStatisticsType t, const int width, const int height ) : type( t ), subClass( gp_sizeIdxInfo->idxFrom( height ) * CODING_STATS_NUM_WIDTHS + gp_sizeIdxInfo->idxFrom( width ) )
  {
  }

  CodingStatisticsClassType( const CodingStatisticsType t, const ComponentID cid ) : type( t ), subClass( ( cid + 1 ) * CODING_STATS_NUM_SIZES )
  {
  }

  CodingStatisticsClassType( const CodingStatisticsType t, const ChannelType chid ) : type( t ), subClass( ( chid + MAX_NUM_COMPONENT + 1 ) * CODING_STATS_NUM_SIZES )
  {
  }

  CodingStatisticsClassType( const CodingStatisticsType t, const uint32_t width, const uint32_t height, const ComponentID cid ) : type( t ), subClass( ( cid + 1 ) * CODING_STATS_NUM_SIZES + gp_sizeIdxInfo->idxFrom( height ) * CODING_STATS_NUM_WIDTHS + gp_sizeIdxInfo->idxFrom( width ) )
  {
  }

  CodingStatisticsClassType( const CodingStatisticsType t, const uint32_t width, const uint32_t height, const ChannelType chid ) : type( t ), subClass( ( chid + MAX_NUM_COMPONENT + 1 ) * CODING_STATS_NUM_SIZES + gp_sizeIdxInfo->idxFrom( height ) * CODING_STATS_NUM_WIDTHS + gp_sizeIdxInfo->idxFrom( width ) )
  {
  }

  ~CodingStatisticsClassType()
  {
    // should not be relevant, but it looks like some instances are used after they are destroyed
    type = STATS__CABAC_BITS__INVALID;
    subClass = 0;
  }

  static uint32_t GetSubClassWidth( const uint32_t subClass )
  {
    return subClass % CODING_STATS_NUM_WIDTHS;
  }

  static uint32_t GetSubClassHeight( const uint32_t subClass )
  {
    return ( subClass % CODING_STATS_NUM_SIZES ) / CODING_STATS_NUM_WIDTHS;
  }

  static const char *GetSubClassString( const uint32_t subClass )
  {
    CHECK( subClass >= CODING_STATS_NUM_SUBCLASSES, "Subclass does not exist" );
    static const char *strings[1 + MAX_NUM_COMPONENT + MAX_NUM_CHANNEL_TYPE] = { "-", "Y", "Cb", "Cr", "Luma", "Chroma" };
    return strings[subClass / CODING_STATS_NUM_SIZES];
  }

  CodingStatisticsType type;
  uint32_t subClass;
};



class CodingStatistics
{
public:

  struct StatLogValue
  {
    uint32_t values[512 + 1];
    StatLogValue()
    {
      const double es = double( CODINGSTATISTICS_ENTROPYSCALE );

      values[0] = 0;

      for( uint32_t i = 1; i < sizeof( values ) / sizeof( uint32_t ); i++ )
      {
        values[i] = uint32_t( log( double( i ) )*es / log( 2.0 ) );
      }
    }
  };

  struct SStat
  {
    SStat() : bits( 0 ), count( 0 ), sum( 0 ), classCount( 0 ) { }

    int64_t bits;
    int64_t count;
    int64_t sum;
    int64_t classCount;

    void clear() { bits = 0; count = 0; sum = 0; classCount = 0; }

    SStat &operator+=( const SStat &src )
    {
      bits += src.bits; count += src.count; sum += src.sum; classCount += src.classCount; return *this;
    }
  };

  struct StatTool
  {
    StatTool() : count( 0 ), pixels( 0 ), classCount( 0 ) { }

    int64_t  count;
    int64_t  pixels;
    int64_t  classCount;

    void clear() { count = 0; pixels = 0; classCount = 0; }

    StatTool &operator+=( const StatTool &src )
    {
      count += src.count; pixels += src.pixels; classCount += src.classCount; return *this;
    }
  };

  class CodingStatisticsData
  {
  private:
    SStat statistics         [STATS__NUM_STATS + 1][CODING_STATS_NUM_SUBCLASSES];
    SStat statistics_ep      [STATS__NUM_STATS + 1][CODING_STATS_NUM_SUBCLASSES];
    StatTool statistics_tool [STATS__NUM_STATS + 1][CODING_STATS_NUM_SUBCLASSES];
    std::map<std::string, SStat> mappings_ep;
    friend class CodingStatistics;
  };

328 329
  int m_mode;

330 331 332 333
private:

  CodingStatisticsData data;

334
  CodingStatistics() : m_mode(STATS__MODE_ALL), data()
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  {
  }

  static void OutputLine( const char *pName, const char sep, uint32_t wIdx, uint32_t hIdx, const char *pSubClassStr, const SStat &sCABAC, const SStat &sEP )
  {
    if( wIdx == 0 && hIdx == 0 )
    {
      OutputLine( pName, sep, "-", "-", pSubClassStr, sCABAC, sEP );
    }
    else
    {
      printf( "%c%-45s%c  %6d %6d %6s ", sep == '~' ? '[' : ' ', pName, sep, gp_sizeIdxInfo->sizeFrom( wIdx ), gp_sizeIdxInfo->sizeFrom( hIdx ), pSubClassStr );
      if( sCABAC.count > 0 )
      {
        const double quote = 100.0 * sCABAC.count / ( double ) sCABAC.classCount;
        const double ratio = 100.0 * sCABAC.bits / ( double ) sCABAC.count;
        printf( "%11.2f%% %12" PRId64 " %12" PRId64 " %12" PRId64 " %11.2f%%", quote, sCABAC.count, sCABAC.sum, sCABAC.bits, ratio );
      }
      else
      {
        printf( "         -/- %12" PRId64 " %12" PRId64 " %12" PRId64 "          -/-", sCABAC.count, sCABAC.sum, sCABAC.bits );
      }
      printf( " %12" PRId64 " %12" PRId64 " %12" PRId64 " %12" PRId64 " (%12" PRId64 ")%c\n", sEP.count, sEP.sum, sEP.bits, sCABAC.bits + sEP.bits, ( sCABAC.bits + sEP.bits ) / 8, sep == '~' ? ']' : ' ' );
    }
  }
  static void OutputLine( const char *pName, const char sep, const char *pWidthString, const char *pHeightString, const char *pSubClassStr, const SStat &sCABAC, const SStat &sEP )
  {
    printf( "%c%-45s%c  %6s %6s %6s ", sep == '~' ? '[' : ' ', pName, sep, pWidthString, pHeightString, pSubClassStr );
    if( sCABAC.count > 0 )
    {
      const double quote = 100.0 * sCABAC.count / ( double ) sCABAC.classCount;
      const double ratio = 100.0 * sCABAC.bits / ( double ) sCABAC.count;
      printf( "%11.2f%% %12" PRId64 " %12" PRId64 " %12" PRId64 " %11.2f%%", quote, sCABAC.count, sCABAC.sum, sCABAC.bits, ratio );
    }
    else
    {
      printf( "         -/- %12" PRId64 " %12" PRId64 " %12" PRId64 "          -/-", sCABAC.count, sCABAC.sum, sCABAC.bits );
    }
    printf( " %12" PRId64 " %12" PRId64 " %12" PRId64 " %12" PRId64 " (%12" PRId64 ")%c\n", sEP.count, sEP.sum, sEP.bits, sCABAC.bits + sEP.bits, ( sCABAC.bits + sEP.bits ) / 8, sep == '~' ? ']' : ' ' );
  }
  static void OutputLine( const char *pName, const char sep, const char *pWidthString, const char *pHeightString, const char *pSubClassStr, const SStat &sEP )
  {
    printf( "%c%-45s%c  %6s %6s %6s          -/- %12s %12s %12s %9s-/- %12" PRId64 " %12" PRId64 " %12" PRId64 " %12" PRId64 " (%12" PRId64 ")%c\n",
            sep == '~' ? '[' : ' ', pName, sep, pWidthString, pHeightString, pSubClassStr,
            "", "", "", "", sEP.count, sEP.sum, sEP.bits, sEP.bits, ( sEP.bits ) / 8, sep == '~' ? ']' : ' ' );
  }

  static void OutputLine( const char *pName, const char sep, const char *pWidthString, const char *pHeightString, const char *pSubClassStr, const StatTool &sTool, uint64_t totalPixels )
  {
    const double ratio = 100.0 * sTool.pixels / ( double ) totalPixels;
    printf( "%c%-45s%c  %6s %6s %6s %12" PRId64 "     %12" PRId64 "       %11.2f%%%c\n",
            sep == '~' ? '[' : ' ', pName, sep, pWidthString, pHeightString, pSubClassStr,
            sTool.count, sTool.pixels, ratio, sep == '~' ? ']' : ' ' );
  }

  static void OutputLine( const char *pName, const char sep, uint32_t wIdx, uint32_t hIdx, const char *pSubClassStr, const StatTool &sTool, uint64_t totalPixels )
  {
    const double ratio = 100.0 * sTool.pixels / ( double ) totalPixels;
    printf( "%c%-45s%c  %6d %6d %6s %12" PRId64 "     %12" PRId64 "       %11.2f%%%c\n",
            sep == '~' ? '[' : ' ', pName, sep, gp_sizeIdxInfo->sizeFrom( wIdx ), gp_sizeIdxInfo->sizeFrom( hIdx ), pSubClassStr,
            sTool.count, sTool.pixels, ratio, sep == '~' ? ']' : ' ' );
  }

  static void OutputDashedLine( const char *pText )
  {
    printf( "--%s", pText );
    uint32_t tot = 0;
    for( ; pText[tot] != 0; tot++ );

    tot += 2;
    for( ; tot < 202; tot++ )
    {
      printf( "-" );
    }
    printf( "\n" );
  }

412
  void OutputBitStats()
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  {
    const int64_t es = CODINGSTATISTICS_ENTROPYSCALE;

    int64_t countTotal = 0;
    int64_t classCounts[STATS__NUM_STATS];
    std::fill_n( classCounts, ( size_t ) STATS__NUM_STATS, 0 );

    int64_t cr = 0; // CABAC remainder, which is added to "STATS__CABAC_INITIALISATION"
    {
      int64_t totalCABACbits = 0, roundedCABACbits = 0;
      for( int i = STATS__NAL_UNIT_PACKING; i < STATS__NUM_STATS; i++ )
      {
        int64_t classCount = 0;

        for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c++ )
        {
          totalCABACbits    += data.statistics[i][c].bits;
          roundedCABACbits  += data.statistics[i][c].bits / es;
          classCount        += data.statistics[i][c].count;
        }

        for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c++ )
        {
          data.statistics[i][c].classCount = classCount;
        }

        classCounts[i] = classCount;
        countTotal    += classCount;
      }
      int64_t remainder = totalCABACbits - roundedCABACbits * es;
      cr = ( remainder + es / 2 ) / es;
    }

    classCounts[0] = countTotal;

    printf( "Note %s will be excluded from the total as it should be the sum of all the other entries (except for %s)\n", getName( STATS__NAL_UNIT_TOTAL_BODY ), getName( STATS__NAL_UNIT_PACKING ) );
    printf( " %-45s-   Width Height   Type  CABAC quote  CABAC count    CABAC Sum   CABAC bits  CABAC ratio     EP Count       EP Sum      EP bits   Total bits ( Total bytes)\n", "Decoder statistics" );

    OutputDashedLine( "" );
    SStat cabacTotalBits, epTotalBits;

    cabacTotalBits.classCount = countTotal;
    epTotalBits   .classCount = countTotal;

    SStat statTotals_cabac[CODING_STATS_NUM_SUBCLASSES];
    SStat statTotals_ep   [CODING_STATS_NUM_SUBCLASSES];

    for( int i = 0; i < STATS__NUM_STATS; i++ )
    {
      SStat cabacSubTotal, epSubTotal;
      cabacSubTotal.classCount = classCounts[i];
      epSubTotal   .classCount = classCounts[i];
      bool bHadClassifiedEntry = false;

      const char *pName = getName( CodingStatisticsType( i ) );

      for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c++ )
      {
        SStat &sCABACorig = data.statistics[i][c];
        SStat &sEP        = data.statistics_ep[i][c];

        if( sCABACorig.bits == 0 && sEP.bits == 0 )
        {
          continue;
        }

        SStat sCABAC;
        {
          int64_t thisCABACbits = sCABACorig.bits / es;
          if( i == STATS__CABAC_INITIALISATION && sCABACorig.bits != 0 )
          {
            thisCABACbits += cr;
            cr = 0;
          }
          sCABAC.bits       = thisCABACbits;
          sCABAC.count      = sCABACorig.count;
          sCABAC.sum        = sCABACorig.sum;
          sCABAC.classCount = classCounts[i];
        }
        uint32_t wIdx = CodingStatisticsClassType::GetSubClassWidth( c );
        uint32_t hIdx = CodingStatisticsClassType::GetSubClassHeight( c );
        OutputLine( pName, ':', wIdx, hIdx, CodingStatisticsClassType::GetSubClassString( c ), sCABAC, sEP );
        cabacSubTotal += sCABAC;
        epSubTotal    += sEP;

        if( i != STATS__NAL_UNIT_TOTAL_BODY )
        {
          cabacTotalBits      += sCABAC;
          epTotalBits         += sEP;
          statTotals_cabac[c] += sCABAC;
          statTotals_ep[c]    += sEP;

        }
        bHadClassifiedEntry = bHadClassifiedEntry || ( c != 0 );
      }

      if( bHadClassifiedEntry )
      {
        cabacSubTotal.classCount = classCounts[i];
        OutputLine( pName, '~', "~~ST~~", "~~ST~~", "~~ST~~", cabacSubTotal, epSubTotal );
      }
      if( i == STATS__NAL_UNIT_TOTAL_BODY )
      {
        OutputDashedLine( "" );
      }
    }
    OutputDashedLine( "" );
    OutputLine( "CABAC Sub-total", '~', "~~ST~~", "~~ST~~", "~~ST~~", cabacTotalBits, epTotalBits );

    OutputDashedLine( "CAVLC HEADER BITS" );
    SStat cavlcTotalBits;
    for( std::map<std::string, SStat>::iterator it = data.mappings_ep.begin(); it != data.mappings_ep.end(); it++ )
    {
      SStat s = it->second;
      cavlcTotalBits += s;
      OutputLine( it->first.c_str(), ':', "-", "-", "-", s );
    }

    OutputDashedLine( "" );
    OutputLine( "CAVLC Header Sub-total", '~', "~~ST~~", "~~ST~~", "~~ST~~", cavlcTotalBits );

    // Now output the breakdowns
    OutputDashedLine( "CABAC Break down by size" );
    for( uint32_t w = 0; w < CODING_STATS_NUM_WIDTHS; w++ )
    {
      for( uint32_t h = 0; h < CODING_STATS_NUM_HEIGHTS; h++ )
      {
        SStat subTotalCabac, subTotalEP;
        for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c += CODING_STATS_NUM_SIZES )
        {
          subTotalCabac += statTotals_cabac[c + h * CODING_STATS_NUM_WIDTHS + w];
          subTotalEP    += statTotals_ep   [c + h * CODING_STATS_NUM_WIDTHS + w];
        }
        if( subTotalCabac.bits != 0 || subTotalEP.bits != 0 )
        {
          OutputLine( "CABAC by size Sub-total", '=', w, h, "All", subTotalCabac, subTotalEP );
        }
      }
    }
    OutputDashedLine( "Break down by component/Channel type" );
    for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c += CODING_STATS_NUM_SIZES )
    {
      SStat subTotalCabac, subTotalEP;
      for( uint32_t w = 0; w < CODING_STATS_NUM_WIDTHS; w++ )
      {
        for( uint32_t h = 0; h < CODING_STATS_NUM_HEIGHTS; h++ )
        {
          subTotalCabac += statTotals_cabac[c + h * CODING_STATS_NUM_WIDTHS + w];
          subTotalEP    += statTotals_ep   [c + h * CODING_STATS_NUM_WIDTHS + w];
        }
      }
      if( subTotalCabac.bits != 0 || subTotalEP.bits != 0 )
      {
        OutputLine( "CABAC by type Sub-total", '=', "-", "-", CodingStatisticsClassType::GetSubClassString( c ), subTotalCabac, subTotalEP );
      }
    }
    OutputDashedLine( "Break down by size and component/Channel type" );
    for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c += CODING_STATS_NUM_SIZES )
    {
      for( uint32_t w = 0; w < CODING_STATS_NUM_WIDTHS; w++ )
      {
        for( uint32_t h = 0; h < CODING_STATS_NUM_HEIGHTS; h++ )
        {
          SStat subTotalCabac, subTotalEP;
          subTotalCabac += statTotals_cabac[c + h * CODING_STATS_NUM_WIDTHS + w];
          subTotalEP    += statTotals_ep   [c + h * CODING_STATS_NUM_WIDTHS + w];

          if( subTotalCabac.bits != 0 || subTotalEP.bits != 0 )
          {
            OutputLine( "CABAC by size and type Sub-total", '=', w, h, CodingStatisticsClassType::GetSubClassString( c ), subTotalCabac, subTotalEP );
          }
        }
      }
    }

    OutputDashedLine( "" );
    cabacTotalBits.classCount = countTotal;
    OutputLine      ( "CABAC Sub-total",        '~', "~~ST~~", "~~ST~~", "~~ST~~", cabacTotalBits, epTotalBits );
    OutputLine      ( "CAVLC Header Sub-total", '~', "~~ST~~", "~~ST~~", "~~ST~~", cavlcTotalBits );
    OutputDashedLine( "GRAND TOTAL" );
    epTotalBits += cavlcTotalBits;
    OutputLine      ( "TOTAL",                  '~', "~~GT~~", "~~GT~~", "~~GT~~", cabacTotalBits, epTotalBits );
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
  }

  void OutputToolStats()
  {
    printf("\n");
    printf( " %-45s-   Width Height   Type        Count  Impacted pixels  %% Impacted pixels\n", "Tools statistics" );
    OutputDashedLine( "" );

    const uint64_t toolCount = STATS__TOOL_TOTAL - (STATS__TOOL_TOTAL_FRAME + 1);
    StatTool subTotalTool[toolCount];
    StatTool statTotalTool[toolCount][CODING_STATS_NUM_SUBCLASSES];
    uint64_t totalPixels = GetStatisticTool( STATS__TOOL_TOTAL_FRAME ).pixels;
    for( int i = 0; i < toolCount; i++ )
    {
      const int type = i + (STATS__TOOL_TOTAL_FRAME + 1);
      const char *pName = getName( CodingStatisticsType( type ) );

      for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c++ )
      {
        StatTool &sTool   = data.statistics_tool[type][c];
        if( sTool.count == 0 )
        {
          continue;
        }

        uint32_t wIdx = CodingStatisticsClassType::GetSubClassWidth( c );
        uint32_t hIdx = CodingStatisticsClassType::GetSubClassHeight( c );
        OutputLine( pName, ':', wIdx, hIdx, CodingStatisticsClassType::GetSubClassString( c ), sTool, totalPixels );

        statTotalTool[i][c] += sTool;
        subTotalTool[i] += sTool;
      }

      if (subTotalTool[i].count != 0)
      {
        OutputLine( pName, '~', "~~ST~~", "~~ST~~", "~~ST~~", subTotalTool[i], totalPixels );
      }
    }

    for( int i = 0; i < toolCount; i++ )
    {
      const int type = i + (STATS__TOOL_TOTAL_FRAME + 1);
      const char *pName = getName( CodingStatisticsType( type ) );

      if (subTotalTool[i].count != 0)
        OutputDashedLine( "Break down by tool/Channel type" );

      for( uint32_t c = 0; c < CODING_STATS_NUM_SUBCLASSES; c += CODING_STATS_NUM_SIZES )
      {
        StatTool typeTotalTool;
        for( uint32_t w = 0; w < CODING_STATS_NUM_WIDTHS; w++ )
        {
          for( uint32_t h = 0; h < CODING_STATS_NUM_HEIGHTS; h++ )
            typeTotalTool += statTotalTool[i][c + h * CODING_STATS_NUM_WIDTHS + w];
        }

        if( typeTotalTool.count != 0 )
        {
          OutputLine( pName, '=', "-", "-", CodingStatisticsClassType::GetSubClassString( c ), typeTotalTool, totalPixels );
        }
      }
    }
  }

public:

  ~CodingStatistics()
  {
#if RExt__DECODER_DEBUG_BIT_STATISTICS
    if (m_mode & STATS__MODE_BITS)
      OutputBitStats();
666 667 668
#endif //RExt__DECODER_DEBUG_BIT_STATISTICS

#ifdef RExt__DECODER_DEBUG_TOOL_STATISTICS
669 670
    if (m_mode & STATS__MODE_TOOLS)
      OutputToolStats();
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
#endif //RExt__DECODER_DEBUG_TOOL_STATISTICS
  }

   static CodingStatistics& GetSingletonInstance()
  {
    static CodingStatistics* inst = nullptr;
    if( !inst )
    {
      inst = new CodingStatistics;
    }

    return *inst;
  }

  static void DestroyInstance()
  {
    CodingStatistics* cs = &GetSingletonInstance();
    delete cs;
  }

  static const CodingStatisticsData &GetStatistics()                        { return GetSingletonInstance().data; }

  static void SetStatistics       ( const CodingStatisticsData &src )       { GetSingletonInstance().data = src; }

  static SStat &GetStatisticEP    ( const CodingStatisticsClassType &stat ) { return GetSingletonInstance().data.statistics_ep[stat.type][stat.subClass]; }

  static SStat &GetStatisticEP    ( const std::string &str )                { return GetSingletonInstance().data.mappings_ep[str]; }

  static SStat &GetStatisticEP    ( const char *pKey )                     { return GetStatisticEP( std::string( pKey ) ); }

  static StatTool &GetStatisticTool ( const CodingStatisticsClassType &stat ) { return GetSingletonInstance().data.statistics_tool[stat.type][stat.subClass]; }

  static int getNumOnes( int bins )
  {
    CHECK( bins < 0, "Bins should not be nagative" );

    int count = 0;
    while( bins )
    {
      count += bins & 1;
      bins >>= 1;
    }
    return count;
  }

  static void IncrementStatisticEP( const CodingStatisticsClassType &stat, const int numBits, const int value )
  {
    CHECK( stat.type == STATS__CABAC_BITS__INVALID, "Should never be used." );
    SStat &s = GetStatisticEP( stat );
    s.bits  += numBits;
    s.count++;
    s.sum   += getNumOnes( value );
  }

  static void IncrementStatisticEP( const std::string &str, const int numBits, const int value )
  {
    SStat &s = GetStatisticEP( str );
    s.bits  += numBits;
    s.count++;
    s.sum   += getNumOnes( value );
  }

  static void IncrementStatisticEP( const char *pKey, const int numBits, const int value )
  {
    SStat &s = GetStatisticEP( pKey );
    s.bits  += numBits;
    s.count++;
    s.sum   += getNumOnes( value );
  }

  static void IncrementStatisticTool( const CodingStatisticsClassType &stat )
  {
    CHECK( stat.type < STATS__TOOL_TOTAL_FRAME || stat.type >= STATS__TOOL_TOTAL, "Should never be used." );
    StatTool &s = GetStatisticTool( stat );
    s.count++;

    uint32_t wIdx = CodingStatisticsClassType::GetSubClassWidth( stat.subClass );
    uint32_t hIdx = CodingStatisticsClassType::GetSubClassHeight( stat.subClass );

    s.pixels = s.count * gp_sizeIdxInfo->sizeFrom( wIdx ) * gp_sizeIdxInfo->sizeFrom( hIdx );
  }

  StatLogValue values;

  static void UpdateCABACStat( const CodingStatisticsClassType &stat, uint32_t uiRangeBefore, uint32_t uiRangeAfter, int val )
  {
    CHECK( stat.type == STATS__CABAC_BITS__INVALID, "Should never be used." );
    CodingStatistics &inst = GetSingletonInstance();
    // doing rangeBefore*p(x)=rangeAfter
    // p(x)=rangeAfter/rangeBefore
    // entropy = -log2(p(x))=-log(p(x))/log(2) = -(log rangeAfter - log rangeBefore) / log(2) = (log rangeBefore / log 2 - log rangeAfter / log 2)
    SStat &s = inst.data.statistics[stat.type][stat.subClass];
    s.bits  += inst.values.values[uiRangeBefore] - inst.values.values[uiRangeAfter];
    s.count++;
    s.sum   += val;
  }
};

#endif