1 /*
2 * Copyright (C) 2007 Vitor Sessak <vitor1001@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Codebook Generator using the ELBG algorithm
24 */
25
26 #include <string.h>
27
33
34 #define DELTA_ERR_MAX 0.1 ///< Precision of the ELBG algorithm (as percentual error)
35
36 /**
37 * In the ELBG jargon, a cell is the set of points that are closest to a
38 * codebook entry. Not to be confused with a RoQ Video cell. */
39 typedef struct cell_s {
43
44 /**
45 * ELBG internal data
46 */
60
62 {
63 int i, dist=0;
64 for (i=0; i<
dim; i++) {
65 dist += (a[i] - b[i])*(a[i] - b[i]);
66 if (dist > limit)
67 return INT_MAX;
68 }
69
70 return dist;
71 }
72
74 {
75 int i;
76 if (div > 1)
79 else if (res != vect)
80 memcpy(res, vect, dim*sizeof(int));
81
82 }
83
85 {
86 int error=0;
87 for (; cells; cells=cells->
next)
89
90 return error;
91 }
92
94 {
95 int i, pick=0,
diff, diff_min = INT_MAX;
96 for (i=0; i<elbg->
numCB; i++)
97 if (i != index) {
99 if (
diff < diff_min) {
100 pick = i;
102 }
103 }
104 return pick;
105 }
106
108 {
109 int i=0;
110 /* Using linear search, do binary if it ever turns to be speed critical */
112
115 } else {
118 }
119
121 i++;
122 }
123
125
126 return i;
127 }
128
129 /**
130 * Implementation of the simple LBG algorithm for just two codebooks
131 */
134 int *centroid[3],
135 int newutility[3],
136 int *points,
138 {
139 int i, idx;
140 int numpoints[2] = {0,0};
141 int *newcentroid[2] = {
144 };
146
147 memset(newcentroid[0], 0, 2 * dim * sizeof(*newcentroid[0]));
148
149 newutility[0] =
150 newutility[1] = 0;
151
152 for (tempcell = cells; tempcell; tempcell=tempcell->
next) {
155 numpoints[idx]++;
156 for (i=0; i<
dim; i++)
157 newcentroid[idx][i] += points[tempcell->
index*dim + i];
158 }
159
160 vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
161 vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
162
163 for (tempcell = cells; tempcell; tempcell=tempcell->
next) {
166 int idx = dist[0] > dist[1];
167 newutility[idx] += dist[idx];
168 }
169
170 return newutility[0] + newutility[1];
171 }
172
174 int *newcentroid_p)
175 {
177 int *
min = newcentroid_i;
178 int *max = newcentroid_p;
179 int i;
180
181 for (i=0; i< elbg->
dim; i++) {
182 min[i]=INT_MAX;
183 max[i]=0;
184 }
185
186 for (tempcell = elbg->
cells[huc]; tempcell; tempcell = tempcell->
next)
187 for(i=0; i<elbg->
dim; i++) {
190 }
191
192 for (i=0; i<elbg->
dim; i++) {
193 int ni = min[i] + (max[i] - min[i])/3;
194 int np = min[i] + (2*(max[i] - min[i]))/3;
195 newcentroid_i[i] = ni;
196 newcentroid_p[i] = np;
197 }
198 }
199
200 /**
201 * Add the points in the low utility cell to its closest cell. Split the high
202 * utility cell, putting the separate points in the (now empty) low utility
203 * cell.
204 *
205 * @param elbg Internal elbg data
206 * @param indexes {luc, huc, cluc}
207 * @param newcentroid A vector with the position of the new centroids
208 */
210 int *newcentroid[3])
211 {
214
215 while(*pp)
217
218 *pp = elbg->
cells[indexes[0]];
219
221 tempdata = elbg->
cells[indexes[1]];
223
224 while(tempdata) {
227 newcentroid[0], elbg->
dim, INT_MAX) >
229 newcentroid[1], elbg->
dim, INT_MAX);
230
231 tempdata->
next = elbg->
cells[indexes[idx]];
232 elbg->
cells[indexes[idx]] = tempdata;
233 tempdata = tempcell2;
234 }
235 }
236
238 {
239 int i;
240 int64_t inc=0;
241
242 for (i=0; i < elbg->
numCB; i++) {
246 }
247 }
248
249
251 {
253
254 elbg->
utility[idx] = newutility;
255 for (tempcell=elbg->
cells[idx]; tempcell; tempcell=tempcell->
next)
257 }
258
259 /**
260 * Evaluate if a shift lower the error. If it does, call shift_codebooks
261 * and update elbg->error, elbg->utility and elbg->nearest_cb.
262 *
263 * @param elbg Internal elbg data
264 * @param idx {luc (low utility cell, huc (high utility cell), cluc (closest cell to low utility cell)}
265 */
267 {
268 int j, k, olderror=0, newerror, cont=0;
269 int newutility[3];
270 int *newcentroid[3] = {
274 };
276
277 for (j=0; j<3; j++)
278 olderror += elbg->
utility[idx[j]];
279
280 memset(newcentroid[2], 0, elbg->
dim*
sizeof(
int));
281
282 for (k=0; k<2; k++)
283 for (tempcell=elbg->
cells[idx[2*k]]; tempcell; tempcell=tempcell->
next) {
284 cont++;
285 for (j=0; j<elbg->
dim; j++)
287 }
288
290
292
295
296 newerror = newutility[2];
297
299 elbg->
cells[idx[1]]);
300
301 if (olderror > newerror) {
303
304 elbg->
error += newerror - olderror;
305
306 for (j=0; j<3; j++)
308
310 }
311 }
312
313 /**
314 * Implementation of the ELBG block
315 */
317 {
318 int idx[3];
319
321
322 for (idx[0]=0; idx[0] < elbg->
numCB; idx[0]++)
325 return;
326
329
330 if (idx[1] != idx[0] && idx[1] != idx[2])
332 }
333 }
334
335 #define BIG_PRIME 433494437LL
336
338 int numCB, int max_steps, int *closest_cb,
340 {
341 int i, k, ret = 0;
342
343 if (numpoints > 24*numCB) {
344 /* ELBG is very costly for a big number of points. So if we have a lot
345 of them, get a good initial codebook to save on iterations */
347 if (!temp_points)
349 for (i=0; i<numpoints/8; i++) {
351 memcpy(temp_points + i*dim, points + k*dim, dim*sizeof(int));
352 }
353
355 numCB, 2 * max_steps, closest_cb, rand_state);
356 if (ret < 0) {
358 return ret;
359 }
361 numCB, 2 * max_steps, closest_cb, rand_state);
363
364 } else // If not, initialize the codebook with random positions
365 for (i=0; i < numCB; i++)
366 memcpy(codebook + i*dim, points + ((i*
BIG_PRIME)%numpoints)*dim,
367 dim*sizeof(int));
368 return ret;
369 }
370
372 int numCB, int max_steps, int *closest_cb,
374 {
375 int dist;
378 int i, j, k, last_error, steps = 0, ret = 0;
383 int best_dist, best_idx = 0;
384
385 elbg->
error = INT_MAX;
395
396 if (!dist_cb || !size_part || !list_buffer || !elbg->
cells ||
400 }
401
403
404 do {
405 free_cells = list_buffer;
406 last_error = elbg->
error;
407 steps++;
408 memset(elbg->
utility, 0, numCB*
sizeof(
int));
409 memset(elbg->
cells, 0, numCB*
sizeof(
cell *));
410
412
413 /* This loop evaluate the actual Voronoi partition. It is the most
414 costly part of the algorithm. */
415 for (i=0; i < numpoints; i++) {
417 for (k=0; k < elbg->
numCB; k++) {
419 if (dist < best_dist) {
420 best_dist = dist;
421 best_idx = k;
422 }
423 }
425 dist_cb[i] = best_dist;
426 elbg->
error += dist_cb[i];
428 free_cells->
index = i;
431 free_cells++;
432 }
433
435
436 memset(size_part, 0, numCB*sizeof(int));
437
439
440 for (i=0; i < numpoints; i++) {
442 for (j=0; j < elbg->
dim; j++)
445 }
446
447 for (i=0; i < elbg->
numCB; i++)
450
452 (steps < max_steps));
453
462 return ret;
463 }
static void do_shiftings(elbg_data *elbg)
Implementation of the ELBG block.
static int simple_lbg(elbg_data *elbg, int dim, int *centroid[3], int newutility[3], int *points, cell *cells)
Implementation of the simple LBG algorithm for just two codebooks.
static int distance_limited(int *a, int *b, int dim, int limit)
static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i, int *newcentroid_p)
static void evaluate_utility_inc(elbg_data *elbg)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
int avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Implementation of the Enhanced LBG Algorithm Based on the paper "Neural Networks 14:1219-1237" that c...
#define ROUNDED_DIV(a, b)
static void vect_division(int *res, int *vect, int div, int dim)
simple assert() macros that are a bit more flexible than ISO C assert().
In the ELBG jargon, a cell is the set of points that are closest to a codebook entry.
static int get_high_utility_cell(elbg_data *elbg)
static int get_closest_codebook(elbg_data *elbg, int index)
static void update_utility_and_n_cb(elbg_data *elbg, int idx, int newutility)
int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Initialize the **codebook vector for the elbg algorithm.
Libavcodec external API header.
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
static void shift_codebook(elbg_data *elbg, int *indexes, int *newcentroid[3])
Add the points in the low utility cell to its closest cell.
static int eval_error_cell(elbg_data *elbg, int *centroid, cell *cells)
common internal and external API header
static av_always_inline int diff(const uint32_t a, const uint32_t b)
#define DELTA_ERR_MAX
Precision of the ELBG algorithm (as percentual error)
#define av_malloc_array(a, b)
static void try_shift_candidate(elbg_data *elbg, int idx[3])
Evaluate if a shift lower the error.