34 #define DELTA_ERR_MAX 0.1
97 memcpy(res, vect,
dim*
sizeof(
int));
104 for (; cells; cells=cells->next) {
117 for (
int i = 0, diff_min = INT_MAX;
i < elbg->
num_cb;
i++)
121 if (
diff < diff_min) {
162 int numpoints[2] = {0,0};
163 int *newcentroid[2] = {
169 memset(newcentroid[0], 0, 2 *
dim *
sizeof(*newcentroid[0]));
174 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
179 newcentroid[idx][
i] += points[tempcell->index*
dim +
i];
185 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
188 int idx = dist[0] > dist[1];
189 if (newutility[idx] >= INT_MAX - dist[idx])
190 newutility[idx] = INT_MAX;
192 newutility[idx] += dist[idx];
195 return (newutility[0] >= INT_MAX - newutility[1]) ? INT_MAX : newutility[0] + newutility[1];
202 int *
min = newcentroid_i;
203 int *
max = newcentroid_p;
206 for (
i=0;
i< elbg->
dim;
i++) {
211 for (tempcell = elbg->
cells[huc]; tempcell; tempcell = tempcell->next)
212 for(
i=0;
i<elbg->
dim;
i++) {
217 for (
i=0;
i<elbg->
dim;
i++) {
220 newcentroid_i[
i] = ni;
221 newcentroid_p[
i] = np;
238 cell **pp = &elbg->
cells[indexes[2]];
243 *pp = elbg->
cells[indexes[0]];
246 tempdata = elbg->
cells[indexes[1]];
250 cell *tempcell2 = tempdata->next;
252 newcentroid[0], elbg->
dim, INT_MAX) >
254 newcentroid[1], elbg->
dim, INT_MAX);
256 tempdata->next = elbg->
cells[indexes[idx]];
257 elbg->
cells[indexes[idx]] = tempdata;
258 tempdata = tempcell2;
278 elbg->
utility[idx] = newutility;
279 for (tempcell=elbg->
cells[idx]; tempcell; tempcell=tempcell->next)
292 int j, k, cont=0,
tmp;
295 int *newcentroid[3] = {
303 olderror += elbg->
utility[idx[j]];
305 memset(newcentroid[2], 0, elbg->
dim*
sizeof(
int));
308 for (tempcell=elbg->
cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
310 for (j=0; j<elbg->
dim; j++)
311 newcentroid[2][j] += elbg->
points[tempcell->index*elbg->
dim + j];
320 newutility[2] = (
tmp >= INT_MAX - newutility[2]) ? INT_MAX : newutility[2] +
tmp;
322 newerror = newutility[2];
325 elbg->
cells[idx[1]]);
326 if (
tmp >= INT_MAX - newerror)
331 if (olderror > newerror) {
334 elbg->
error += newerror - olderror;
352 for (idx[0]=0; idx[0] < elbg->
num_cb; idx[0]++)
360 if (idx[1] != idx[0] && idx[1] != idx[2])
368 int *
const size_part = elbg->size_part;
373 elbg->error = INT_MAX;
374 elbg->points = points;
377 cell *free_cells = elbg->cell_buffer;
378 last_error = elbg->error;
380 memset(elbg->utility, 0, elbg->num_cb *
sizeof(*elbg->utility));
381 memset(elbg->cells, 0, elbg->num_cb *
sizeof(*elbg->cells));
387 for (
i=0;
i < numpoints;
i++) {
389 elbg->codebook + best_idx * elbg->dim,
391 for (
int k = 0; k < elbg->num_cb; k++) {
393 elbg->codebook + k * elbg->dim,
394 elbg->dim, best_dist);
395 if (dist < best_dist) {
400 elbg->nearest_cb[
i] = best_idx;
401 elbg->error = (elbg->error >= INT_MAX - best_dist) ? INT_MAX : elbg->error + best_dist;
402 elbg->utility[elbg->nearest_cb[
i]] = (elbg->utility[elbg->nearest_cb[
i]] >= INT_MAX - best_dist) ?
403 INT_MAX : elbg->utility[elbg->nearest_cb[
i]] + best_dist;
404 free_cells->index =
i;
405 free_cells->next = elbg->cells[elbg->nearest_cb[
i]];
406 elbg->cells[elbg->nearest_cb[
i]] = free_cells;
412 memset(size_part, 0, elbg->num_cb *
sizeof(*size_part));
414 memset(elbg->codebook, 0, elbg->num_cb * elbg->dim *
sizeof(*elbg->codebook));
416 for (
i=0;
i < numpoints;
i++) {
417 size_part[elbg->nearest_cb[
i]]++;
418 for (j=0; j < elbg->dim; j++)
419 elbg->codebook[elbg->nearest_cb[
i]*elbg->dim + j] +=
420 elbg->points[
i*elbg->dim + j];
423 for (
int i = 0;
i < elbg->num_cb;
i++)
425 elbg->codebook +
i*elbg->dim, size_part[
i], elbg->dim);
427 }
while(((last_error - elbg->error) >
DELTA_ERR_MAX*elbg->error) &&
428 (
steps < max_steps));
431 #define BIG_PRIME 433494437LL
440 int numpoints,
int max_steps)
444 if (numpoints > 24LL * elbg->num_cb) {
447 for (
int i = 0;
i < numpoints / 8;
i++) {
449 memcpy(temp_points +
i*
dim, points + k*
dim,
dim *
sizeof(*temp_points));
454 init_elbg(elbg, temp_points, temp_points + numpoints / 8 *
dim,
455 numpoints / 8, 2 * max_steps);
456 do_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps);
458 for (
int i = 0;
i < elbg->num_cb;
i++)
460 dim *
sizeof(*elbg->codebook));
464 int *
codebook,
int num_cb,
int max_steps,
465 int *closest_cb,
AVLFG *rand_state, uintptr_t
flags)
474 elbg->rand_state = rand_state;
476 elbg->num_cb = num_cb;
479 #define ALLOCATE_IF_NECESSARY(field, new_elements, multiplicator) \
480 if (elbg->field ## _allocated < new_elements) { \
481 av_freep(&elbg->field); \
482 elbg->field = av_malloc_array(new_elements, \
483 multiplicator * sizeof(*elbg->field)); \
484 if (!elbg->field) { \
485 elbg->field ## _allocated = 0; \
486 return AVERROR(ENOMEM); \
488 elbg->field ## _allocated = new_elements; \
500 if (numpoints > 24LL * elbg->num_cb) {
505 uint64_t prod =
dim * (uint64_t)(numpoints / 7
U);
511 init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps);
512 do_elbg (elbg, points, numpoints, max_steps);