33 #define DELTA_ERR_MAX 0.1
96 memcpy(res, vect,
dim*
sizeof(
int));
103 for (; cells; cells=cells->next) {
116 for (
int i = 0, diff_min = INT_MAX;
i < elbg->
num_cb;
i++)
120 if (
diff < diff_min) {
161 int numpoints[2] = {0,0};
162 int *newcentroid[2] = {
168 memset(newcentroid[0], 0, 2 *
dim *
sizeof(*newcentroid[0]));
173 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
178 newcentroid[idx][
i] += points[tempcell->index*
dim +
i];
184 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
187 int idx = dist[0] > dist[1];
188 if (newutility[idx] >= INT_MAX - dist[idx])
189 newutility[idx] = INT_MAX;
191 newutility[idx] += dist[idx];
194 return (newutility[0] >= INT_MAX - newutility[1]) ? INT_MAX : newutility[0] + newutility[1];
201 int *
min = newcentroid_i;
202 int *
max = newcentroid_p;
205 for (
i=0;
i< elbg->
dim;
i++) {
210 for (tempcell = elbg->
cells[huc]; tempcell; tempcell = tempcell->next)
211 for(
i=0;
i<elbg->
dim;
i++) {
216 for (
i=0;
i<elbg->
dim;
i++) {
219 newcentroid_i[
i] = ni;
220 newcentroid_p[
i] = np;
237 cell **pp = &elbg->
cells[indexes[2]];
242 *pp = elbg->
cells[indexes[0]];
245 tempdata = elbg->
cells[indexes[1]];
249 cell *tempcell2 = tempdata->next;
251 newcentroid[0], elbg->
dim, INT_MAX) >
253 newcentroid[1], elbg->
dim, INT_MAX);
255 tempdata->next = elbg->
cells[indexes[idx]];
256 elbg->
cells[indexes[idx]] = tempdata;
257 tempdata = tempcell2;
277 elbg->
utility[idx] = newutility;
278 for (tempcell=elbg->
cells[idx]; tempcell; tempcell=tempcell->next)
291 int j, k, cont=0,
tmp;
292 int64_t olderror=0, newerror;
294 int *newcentroid[3] = {
302 olderror += elbg->
utility[idx[j]];
304 memset(newcentroid[2], 0, elbg->
dim*
sizeof(
int));
307 for (tempcell=elbg->
cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
309 for (j=0; j<elbg->
dim; j++)
310 newcentroid[2][j] += elbg->
points[tempcell->index*elbg->
dim + j];
319 newutility[2] = (
tmp >= INT_MAX - newutility[2]) ? INT_MAX : newutility[2] +
tmp;
321 newerror = newutility[2];
324 elbg->
cells[idx[1]]);
325 if (
tmp >= INT_MAX - newerror)
330 if (olderror > newerror) {
333 elbg->
error += newerror - olderror;
351 for (idx[0]=0; idx[0] < elbg->
num_cb; idx[0]++)
359 if (idx[1] != idx[0] && idx[1] != idx[2])
367 int *
const size_part = elbg->size_part;
372 elbg->error = INT_MAX;
373 elbg->points = points;
376 cell *free_cells = elbg->cell_buffer;
377 last_error = elbg->error;
379 memset(elbg->utility, 0, elbg->num_cb *
sizeof(*elbg->utility));
380 memset(elbg->cells, 0, elbg->num_cb *
sizeof(*elbg->cells));
386 for (
i=0;
i < numpoints;
i++) {
388 elbg->codebook + best_idx * elbg->dim,
390 for (
int k = 0; k < elbg->num_cb; k++) {
392 elbg->codebook + k * elbg->dim,
393 elbg->dim, best_dist);
394 if (dist < best_dist) {
399 elbg->nearest_cb[
i] = best_idx;
400 elbg->error = (elbg->error >= INT_MAX - best_dist) ? INT_MAX : elbg->error + best_dist;
401 elbg->utility[elbg->nearest_cb[
i]] = (elbg->utility[elbg->nearest_cb[
i]] >= INT_MAX - best_dist) ?
402 INT_MAX : elbg->utility[elbg->nearest_cb[
i]] + best_dist;
403 free_cells->index =
i;
404 free_cells->next = elbg->cells[elbg->nearest_cb[
i]];
405 elbg->cells[elbg->nearest_cb[
i]] = free_cells;
411 memset(size_part, 0, elbg->num_cb *
sizeof(*size_part));
413 memset(elbg->codebook, 0, elbg->num_cb * elbg->dim *
sizeof(*elbg->codebook));
415 for (
i=0;
i < numpoints;
i++) {
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 (
int i = 0;
i < elbg->num_cb;
i++)
424 elbg->codebook +
i*elbg->dim, size_part[
i], elbg->dim);
426 }
while(((last_error - elbg->error) >
DELTA_ERR_MAX*elbg->error) &&
427 (
steps < max_steps));
430 #define BIG_PRIME 433494437LL
439 int numpoints,
int max_steps)
443 if (numpoints > 24LL * elbg->num_cb) {
446 for (
int i = 0;
i < numpoints / 8;
i++) {
448 memcpy(temp_points +
i*
dim, points + k*
dim,
dim *
sizeof(*temp_points));
453 init_elbg(elbg, temp_points, temp_points + numpoints / 8 *
dim,
454 numpoints / 8, 2 * max_steps);
455 do_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps);
457 for (
int i = 0;
i < elbg->num_cb;
i++)
459 dim *
sizeof(*elbg->codebook));
463 int *
codebook,
int num_cb,
int max_steps,
464 int *closest_cb,
AVLFG *rand_state, uintptr_t
flags)
473 elbg->rand_state = rand_state;
475 elbg->num_cb = num_cb;
478 #define ALLOCATE_IF_NECESSARY(field, new_elements, multiplicator) \
479 if (elbg->field ## _allocated < new_elements) { \
480 av_freep(&elbg->field); \
481 elbg->field = av_malloc_array(new_elements, \
482 multiplicator * sizeof(*elbg->field)); \
483 if (!elbg->field) { \
484 elbg->field ## _allocated = 0; \
485 return AVERROR(ENOMEM); \
487 elbg->field ## _allocated = new_elements; \
499 if (numpoints > 24LL * elbg->num_cb) {
504 uint64_t prod =
dim * (uint64_t)(numpoints / 7
U);
510 init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps);
511 do_elbg (elbg, points, numpoints, max_steps);