Lines Matching defs:elbg
31 #include "elbg.h"
84 static int eval_error_cell(elbg_data *elbg, int *centroid, cell *cells)
88 error += distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX);
93 static int get_closest_codebook(elbg_data *elbg, int index)
96 for (i=0; i<elbg->numCB; i++)
98 diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min);
107 static int get_high_utility_cell(elbg_data *elbg)
113 if (elbg->utility_inc[elbg->numCB-1] < INT_MAX) {
114 r = av_lfg_get(elbg->rand_state) % (unsigned int)elbg->utility_inc[elbg->numCB-1] + 1;
116 r = av_lfg_get(elbg->rand_state);
117 r = (av_lfg_get(elbg->rand_state) + (r<<32)) % elbg->utility_inc[elbg->numCB-1] + 1;
120 while (elbg->utility_inc[i] < r) {
124 av_assert2(elbg->cells[i]);
132 static int simple_lbg(elbg_data *elbg,
142 elbg->scratchbuf + 3*dim,
143 elbg->scratchbuf + 4*dim
173 static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i,
181 for (i=0; i< elbg->dim; i++) {
186 for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
187 for(i=0; i<elbg->dim; i++) {
188 min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
189 max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
192 for (i=0; i<elbg->dim; i++) {
205 * @param elbg Internal elbg data
209 static void shift_codebook(elbg_data *elbg, int *indexes,
213 cell **pp = &elbg->cells[indexes[2]];
218 *pp = elbg->cells[indexes[0]];
220 elbg->cells[indexes[0]] = NULL;
221 tempdata = elbg->cells[indexes[1]];
222 elbg->cells[indexes[1]] = NULL;
226 int idx = distance_limited(elbg->points + tempdata->index*elbg->dim,
227 newcentroid[0], elbg->dim, INT_MAX) >
228 distance_limited(elbg->points + tempdata->index*elbg->dim,
229 newcentroid[1], elbg->dim, INT_MAX);
231 tempdata->next = elbg->cells[indexes[idx]];
232 elbg->cells[indexes[idx]] = tempdata;
237 static void evaluate_utility_inc(elbg_data *elbg)
242 for (i=0; i < elbg->numCB; i++) {
243 if (elbg->numCB*elbg->utility[i] > elbg->error)
244 inc += elbg->utility[i];
245 elbg->utility_inc[i] = inc;
250 static void update_utility_and_n_cb(elbg_data *elbg, int idx, int newutility)
254 elbg->utility[idx] = newutility;
255 for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next)
256 elbg->nearest_cb[tempcell->index] = idx;
261 * and update elbg->error, elbg->utility and elbg->nearest_cb.
263 * @param elbg Internal elbg data
266 static void try_shift_candidate(elbg_data *elbg, int idx[3])
271 elbg->scratchbuf,
272 elbg->scratchbuf + elbg->dim,
273 elbg->scratchbuf + 2*elbg->dim
278 olderror += elbg->utility[idx[j]];
280 memset(newcentroid[2], 0, elbg->dim*sizeof(int));
283 for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
285 for (j=0; j<elbg->dim; j++)
286 newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j];
289 vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim);
291 get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]);
293 newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]);
294 newutility[2] += eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]);
298 newerror += simple_lbg(elbg, elbg->dim, newcentroid, newutility, elbg->points,
299 elbg->cells[idx[1]]);
302 shift_codebook(elbg, idx, newcentroid);
304 elbg->error += newerror - olderror;
307 update_utility_and_n_cb(elbg, idx[j], newutility[j]);
309 evaluate_utility_inc(elbg);
316 static void do_shiftings(elbg_data *elbg)
320 evaluate_utility_inc(elbg);
322 for (idx[0]=0; idx[0] < elbg->numCB; idx[0]++)
323 if (elbg->numCB*elbg->utility[idx[0]] < elbg->error) {
324 if (elbg->utility_inc[elbg->numCB-1] == 0)
327 idx[1] = get_high_utility_cell(elbg);
328 idx[2] = get_closest_codebook(elbg, idx[0]);
331 try_shift_candidate(elbg, idx);
370 elbg_data *elbg = &elbg_d;
378 elbg->error = INT_MAX;
379 elbg->dim = dim;
380 elbg->numCB = numCB;
381 elbg->codebook = codebook;
382 elbg->cells = av_malloc_array(numCB, sizeof(cell *));
383 elbg->utility = av_malloc_array(numCB, sizeof(int));
384 elbg->nearest_cb = closest_cb;
385 elbg->points = points;
386 elbg->utility_inc = av_malloc_array(numCB, sizeof(*elbg->utility_inc));
387 elbg->scratchbuf = av_malloc_array(5*dim, sizeof(int));
389 elbg->rand_state = rand_state;
393 last_error = elbg->error;
395 memset(elbg->utility, 0, numCB*sizeof(int));
396 memset(elbg->cells, 0, numCB*sizeof(cell *));
398 elbg->error = 0;
403 best_dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + best_idx*elbg->dim, dim, INT_MAX);
404 for (k=0; k < elbg->numCB; k++) {
405 dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + k*elbg->dim, dim, best_dist);
411 elbg->nearest_cb[i] = best_idx;
413 elbg->error += dist_cb[i];
414 elbg->utility[elbg->nearest_cb[i]] += dist_cb[i];
416 free_cells->next = elbg->cells[elbg->nearest_cb[i]];
417 elbg->cells[elbg->nearest_cb[i]] = free_cells;
421 do_shiftings(elbg);
425 memset(elbg->codebook, 0, elbg->numCB*dim*sizeof(int));
428 size_part[elbg->nearest_cb[i]]++;
429 for (j=0; j < elbg->dim; j++)
430 elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] +=
431 elbg->points[i*elbg->dim + j];
434 for (i=0; i < elbg->numCB; i++)
435 vect_division(elbg->codebook + i*elbg->dim,
436 elbg->codebook + i*elbg->dim, size_part[i], elbg->dim);
438 } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) &&
443 av_free(elbg->utility);
445 av_free(elbg->cells);
446 av_free(elbg->utility_inc);
447 av_free(elbg->scratchbuf);