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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
179
180
181
182
183
184
185
186
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
|
/*
* DCA ADPCM engine
* Copyright (C) 2017 Daniil Cherednik
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/mem.h"
#include "dcaadpcm.h"
#include "dcaenc.h"
#include "dca_core.h"
#include "mathops.h"
typedef int32_t premultiplied_coeffs[10];
//assume we have DCA_ADPCM_COEFFS values before x
static inline int64_t calc_corr(const int32_t *x, int len, int j, int k)
{
int n;
int64_t s = 0;
for (n = 0; n < len; n++)
s += MUL64(x[n-j], x[n-k]);
return s;
}
static inline int64_t apply_filter(const int16_t a[DCA_ADPCM_COEFFS], const int64_t corr[15], const int32_t aa[10])
{
int64_t err = 0;
int64_t tmp = 0;
err = corr[0];
tmp += MUL64(a[0], corr[1]);
tmp += MUL64(a[1], corr[2]);
tmp += MUL64(a[2], corr[3]);
tmp += MUL64(a[3], corr[4]);
tmp = norm__(tmp, 13);
tmp += tmp;
err -= tmp;
tmp = 0;
tmp += MUL64(corr[5], aa[0]);
tmp += MUL64(corr[6], aa[1]);
tmp += MUL64(corr[7], aa[2]);
tmp += MUL64(corr[8], aa[3]);
tmp += MUL64(corr[9], aa[4]);
tmp += MUL64(corr[10], aa[5]);
tmp += MUL64(corr[11], aa[6]);
tmp += MUL64(corr[12], aa[7]);
tmp += MUL64(corr[13], aa[8]);
tmp += MUL64(corr[14], aa[9]);
tmp = norm__(tmp, 26);
err += tmp;
return llabs(err);
}
static int64_t find_best_filter(const DCAADPCMEncContext *s, const int32_t *in, int len)
{
const premultiplied_coeffs *precalc_data = s->private_data;
int i, j, k = 0;
int vq = -1;
int64_t err;
int64_t min_err = 1ll << 62;
int64_t corr[15];
for (i = 0; i <= DCA_ADPCM_COEFFS; i++)
for (j = i; j <= DCA_ADPCM_COEFFS; j++)
corr[k++] = calc_corr(in+4, len, i, j);
for (i = 0; i < DCA_ADPCM_VQCODEBOOK_SZ; i++) {
err = apply_filter(ff_dca_adpcm_vb[i], corr, *precalc_data);
if (err < min_err) {
min_err = err;
vq = i;
}
precalc_data++;
}
return vq;
}
static inline int64_t calc_prediction_gain(int pred_vq, const int32_t *in, int32_t *out, int len)
{
int i;
int32_t error;
int64_t signal_energy = 0;
int64_t error_energy = 0;
for (i = 0; i < len; i++) {
error = in[DCA_ADPCM_COEFFS + i] - ff_dcaadpcm_predict(pred_vq, in + i);
out[i] = error;
signal_energy += MUL64(in[DCA_ADPCM_COEFFS + i], in[DCA_ADPCM_COEFFS + i]);
error_energy += MUL64(error, error);
}
if (!error_energy)
return -1;
return signal_energy / error_energy;
}
int ff_dcaadpcm_subband_analysis(const DCAADPCMEncContext *s, const int32_t *in, int len, int *diff)
{
int pred_vq, i;
int32_t input_buffer[16 + DCA_ADPCM_COEFFS];
int32_t input_buffer2[16 + DCA_ADPCM_COEFFS];
int32_t max = 0;
int shift_bits;
uint64_t pg = 0;
for (i = 0; i < len + DCA_ADPCM_COEFFS; i++)
max |= FFABS(in[i]);
// normalize input to simplify apply_filter
shift_bits = av_log2(max) - 11;
for (i = 0; i < len + DCA_ADPCM_COEFFS; i++) {
input_buffer[i] = norm__(in[i], 7);
input_buffer2[i] = norm__(in[i], shift_bits);
}
pred_vq = find_best_filter(s, input_buffer2, len);
if (pred_vq < 0)
return -1;
pg = calc_prediction_gain(pred_vq, input_buffer, diff, len);
// Greater than 10db (10*log(10)) prediction gain to use ADPCM.
// TODO: Tune it.
if (pg < 10)
return -1;
for (i = 0; i < len; i++)
diff[i] <<= 7;
return pred_vq;
}
static void precalc(premultiplied_coeffs *data)
{
int i, j, k;
for (i = 0; i < DCA_ADPCM_VQCODEBOOK_SZ; i++) {
int id = 0;
int32_t t = 0;
for (j = 0; j < DCA_ADPCM_COEFFS; j++) {
for (k = j; k < DCA_ADPCM_COEFFS; k++) {
t = (int32_t)ff_dca_adpcm_vb[i][j] * (int32_t)ff_dca_adpcm_vb[i][k];
if (j != k)
t *= 2;
(*data)[id++] = t;
}
}
data++;
}
}
int ff_dcaadpcm_do_real(int pred_vq_index,
softfloat quant, int32_t scale_factor, int32_t step_size,
const int32_t *prev_hist, const int32_t *in, int32_t *next_hist, int32_t *out,
int len, int32_t peak)
{
int i;
int64_t delta;
int32_t dequant_delta;
int32_t work_bufer[16 + DCA_ADPCM_COEFFS];
memcpy(work_bufer, prev_hist, sizeof(int32_t) * DCA_ADPCM_COEFFS);
for (i = 0; i < len; i++) {
work_bufer[DCA_ADPCM_COEFFS + i] = ff_dcaadpcm_predict(pred_vq_index, &work_bufer[i]);
delta = (int64_t)in[i] - ((int64_t)work_bufer[DCA_ADPCM_COEFFS + i] << 7);
out[i] = quantize_value(av_clip64(delta, -peak, peak), quant);
ff_dca_core_dequantize(&dequant_delta, &out[i], step_size, scale_factor, 0, 1);
work_bufer[DCA_ADPCM_COEFFS+i] += dequant_delta;
}
memcpy(next_hist, &work_bufer[len], sizeof(int32_t) * DCA_ADPCM_COEFFS);
return 0;
}
av_cold int ff_dcaadpcm_init(DCAADPCMEncContext *s)
{
if (!s)
return -1;
s->private_data = av_malloc(sizeof(premultiplied_coeffs) * DCA_ADPCM_VQCODEBOOK_SZ);
if (!s->private_data)
return AVERROR(ENOMEM);
precalc(s->private_data);
return 0;
}
av_cold void ff_dcaadpcm_free(DCAADPCMEncContext *s)
{
if (!s)
return;
av_freep(&s->private_data);
}
|