Lines Matching defs:elbg
29 #include "elbg.h"
82 static int eval_error_cell(elbg_data *elbg, int *centroid, cell *cells)
86 error += distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX);
91 static int get_closest_codebook(elbg_data *elbg, int index)
94 for (i=0; i<elbg->numCB; i++)
96 diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min);
105 static int get_high_utility_cell(elbg_data *elbg)
109 int r = av_lfg_get(elbg->rand_state)%elbg->utility_inc[elbg->numCB-1] + 1;
110 while (elbg->utility_inc[i] < r)
113 assert(!elbg->cells[i]);
121 static int simple_lbg(elbg_data *elbg,
131 elbg->scratchbuf + 3*dim,
132 elbg->scratchbuf + 4*dim
162 static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i,
170 for (i=0; i< elbg->dim; i++) {
175 for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
176 for(i=0; i<elbg->dim; i++) {
177 min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
178 max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
181 for (i=0; i<elbg->dim; i++) {
194 * @param elbg Internal elbg data
198 static void shift_codebook(elbg_data *elbg, int *indexes,
202 cell **pp = &elbg->cells[indexes[2]];
207 *pp = elbg->cells[indexes[0]];
209 elbg->cells[indexes[0]] = NULL;
210 tempdata = elbg->cells[indexes[1]];
211 elbg->cells[indexes[1]] = NULL;
215 int idx = distance_limited(elbg->points + tempdata->index*elbg->dim,
216 newcentroid[0], elbg->dim, INT_MAX) >
217 distance_limited(elbg->points + tempdata->index*elbg->dim,
218 newcentroid[1], elbg->dim, INT_MAX);
220 tempdata->next = elbg->cells[indexes[idx]];
221 elbg->cells[indexes[idx]] = tempdata;
226 static void evaluate_utility_inc(elbg_data *elbg)
230 for (i=0; i < elbg->numCB; i++) {
231 if (elbg->numCB*elbg->utility[i] > elbg->error)
232 inc += elbg->utility[i];
233 elbg->utility_inc[i] = inc;
238 static void update_utility_and_n_cb(elbg_data *elbg, int idx, int newutility)
242 elbg->utility[idx] = newutility;
243 for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next)
244 elbg->nearest_cb[tempcell->index] = idx;
249 * and update elbg->error, elbg->utility and elbg->nearest_cb.
251 * @param elbg Internal elbg data
254 static void try_shift_candidate(elbg_data *elbg, int idx[3])
259 elbg->scratchbuf,
260 elbg->scratchbuf + elbg->dim,
261 elbg->scratchbuf + 2*elbg->dim
266 olderror += elbg->utility[idx[j]];
268 memset(newcentroid[2], 0, elbg->dim*sizeof(int));
271 for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
273 for (j=0; j<elbg->dim; j++)
274 newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j];
277 vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim);
279 get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]);
281 newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]);
282 newutility[2] += eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]);
286 newerror += simple_lbg(elbg, elbg->dim, newcentroid, newutility, elbg->points,
287 elbg->cells[idx[1]]);
290 shift_codebook(elbg, idx, newcentroid);
292 elbg->error += newerror - olderror;
295 update_utility_and_n_cb(elbg, idx[j], newutility[j]);
297 evaluate_utility_inc(elbg);
304 static void do_shiftings(elbg_data *elbg)
308 evaluate_utility_inc(elbg);
310 for (idx[0]=0; idx[0] < elbg->numCB; idx[0]++)
311 if (elbg->numCB*elbg->utility[idx[0]] < elbg->error) {
312 if (elbg->utility_inc[elbg->numCB-1] == 0)
315 idx[1] = get_high_utility_cell(elbg);
316 idx[2] = get_closest_codebook(elbg, idx[0]);
319 try_shift_candidate(elbg, idx);
358 elbg_data *elbg = &elbg_d;
366 elbg->error = INT_MAX;
367 elbg->dim = dim;
368 elbg->numCB = numCB;
369 elbg->codebook = codebook;
370 elbg->cells = av_malloc(numCB*sizeof(cell *));
371 elbg->utility = av_malloc(numCB*sizeof(int));
372 elbg->nearest_cb = closest_cb;
373 elbg->points = points;
374 elbg->utility_inc = av_malloc(numCB*sizeof(int));
375 elbg->scratchbuf = av_malloc(5*dim*sizeof(int));
377 elbg->rand_state = rand_state;
381 last_error = elbg->error;
383 memset(elbg->utility, 0, numCB*sizeof(int));
384 memset(elbg->cells, 0, numCB*sizeof(cell *));
386 elbg->error = 0;
391 best_dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + best_idx*elbg->dim, dim, INT_MAX);
392 for (k=0; k < elbg->numCB; k++) {
393 dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + k*elbg->dim, dim, best_dist);
399 elbg->nearest_cb[i] = best_idx;
401 elbg->error += dist_cb[i];
402 elbg->utility[elbg->nearest_cb[i]] += dist_cb[i];
404 free_cells->next = elbg->cells[elbg->nearest_cb[i]];
405 elbg->cells[elbg->nearest_cb[i]] = free_cells;
409 do_shiftings(elbg);
413 memset(elbg->codebook, 0, elbg->numCB*dim*sizeof(int));
416 size_part[elbg->nearest_cb[i]]++;
417 for (j=0; j < elbg->dim; j++)
418 elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] +=
419 elbg->points[i*elbg->dim + j];
422 for (i=0; i < elbg->numCB; i++)
423 vect_division(elbg->codebook + i*elbg->dim,
424 elbg->codebook + i*elbg->dim, size_part[i], elbg->dim);
426 } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) &&
431 av_free(elbg->utility);
433 av_free(elbg->cells);
434 av_free(elbg->utility_inc);
435 av_free(elbg->scratchbuf);