33 #define DELTA_ERR_MAX 0.1
93 memcpy(res, vect,
dim*
sizeof(
int));
100 for (; cells; cells=cells->next)
109 for (
int i = 0, diff_min = INT_MAX;
i < elbg->
num_cb;
i++)
113 if (
diff < diff_min) {
154 int numpoints[2] = {0,0};
155 int *newcentroid[2] = {
161 memset(newcentroid[0], 0, 2 *
dim *
sizeof(*newcentroid[0]));
166 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
171 newcentroid[idx][
i] += points[tempcell->index*
dim +
i];
177 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
180 int idx = dist[0] > dist[1];
181 newutility[idx] += dist[idx];
184 return newutility[0] + newutility[1];
191 int *
min = newcentroid_i;
192 int *
max = newcentroid_p;
195 for (
i=0;
i< elbg->
dim;
i++) {
200 for (tempcell = elbg->
cells[huc]; tempcell; tempcell = tempcell->next)
201 for(
i=0;
i<elbg->
dim;
i++) {
206 for (
i=0;
i<elbg->
dim;
i++) {
209 newcentroid_i[
i] = ni;
210 newcentroid_p[
i] = np;
227 cell **pp = &elbg->
cells[indexes[2]];
232 *pp = elbg->
cells[indexes[0]];
235 tempdata = elbg->
cells[indexes[1]];
239 cell *tempcell2 = tempdata->next;
241 newcentroid[0], elbg->
dim, INT_MAX) >
243 newcentroid[1], elbg->
dim, INT_MAX);
245 tempdata->next = elbg->
cells[indexes[idx]];
246 elbg->
cells[indexes[idx]] = tempdata;
247 tempdata = tempcell2;
267 elbg->
utility[idx] = newutility;
268 for (tempcell=elbg->
cells[idx]; tempcell; tempcell=tempcell->next)
282 int64_t olderror=0, newerror;
284 int *newcentroid[3] = {
292 olderror += elbg->
utility[idx[j]];
294 memset(newcentroid[2], 0, elbg->
dim*
sizeof(
int));
297 for (tempcell=elbg->
cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
299 for (j=0; j<elbg->
dim; j++)
300 newcentroid[2][j] += elbg->
points[tempcell->index*elbg->
dim + j];
310 newerror = newutility[2];
313 elbg->
cells[idx[1]]);
315 if (olderror > newerror) {
318 elbg->
error += newerror - olderror;
336 for (idx[0]=0; idx[0] < elbg->
num_cb; idx[0]++)
344 if (idx[1] != idx[0] && idx[1] != idx[2])
352 int *
const size_part = elbg->size_part;
357 elbg->error = INT64_MAX;
358 elbg->points = points;
361 cell *free_cells = elbg->cell_buffer;
362 last_error = elbg->error;
364 memset(elbg->utility, 0, elbg->num_cb *
sizeof(*elbg->utility));
365 memset(elbg->cells, 0, elbg->num_cb *
sizeof(*elbg->cells));
371 for (
i=0;
i < numpoints;
i++) {
373 elbg->codebook + best_idx * elbg->dim,
375 for (
int k = 0; k < elbg->num_cb; k++) {
377 elbg->codebook + k * elbg->dim,
378 elbg->dim, best_dist);
379 if (dist < best_dist) {
384 elbg->nearest_cb[
i] = best_idx;
385 elbg->error += best_dist;
386 elbg->utility[elbg->nearest_cb[
i]] += best_dist;
387 free_cells->index =
i;
388 free_cells->next = elbg->cells[elbg->nearest_cb[
i]];
389 elbg->cells[elbg->nearest_cb[
i]] = free_cells;
395 memset(size_part, 0, elbg->num_cb *
sizeof(*size_part));
397 memset(elbg->codebook, 0, elbg->num_cb * elbg->dim *
sizeof(*elbg->codebook));
399 for (
i=0;
i < numpoints;
i++) {
400 size_part[elbg->nearest_cb[
i]]++;
401 for (j=0; j < elbg->dim; j++)
402 elbg->codebook[elbg->nearest_cb[
i]*elbg->dim + j] +=
403 elbg->points[
i*elbg->dim + j];
406 for (
int i = 0;
i < elbg->num_cb;
i++)
408 elbg->codebook +
i*elbg->dim, size_part[
i], elbg->dim);
410 }
while(((last_error - elbg->error) >
DELTA_ERR_MAX*elbg->error) &&
411 (
steps < max_steps));
414 #define BIG_PRIME 433494437LL
423 int numpoints,
int max_steps)
427 if (numpoints > 24LL * elbg->num_cb) {
430 for (
int i = 0;
i < numpoints / 8;
i++) {
432 memcpy(temp_points +
i*
dim, points + k*
dim,
dim *
sizeof(*temp_points));
437 init_elbg(elbg, temp_points, temp_points + numpoints / 8 *
dim,
438 numpoints / 8, 2 * max_steps);
439 do_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps);
441 for (
int i = 0;
i < elbg->num_cb;
i++)
443 dim *
sizeof(*elbg->codebook));
447 int *
codebook,
int num_cb,
int max_steps,
448 int *closest_cb,
AVLFG *rand_state, uintptr_t
flags)
457 elbg->rand_state = rand_state;
459 elbg->num_cb = num_cb;
462 #define ALLOCATE_IF_NECESSARY(field, new_elements, multiplicator) \
463 if (elbg->field ## _allocated < new_elements) { \
464 av_freep(&elbg->field); \
465 elbg->field = av_malloc_array(new_elements, \
466 multiplicator * sizeof(*elbg->field)); \
467 if (!elbg->field) { \
468 elbg->field ## _allocated = 0; \
469 return AVERROR(ENOMEM); \
471 elbg->field ## _allocated = new_elements; \
483 if (numpoints > 24LL * elbg->num_cb) {
488 uint64_t prod =
dim * (uint64_t)(numpoints / 7
U);
494 init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps);
495 do_elbg (elbg, points, numpoints, max_steps);