elbg.c
Go to the documentation of this file.
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 
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/lfg.h"
31 #include "elbg.h"
32 #include "avcodec.h"
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 {
40  int index;
41  struct cell_s *next;
42 } cell;
43 
44 /**
45  * ELBG internal data
46  */
47 typedef struct{
48  int error;
49  int dim;
50  int numCB;
51  int *codebook;
53  int *utility;
55  int *nearest_cb;
56  int *points;
58  int *scratchbuf;
59 } elbg_data;
60 
61 static inline int distance_limited(int *a, int *b, int dim, int limit)
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 
73 static inline void vect_division(int *res, int *vect, int div, int dim)
74 {
75  int i;
76  if (div > 1)
77  for (i=0; i<dim; i++)
78  res[i] = ROUNDED_DIV(vect[i],div);
79  else if (res != vect)
80  memcpy(res, vect, dim*sizeof(int));
81 
82 }
83 
84 static int eval_error_cell(elbg_data *elbg, int *centroid, cell *cells)
85 {
86  int error=0;
87  for (; cells; cells=cells->next)
88  error += distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX);
89 
90  return error;
91 }
92 
93 static int get_closest_codebook(elbg_data *elbg, int index)
94 {
95  int i, pick=0, diff, diff_min = INT_MAX;
96  for (i=0; i<elbg->numCB; i++)
97  if (i != index) {
98  diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min);
99  if (diff < diff_min) {
100  pick = i;
101  diff_min = diff;
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 */
111  int r = av_lfg_get(elbg->rand_state)%elbg->utility_inc[elbg->numCB-1] + 1;
112  while (elbg->utility_inc[i] < r)
113  i++;
114 
115  av_assert2(elbg->cells[i]);
116 
117  return i;
118 }
119 
120 /**
121  * Implementation of the simple LBG algorithm for just two codebooks
122  */
123 static int simple_lbg(elbg_data *elbg,
124  int dim,
125  int *centroid[3],
126  int newutility[3],
127  int *points,
128  cell *cells)
129 {
130  int i, idx;
131  int numpoints[2] = {0,0};
132  int *newcentroid[2] = {
133  elbg->scratchbuf + 3*dim,
134  elbg->scratchbuf + 4*dim
135  };
136  cell *tempcell;
137 
138  memset(newcentroid[0], 0, 2 * dim * sizeof(*newcentroid[0]));
139 
140  newutility[0] =
141  newutility[1] = 0;
142 
143  for (tempcell = cells; tempcell; tempcell=tempcell->next) {
144  idx = distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX)>=
145  distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX);
146  numpoints[idx]++;
147  for (i=0; i<dim; i++)
148  newcentroid[idx][i] += points[tempcell->index*dim + i];
149  }
150 
151  vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
152  vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
153 
154  for (tempcell = cells; tempcell; tempcell=tempcell->next) {
155  int dist[2] = {distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX),
156  distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX)};
157  int idx = dist[0] > dist[1];
158  newutility[idx] += dist[idx];
159  }
160 
161  return newutility[0] + newutility[1];
162 }
163 
164 static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i,
165  int *newcentroid_p)
166 {
167  cell *tempcell;
168  int *min = newcentroid_i;
169  int *max = newcentroid_p;
170  int i;
171 
172  for (i=0; i< elbg->dim; i++) {
173  min[i]=INT_MAX;
174  max[i]=0;
175  }
176 
177  for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
178  for(i=0; i<elbg->dim; i++) {
179  min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
180  max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
181  }
182 
183  for (i=0; i<elbg->dim; i++) {
184  int ni = min[i] + (max[i] - min[i])/3;
185  int np = min[i] + (2*(max[i] - min[i]))/3;
186  newcentroid_i[i] = ni;
187  newcentroid_p[i] = np;
188  }
189 }
190 
191 /**
192  * Add the points in the low utility cell to its closest cell. Split the high
193  * utility cell, putting the separate points in the (now empty) low utility
194  * cell.
195  *
196  * @param elbg Internal elbg data
197  * @param indexes {luc, huc, cluc}
198  * @param newcentroid A vector with the position of the new centroids
199  */
200 static void shift_codebook(elbg_data *elbg, int *indexes,
201  int *newcentroid[3])
202 {
203  cell *tempdata;
204  cell **pp = &elbg->cells[indexes[2]];
205 
206  while(*pp)
207  pp= &(*pp)->next;
208 
209  *pp = elbg->cells[indexes[0]];
210 
211  elbg->cells[indexes[0]] = NULL;
212  tempdata = elbg->cells[indexes[1]];
213  elbg->cells[indexes[1]] = NULL;
214 
215  while(tempdata) {
216  cell *tempcell2 = tempdata->next;
217  int idx = distance_limited(elbg->points + tempdata->index*elbg->dim,
218  newcentroid[0], elbg->dim, INT_MAX) >
219  distance_limited(elbg->points + tempdata->index*elbg->dim,
220  newcentroid[1], elbg->dim, INT_MAX);
221 
222  tempdata->next = elbg->cells[indexes[idx]];
223  elbg->cells[indexes[idx]] = tempdata;
224  tempdata = tempcell2;
225  }
226 }
227 
228 static void evaluate_utility_inc(elbg_data *elbg)
229 {
230  int i, inc=0;
231 
232  for (i=0; i < elbg->numCB; i++) {
233  if (elbg->numCB*elbg->utility[i] > elbg->error)
234  inc += elbg->utility[i];
235  elbg->utility_inc[i] = inc;
236  }
237 }
238 
239 
240 static void update_utility_and_n_cb(elbg_data *elbg, int idx, int newutility)
241 {
242  cell *tempcell;
243 
244  elbg->utility[idx] = newutility;
245  for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next)
246  elbg->nearest_cb[tempcell->index] = idx;
247 }
248 
249 /**
250  * Evaluate if a shift lower the error. If it does, call shift_codebooks
251  * and update elbg->error, elbg->utility and elbg->nearest_cb.
252  *
253  * @param elbg Internal elbg data
254  * @param idx {luc (low utility cell, huc (high utility cell), cluc (closest cell to low utility cell)}
255  */
256 static void try_shift_candidate(elbg_data *elbg, int idx[3])
257 {
258  int j, k, olderror=0, newerror, cont=0;
259  int newutility[3];
260  int *newcentroid[3] = {
261  elbg->scratchbuf,
262  elbg->scratchbuf + elbg->dim,
263  elbg->scratchbuf + 2*elbg->dim
264  };
265  cell *tempcell;
266 
267  for (j=0; j<3; j++)
268  olderror += elbg->utility[idx[j]];
269 
270  memset(newcentroid[2], 0, elbg->dim*sizeof(int));
271 
272  for (k=0; k<2; k++)
273  for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
274  cont++;
275  for (j=0; j<elbg->dim; j++)
276  newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j];
277  }
278 
279  vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim);
280 
281  get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]);
282 
283  newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]);
284  newutility[2] += eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]);
285 
286  newerror = newutility[2];
287 
288  newerror += simple_lbg(elbg, elbg->dim, newcentroid, newutility, elbg->points,
289  elbg->cells[idx[1]]);
290 
291  if (olderror > newerror) {
292  shift_codebook(elbg, idx, newcentroid);
293 
294  elbg->error += newerror - olderror;
295 
296  for (j=0; j<3; j++)
297  update_utility_and_n_cb(elbg, idx[j], newutility[j]);
298 
299  evaluate_utility_inc(elbg);
300  }
301  }
302 
303 /**
304  * Implementation of the ELBG block
305  */
306 static void do_shiftings(elbg_data *elbg)
307 {
308  int idx[3];
309 
310  evaluate_utility_inc(elbg);
311 
312  for (idx[0]=0; idx[0] < elbg->numCB; idx[0]++)
313  if (elbg->numCB*elbg->utility[idx[0]] < elbg->error) {
314  if (elbg->utility_inc[elbg->numCB-1] == 0)
315  return;
316 
317  idx[1] = get_high_utility_cell(elbg);
318  idx[2] = get_closest_codebook(elbg, idx[0]);
319 
320  if (idx[1] != idx[0] && idx[1] != idx[2])
321  try_shift_candidate(elbg, idx);
322  }
323 }
324 
325 #define BIG_PRIME 433494437LL
326 
327 void ff_init_elbg(int *points, int dim, int numpoints, int *codebook,
328  int numCB, int max_steps, int *closest_cb,
329  AVLFG *rand_state)
330 {
331  int i, k;
332 
333  if (numpoints > 24*numCB) {
334  /* ELBG is very costly for a big number of points. So if we have a lot
335  of them, get a good initial codebook to save on iterations */
336  int *temp_points = av_malloc(dim*(numpoints/8)*sizeof(int));
337  for (i=0; i<numpoints/8; i++) {
338  k = (i*BIG_PRIME) % numpoints;
339  memcpy(temp_points + i*dim, points + k*dim, dim*sizeof(int));
340  }
341 
342  ff_init_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state);
343  ff_do_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state);
344 
345  av_free(temp_points);
346 
347  } else // If not, initialize the codebook with random positions
348  for (i=0; i < numCB; i++)
349  memcpy(codebook + i*dim, points + ((i*BIG_PRIME)%numpoints)*dim,
350  dim*sizeof(int));
351 
352 }
353 
354 void ff_do_elbg(int *points, int dim, int numpoints, int *codebook,
355  int numCB, int max_steps, int *closest_cb,
356  AVLFG *rand_state)
357 {
358  int dist;
359  elbg_data elbg_d;
360  elbg_data *elbg = &elbg_d;
361  int i, j, k, last_error, steps=0;
362  int *dist_cb = av_malloc(numpoints*sizeof(int));
363  int *size_part = av_malloc(numCB*sizeof(int));
364  cell *list_buffer = av_malloc(numpoints*sizeof(cell));
365  cell *free_cells;
366  int best_dist, best_idx = 0;
367 
368  elbg->error = INT_MAX;
369  elbg->dim = dim;
370  elbg->numCB = numCB;
371  elbg->codebook = codebook;
372  elbg->cells = av_malloc(numCB*sizeof(cell *));
373  elbg->utility = av_malloc(numCB*sizeof(int));
374  elbg->nearest_cb = closest_cb;
375  elbg->points = points;
376  elbg->utility_inc = av_malloc(numCB*sizeof(int));
377  elbg->scratchbuf = av_malloc(5*dim*sizeof(int));
378 
379  elbg->rand_state = rand_state;
380 
381  do {
382  free_cells = list_buffer;
383  last_error = elbg->error;
384  steps++;
385  memset(elbg->utility, 0, numCB*sizeof(int));
386  memset(elbg->cells, 0, numCB*sizeof(cell *));
387 
388  elbg->error = 0;
389 
390  /* This loop evaluate the actual Voronoi partition. It is the most
391  costly part of the algorithm. */
392  for (i=0; i < numpoints; i++) {
393  best_dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + best_idx*elbg->dim, dim, INT_MAX);
394  for (k=0; k < elbg->numCB; k++) {
395  dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + k*elbg->dim, dim, best_dist);
396  if (dist < best_dist) {
397  best_dist = dist;
398  best_idx = k;
399  }
400  }
401  elbg->nearest_cb[i] = best_idx;
402  dist_cb[i] = best_dist;
403  elbg->error += dist_cb[i];
404  elbg->utility[elbg->nearest_cb[i]] += dist_cb[i];
405  free_cells->index = i;
406  free_cells->next = elbg->cells[elbg->nearest_cb[i]];
407  elbg->cells[elbg->nearest_cb[i]] = free_cells;
408  free_cells++;
409  }
410 
411  do_shiftings(elbg);
412 
413  memset(size_part, 0, numCB*sizeof(int));
414 
415  memset(elbg->codebook, 0, elbg->numCB*dim*sizeof(int));
416 
417  for (i=0; i < numpoints; i++) {
418  size_part[elbg->nearest_cb[i]]++;
419  for (j=0; j < elbg->dim; j++)
420  elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] +=
421  elbg->points[i*elbg->dim + j];
422  }
423 
424  for (i=0; i < elbg->numCB; i++)
425  vect_division(elbg->codebook + i*elbg->dim,
426  elbg->codebook + i*elbg->dim, size_part[i], elbg->dim);
427 
428  } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) &&
429  (steps < max_steps));
430 
431  av_free(dist_cb);
432  av_free(size_part);
433  av_free(elbg->utility);
434  av_free(list_buffer);
435  av_free(elbg->cells);
436  av_free(elbg->utility_inc);
437  av_free(elbg->scratchbuf);
438 }
Definition: lfg.h:25
struct cell_s * next
Definition: elbg.c:41
static void do_shiftings(elbg_data *elbg)
Implementation of the ELBG block.
Definition: elbg.c:306
if max(w)>1 w=0.9 *w/max(w)
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.
Definition: elbg.c:123
cell ** cells
Definition: elbg.c:52
static int distance_limited(int *a, int *b, int dim, int limit)
Definition: elbg.c:61
static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i, int *newcentroid_p)
Definition: elbg.c:164
struct cell_s cell
In the ELBG jargon, a cell is the set of points that are closest to a codebook entry.
ELBG internal data.
Definition: elbg.c:47
static void evaluate_utility_inc(elbg_data *elbg)
Definition: elbg.c:228
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
AVLFG * rand_state
Definition: elbg.c:57
#define b
Definition: input.c:42
#define ROUNDED_DIV(a, b)
Definition: common.h:50
int * codebook
Definition: elbg.c:51
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
int * utility_inc
Definition: elbg.c:54
static void vect_division(int *res, int *vect, int div, int dim)
Definition: elbg.c:73
const char * r
Definition: vf_curves.c:94
int * nearest_cb
Definition: elbg.c:55
simple assert() macros that are a bit more flexible than ISO C assert().
#define FFMAX(a, b)
Definition: common.h:56
external API header
void ff_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...
Definition: elbg.c:354
int dim
Definition: elbg.c:49
#define FFMIN(a, b)
Definition: common.h:58
static int get_high_utility_cell(elbg_data *elbg)
Definition: elbg.c:107
#define BIG_PRIME
Definition: elbg.c:325
static int get_closest_codebook(elbg_data *elbg, int index)
Definition: elbg.c:93
#define diff(a, as, b, bs)
Definition: vf_phase.c:80
int * scratchbuf
Definition: elbg.c:58
static void update_utility_and_n_cb(elbg_data *elbg, int idx, int newutility)
Definition: elbg.c:240
int * points
Definition: elbg.c:56
for k
NULL
Definition: eval.c:55
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition: lfg.h:38
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
int dim
void ff_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.
Definition: elbg.c:327
static void shift_codebook(elbg_data *elbg, int *indexes, int *newcentroid[3])
Add the points in the low utility cell to its closest cell.
Definition: elbg.c:200
static int eval_error_cell(elbg_data *elbg, int *centroid, cell *cells)
Definition: elbg.c:84
common internal and external API header
int error
Definition: elbg.c:48
int index
Definition: elbg.c:40
#define DELTA_ERR_MAX
Precision of the ELBG algorithm (as percentual error)
Definition: elbg.c:34
int numCB
Definition: elbg.c:50
In the ELBG jargon, a cell is the set of points that are closest to a codebook entry.
Definition: elbg.c:39
static void try_shift_candidate(elbg_data *elbg, int idx[3])
Evaluate if a shift lower the error.
Definition: elbg.c:256
int * utility
Definition: elbg.c:53
float min