1 /*
2  * Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 /* clang-format off */
13 
14 #if !defined(_pvq_H)
15 # define _pvq_H (1)
16 # include "generic_code.h"
17 # include "odintrin.h"
18 
19 extern const uint16_t EXP_CDF_TABLE[][16];
20 extern const uint16_t LAPLACE_OFFSET[];
21 
22 #define AV1_PVQ_ENABLE_ACTIVITY_MASKING (0)
23 
24 # define PVQ_MAX_PARTITIONS (1 + 3*(OD_TXSIZES-1))
25 
26 # define OD_NOREF_ADAPT_SPEED (4)
27 /* Normalized lambda for PVQ quantizer. Since we normalize the gain by q, the
28    distortion is normalized by q^2 and lambda does not need the q^2 factor.
29    At high rate, this would be log(2)/6, but we're using a slightly more
30    aggressive value, closer to:
31    Li, Xiang, et al. "Laplace distribution based Lagrangian rate distortion
32    optimization for hybrid video coding." Circuits and Systems for Video
33    Technology, IEEE Transactions on 19.2 (2009): 193-205.
34    */
35 # define OD_PVQ_LAMBDA (.1146)
36 
37 #define OD_PVQ_SKIP_ZERO 1
38 #define OD_PVQ_SKIP_COPY 2
39 
40 /* Maximum size for coding a PVQ band. */
41 #define OD_MAX_PVQ_SIZE (1024)
42 
43 #if defined(OD_FLOAT_PVQ)
44 #define OD_QM_SHIFT (15)
45 #else
46 #define OD_QM_SHIFT (11)
47 #endif
48 #define OD_QM_SCALE (1 << OD_QM_SHIFT)
49 #if defined(OD_FLOAT_PVQ)
50 #define OD_QM_SCALE_1 (1./OD_QM_SCALE)
51 #endif
52 #define OD_QM_SCALE_MAX 32767
53 #define OD_QM_INV_SHIFT (12)
54 #define OD_QM_INV_SCALE (1 << OD_QM_INV_SHIFT)
55 #if defined(OD_FLOAT_PVQ)
56 #define OD_QM_INV_SCALE_1 (1./OD_QM_INV_SCALE)
57 #endif
58 #define OD_QM_OFFSET(bs) ((((1 << 2*bs) - 1) << 2*OD_LOG_BSIZE0)/3)
59 #define OD_QM_STRIDE (OD_QM_OFFSET(OD_TXSIZES))
60 #define OD_QM_BUFFER_SIZE (2*OD_QM_STRIDE)
61 
62 #if !defined(OD_FLOAT_PVQ)
63 #define OD_THETA_SHIFT (15)
64 #define OD_THETA_SCALE ((1 << OD_THETA_SHIFT)*2./M_PI)
65 #define OD_MAX_THETA_SCALE (1 << OD_THETA_SHIFT)
66 #define OD_TRIG_SCALE (32768)
67 #define OD_BETA_SHIFT (12)
68 #define OD_BETA_SCALE_1 (1./(1 << OD_BETA_SHIFT))
69 /*Multiplies 16-bit a by 32-bit b and keeps bits [16:64-OD_BETA_SHIFT-1].*/
70 #define OD_MULT16_32_QBETA(a, b) \
71  ((int16_t)(a)*(int64_t)(int32_t)(b) >> OD_BETA_SHIFT)
72 # define OD_MULT16_16_QBETA(a, b) \
73   ((((int16_t)(a))*((int32_t)(int16_t)(b))) >> OD_BETA_SHIFT)
74 #define OD_CGAIN_SHIFT (8)
75 #define OD_CGAIN_SCALE (1 << OD_CGAIN_SHIFT)
76 #else
77 #define OD_BETA_SCALE_1 (1.)
78 #define OD_THETA_SCALE (1)
79 #define OD_TRIG_SCALE (1)
80 #define OD_CGAIN_SCALE (1)
81 #endif
82 #define OD_THETA_SCALE_1 (1./OD_THETA_SCALE)
83 #define OD_TRIG_SCALE_1 (1./OD_TRIG_SCALE)
84 #define OD_CGAIN_SCALE_1 (1./OD_CGAIN_SCALE)
85 #define OD_CGAIN_SCALE_2 (OD_CGAIN_SCALE_1*OD_CGAIN_SCALE_1)
86 
87 /* Largest PVQ partition is half the coefficients of largest block size. */
88 #define MAXN (OD_TXSIZE_MAX*OD_TXSIZE_MAX/2)
89 
90 #define OD_COMPAND_SHIFT (8 + OD_COEFF_SHIFT)
91 #define OD_COMPAND_SCALE (1 << OD_COMPAND_SHIFT)
92 #define OD_COMPAND_SCALE_1 (1./OD_COMPAND_SCALE)
93 
94 #define OD_QM_SIZE (OD_TXSIZES*(OD_TXSIZES + 1))
95 
96 #define OD_FLAT_QM 0
97 #define OD_HVS_QM  1
98 
99 # define OD_NSB_ADAPT_CTXS (4)
100 
101 # define OD_ADAPT_K_Q8        0
102 # define OD_ADAPT_SUM_EX_Q8   1
103 # define OD_ADAPT_COUNT_Q8    2
104 # define OD_ADAPT_COUNT_EX_Q8 3
105 
106 # define OD_ADAPT_NO_VALUE (-2147483647-1)
107 
108 typedef enum {
109   PVQ_SKIP = 0x0,
110   DC_CODED = 0x1,
111   AC_CODED = 0x2,
112   AC_DC_CODED = 0x3,
113 } PVQ_SKIP_TYPE;
114 
115 typedef struct od_pvq_adapt_ctx  od_pvq_adapt_ctx;
116 typedef struct od_pvq_codeword_ctx od_pvq_codeword_ctx;
117 
118 struct od_pvq_codeword_ctx {
119   int                 pvq_adapt[2*OD_TXSIZES*OD_NSB_ADAPT_CTXS];
120   /* CDFs are size 16 despite the fact that we're using less than that. */
121   uint16_t            pvq_k1_cdf[12][CDF_SIZE(16)];
122   uint16_t            pvq_split_cdf[22*7][CDF_SIZE(8)];
123 };
124 
125 struct od_pvq_adapt_ctx {
126   od_pvq_codeword_ctx pvq_codeword_ctx;
127   generic_encoder     pvq_param_model[3];
128   int                 pvq_ext[OD_TXSIZES*PVQ_MAX_PARTITIONS];
129   int                 pvq_exg[OD_NPLANES_MAX][OD_TXSIZES][PVQ_MAX_PARTITIONS];
130   uint16_t pvq_gaintheta_cdf[2*OD_TXSIZES*PVQ_MAX_PARTITIONS][CDF_SIZE(16)];
131   uint16_t pvq_skip_dir_cdf[2*(OD_TXSIZES-1)][CDF_SIZE(7)];
132 };
133 
134 typedef struct od_qm_entry {
135   int interp_q;
136   int scale_q8;
137   const unsigned char *qm_q4;
138 } od_qm_entry;
139 
140 extern const od_qm_entry OD_DEFAULT_QMS[2][2][OD_NPLANES_MAX];
141 
142 void od_adapt_pvq_ctx_reset(od_pvq_adapt_ctx *state, int is_keyframe);
143 int od_pvq_size_ctx(int n);
144 int od_pvq_k1_ctx(int n, int orig_size);
145 
146 od_val16 od_pvq_sin(od_val32 x);
147 od_val16 od_pvq_cos(od_val32 x);
148 #if !defined(OD_FLOAT_PVQ)
149 int od_vector_log_mag(const od_coeff *x, int n);
150 #endif
151 
152 void od_interp_qm(unsigned char *out, int q, const od_qm_entry *entry1,
153                   const od_qm_entry *entry2);
154 
155 int od_qm_get_index(int bs, int band);
156 
157 extern const od_val16 *const OD_PVQ_BETA[2][OD_NPLANES_MAX][OD_TXSIZES + 1];
158 
159 void od_init_qm(int16_t *x, int16_t *x_inv, const int *qm);
160 int od_compute_householder(od_val16 *r, int n, od_val32 gr, int *sign,
161  int shift);
162 void od_apply_householder(od_val16 *out, const od_val16 *x, const od_val16 *r,
163  int n);
164 void od_pvq_synthesis_partial(od_coeff *xcoeff, const od_coeff *ypulse,
165                                   const od_val16 *r, int n,
166                                   int noref, od_val32 g,
167                                   od_val32 theta, int m, int s,
168                                   const int16_t *qm_inv);
169 od_val32 od_gain_expand(od_val32 cg, int q0, od_val16 beta);
170 od_val32 od_pvq_compute_gain(const od_val16 *x, int n, int q0, od_val32 *g,
171  od_val16 beta, int bshift);
172 int od_pvq_compute_max_theta(od_val32 qcg, od_val16 beta);
173 od_val32 od_pvq_compute_theta(int t, int max_theta);
174 int od_pvq_compute_k(od_val32 qcg, int itheta, int noref, int n, od_val16 beta);
175 
176 int od_vector_is_null(const od_coeff *x, int len);
177 int od_qm_offset(int bs, int xydec);
178 
179 #endif
180