11cb0ef41Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci   Distributed under MIT license.
41cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
51cb0ef41Sopenharmony_ci*/
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci/* Functions to estimate the bit cost of Huffman trees. */
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#ifndef BROTLI_ENC_BIT_COST_H_
101cb0ef41Sopenharmony_ci#define BROTLI_ENC_BIT_COST_H_
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include "../common/platform.h"
131cb0ef41Sopenharmony_ci#include <brotli/types.h>
141cb0ef41Sopenharmony_ci#include "./fast_log.h"
151cb0ef41Sopenharmony_ci#include "./histogram.h"
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
181cb0ef41Sopenharmony_ciextern "C" {
191cb0ef41Sopenharmony_ci#endif
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cistatic BROTLI_INLINE double ShannonEntropy(
221cb0ef41Sopenharmony_ci    const uint32_t* population, size_t size, size_t* total) {
231cb0ef41Sopenharmony_ci  size_t sum = 0;
241cb0ef41Sopenharmony_ci  double retval = 0;
251cb0ef41Sopenharmony_ci  const uint32_t* population_end = population + size;
261cb0ef41Sopenharmony_ci  size_t p;
271cb0ef41Sopenharmony_ci  if (size & 1) {
281cb0ef41Sopenharmony_ci    goto odd_number_of_elements_left;
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci  while (population < population_end) {
311cb0ef41Sopenharmony_ci    p = *population++;
321cb0ef41Sopenharmony_ci    sum += p;
331cb0ef41Sopenharmony_ci    retval -= (double)p * FastLog2(p);
341cb0ef41Sopenharmony_ci odd_number_of_elements_left:
351cb0ef41Sopenharmony_ci    p = *population++;
361cb0ef41Sopenharmony_ci    sum += p;
371cb0ef41Sopenharmony_ci    retval -= (double)p * FastLog2(p);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci  if (sum) retval += (double)sum * FastLog2(sum);
401cb0ef41Sopenharmony_ci  *total = sum;
411cb0ef41Sopenharmony_ci  return retval;
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cistatic BROTLI_INLINE double BitsEntropy(
451cb0ef41Sopenharmony_ci    const uint32_t* population, size_t size) {
461cb0ef41Sopenharmony_ci  size_t sum;
471cb0ef41Sopenharmony_ci  double retval = ShannonEntropy(population, size, &sum);
481cb0ef41Sopenharmony_ci  if (retval < sum) {
491cb0ef41Sopenharmony_ci    /* At least one bit per literal is needed. */
501cb0ef41Sopenharmony_ci    retval = (double)sum;
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci  return retval;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciBROTLI_INTERNAL double BrotliPopulationCostLiteral(const HistogramLiteral*);
561cb0ef41Sopenharmony_ciBROTLI_INTERNAL double BrotliPopulationCostCommand(const HistogramCommand*);
571cb0ef41Sopenharmony_ciBROTLI_INTERNAL double BrotliPopulationCostDistance(const HistogramDistance*);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
601cb0ef41Sopenharmony_ci}  /* extern "C" */
611cb0ef41Sopenharmony_ci#endif
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci#endif  /* BROTLI_ENC_BIT_COST_H_ */
64