1 /*****************************************************************************
2  * This file is part of Kvazaar HEVC encoder.
3  *
4  * Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  *   list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice, this
14  *   list of conditions and the following disclaimer in the documentation and/or
15  *   other materials provided with the distribution.
16  *
17  * * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
28  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  * INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
31  ****************************************************************************/
32 
33 #ifndef FAST_COEFF_COST_H_
34 #define FAST_COEFF_COST_H_
35 
36 #include <stdio.h>
37 #include "kvazaar.h"
38 // #include "encoderstate.h"
39 
40 #define MAX_FAST_COEFF_COST_QP 50
41 
42 typedef struct {
43   uint64_t wts_by_qp[MAX_FAST_COEFF_COST_QP];
44 } fast_coeff_table_t;
45 
46 // Weights for 4 buckets (coeff 0, coeff 1, coeff 2, coeff >= 3), for QPs from
47 // 0 to MAX_FAST_COEFF_COST_QP
48 static const float default_fast_coeff_cost_wts[][4] = {
49   // Just extend it by stretching the first actual values..
50   {0.164240, 4.161530, 3.509033, 6.928047},
51   {0.164240, 4.161530, 3.509033, 6.928047},
52   {0.164240, 4.161530, 3.509033, 6.928047},
53   {0.164240, 4.161530, 3.509033, 6.928047},
54   {0.164240, 4.161530, 3.509033, 6.928047},
55   {0.164240, 4.161530, 3.509033, 6.928047},
56   {0.164240, 4.161530, 3.509033, 6.928047},
57   {0.164240, 4.161530, 3.509033, 6.928047},
58   {0.164240, 4.161530, 3.509033, 6.928047},
59   {0.164240, 4.161530, 3.509033, 6.928047},
60   // up to here
61   {0.164240, 4.161530, 3.509033, 6.928047},
62   {0.162844, 4.055940, 3.564467, 6.861493},
63   {0.128729, 4.311973, 3.942837, 6.935403},
64   {0.110956, 4.433190, 3.945753, 6.877697},
65   {0.095026, 4.483547, 4.194173, 6.781540},
66   {0.075046, 4.633703, 4.084193, 6.698600},
67   {0.052426, 4.967223, 4.027210, 6.549197},
68   {0.040219, 5.141820, 3.982650, 6.461557},
69   {0.035090, 5.192493, 3.830950, 6.418477},
70   {0.029845, 5.211647, 3.815457, 6.345440},
71   {0.023522, 5.322213, 3.816537, 6.360677},
72   {0.021305, 5.225923, 3.842700, 6.325787},
73   {0.015878, 5.183090, 3.956003, 6.329680},
74   {0.010430, 5.099230, 4.176803, 6.305400},
75   {0.008433, 5.030257, 4.237587, 6.270133},
76   {0.006500, 4.969247, 4.339397, 6.217827},
77   {0.004929, 4.923500, 4.442413, 6.183523},
78   {0.003715, 4.915583, 4.429090, 6.125320},
79   {0.003089, 4.883907, 4.562790, 6.156447},
80   {0.002466, 4.881063, 4.629883, 6.142643},
81   {0.002169, 4.882493, 4.646313, 6.127663},
82   {0.002546, 4.793337, 4.837413, 6.199270},
83   {0.001314, 4.808853, 4.828337, 6.243437},
84   {0.001154, 4.862603, 4.846883, 6.205523},
85   {0.000984, 4.866403, 4.859330, 6.240893},
86   {0.000813, 4.856633, 4.924527, 6.293413},
87   {0.001112, 4.789260, 5.009880, 6.433540},
88   {0.000552, 4.760747, 5.090447, 6.599380},
89   {0.000391, 4.961447, 5.111033, 6.756370},
90   {0.000332, 4.980953, 5.138127, 6.867420},
91   {0.000201, 5.181957, 4.740160, 6.460997},
92   {0.000240, 5.185390, 4.874840, 6.819093},
93   {0.000130, 5.270350, 4.734213, 6.826240},
94   {0.000104, 5.371937, 4.595087, 6.659253},
95   {0.000083, 5.362000, 4.617470, 6.837770},
96   {0.000069, 5.285997, 4.754993, 7.159043},
97   {0.000049, 5.488470, 4.396107, 6.727357},
98   {0.000058, 4.958940, 4.580460, 6.477740},
99   {0.000028, 5.521253, 4.440493, 7.205017},
100   {0.000000, 0.000000, 0.000000, 0.000000},
101   {0.000019, 5.811260, 4.399110, 7.336310},
102 };
103 
104 typedef struct encoder_state_t encoder_state_t;
105 
106 int kvz_fast_coeff_table_parse(fast_coeff_table_t *fast_coeff_table, FILE *fast_coeff_table_f);
107 void kvz_fast_coeff_use_default_table(fast_coeff_table_t *fast_coeff_table);
108 uint64_t kvz_fast_coeff_get_weights(const encoder_state_t *state);
109 
110 #endif // FAST_COEFF_COST_H_
111