xref: /aosp_15_r20/external/webp/src/enc/cost_enc.h (revision b2055c353e87c8814eb2b6b1b11112a1562253bd)
1*b2055c35SXin Li // Copyright 2011 Google Inc. All Rights Reserved.
2*b2055c35SXin Li //
3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license
4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source
5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found
6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may
7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree.
8*b2055c35SXin Li // -----------------------------------------------------------------------------
9*b2055c35SXin Li //
10*b2055c35SXin Li // Cost tables for level and modes.
11*b2055c35SXin Li //
12*b2055c35SXin Li // Author: Skal ([email protected])
13*b2055c35SXin Li 
14*b2055c35SXin Li #ifndef WEBP_ENC_COST_ENC_H_
15*b2055c35SXin Li #define WEBP_ENC_COST_ENC_H_
16*b2055c35SXin Li 
17*b2055c35SXin Li #include <assert.h>
18*b2055c35SXin Li #include <stdlib.h>
19*b2055c35SXin Li #include "src/enc/vp8i_enc.h"
20*b2055c35SXin Li 
21*b2055c35SXin Li #ifdef __cplusplus
22*b2055c35SXin Li extern "C" {
23*b2055c35SXin Li #endif
24*b2055c35SXin Li 
25*b2055c35SXin Li // On-the-fly info about the current set of residuals. Handy to avoid
26*b2055c35SXin Li // passing zillions of params.
27*b2055c35SXin Li typedef struct VP8Residual VP8Residual;
28*b2055c35SXin Li struct VP8Residual {
29*b2055c35SXin Li   int first;
30*b2055c35SXin Li   int last;
31*b2055c35SXin Li   const int16_t* coeffs;
32*b2055c35SXin Li 
33*b2055c35SXin Li   int coeff_type;
34*b2055c35SXin Li   ProbaArray*   prob;
35*b2055c35SXin Li   StatsArray*   stats;
36*b2055c35SXin Li   CostArrayPtr  costs;
37*b2055c35SXin Li };
38*b2055c35SXin Li 
39*b2055c35SXin Li void VP8InitResidual(int first, int coeff_type,
40*b2055c35SXin Li                      VP8Encoder* const enc, VP8Residual* const res);
41*b2055c35SXin Li 
42*b2055c35SXin Li int VP8RecordCoeffs(int ctx, const VP8Residual* const res);
43*b2055c35SXin Li 
44*b2055c35SXin Li // Record proba context used.
VP8RecordStats(int bit,proba_t * const stats)45*b2055c35SXin Li static WEBP_INLINE int VP8RecordStats(int bit, proba_t* const stats) {
46*b2055c35SXin Li   proba_t p = *stats;
47*b2055c35SXin Li   // An overflow is inbound. Note we handle this at 0xfffe0000u instead of
48*b2055c35SXin Li   // 0xffff0000u to make sure p + 1u does not overflow.
49*b2055c35SXin Li   if (p >= 0xfffe0000u) {
50*b2055c35SXin Li     p = ((p + 1u) >> 1) & 0x7fff7fffu;  // -> divide the stats by 2.
51*b2055c35SXin Li   }
52*b2055c35SXin Li   // record bit count (lower 16 bits) and increment total count (upper 16 bits).
53*b2055c35SXin Li   p += 0x00010000u + bit;
54*b2055c35SXin Li   *stats = p;
55*b2055c35SXin Li   return bit;
56*b2055c35SXin Li }
57*b2055c35SXin Li 
58*b2055c35SXin Li // Cost of coding one event with probability 'proba'.
VP8BitCost(int bit,uint8_t proba)59*b2055c35SXin Li static WEBP_INLINE int VP8BitCost(int bit, uint8_t proba) {
60*b2055c35SXin Li   return !bit ? VP8EntropyCost[proba] : VP8EntropyCost[255 - proba];
61*b2055c35SXin Li }
62*b2055c35SXin Li 
63*b2055c35SXin Li // Level cost calculations
64*b2055c35SXin Li extern const uint16_t VP8LevelCodes[MAX_VARIABLE_LEVEL][2];
65*b2055c35SXin Li void VP8CalculateLevelCosts(VP8EncProba* const proba);
VP8LevelCost(const uint16_t * const table,int level)66*b2055c35SXin Li static WEBP_INLINE int VP8LevelCost(const uint16_t* const table, int level) {
67*b2055c35SXin Li   return VP8LevelFixedCosts[level]
68*b2055c35SXin Li        + table[(level > MAX_VARIABLE_LEVEL) ? MAX_VARIABLE_LEVEL : level];
69*b2055c35SXin Li }
70*b2055c35SXin Li 
71*b2055c35SXin Li // Mode costs
72*b2055c35SXin Li extern const uint16_t VP8FixedCostsUV[4];
73*b2055c35SXin Li extern const uint16_t VP8FixedCostsI16[4];
74*b2055c35SXin Li extern const uint16_t VP8FixedCostsI4[NUM_BMODES][NUM_BMODES][NUM_BMODES];
75*b2055c35SXin Li 
76*b2055c35SXin Li //------------------------------------------------------------------------------
77*b2055c35SXin Li 
78*b2055c35SXin Li #ifdef __cplusplus
79*b2055c35SXin Li }    // extern "C"
80*b2055c35SXin Li #endif
81*b2055c35SXin Li 
82*b2055c35SXin Li #endif  // WEBP_ENC_COST_ENC_H_
83