1 /*
2 * Copyright (C) 2007 Vitor Sessak <vitor1001@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * Codebook Generator using the ELBG algorithm
24 */
25
26 #include <string.h>
27
33
34 #define DELTA_ERR_MAX 0.1 ///< Precision of the ELBG algorithm (as percentage error)
35
36 /**
37 * In the ELBG jargon, a cell is the set of points that are closest to a
38 * codebook entry. Not to be confused with a RoQ Video cell. */
42 } cell;
43
44 /**
45 * ELBG internal data
46 */
62
63 /* Sizes for the buffers above. Pointers without such a field
64 * are not allocated by us and only valid for the duration
65 * of a single call to avpriv_elbg_do(). */
74
76 {
80
85 }
86
87 return dist;
88 }
89
91 {
93 if (div > 1)
96 else if (res != vect)
97 memcpy(res, vect,
dim*
sizeof(
int));
98
99 }
100
102 {
104 for (; cells; cells=cells->next) {
107 return INT_MAX;
109 }
110
112 }
113
115 {
116 int pick = 0;
117 for (
int i = 0, diff_min = INT_MAX;
i < elbg->
num_cb;
i++)
121 if (
diff < diff_min) {
124 }
125 }
126 return pick;
127 }
128
130 {
132 /* Using linear search, do binary if it ever turns to be speed critical */
134
137 } else {
140 }
141
144 }
145
147
149 }
150
151 /**
152 * Implementation of the simple LBG algorithm for just two codebooks
153 */
156 int *centroid[3],
157 int newutility[3],
158 int *points,
159 cell *cells)
160 {
162 int numpoints[2] = {0,0};
163 int *newcentroid[2] = {
166 };
167 cell *tempcell;
168
169 memset(newcentroid[0], 0, 2 *
dim *
sizeof(*newcentroid[0]));
170
171 newutility[0] =
172 newutility[1] = 0;
173
174 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
177 numpoints[idx]++;
179 newcentroid[idx][
i] += points[tempcell->index*
dim +
i];
180 }
181
184
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;
191 else
192 newutility[idx] += dist[idx];
193 }
194
195 return (newutility[0] >= INT_MAX - newutility[1]) ? INT_MAX : newutility[0] + newutility[1];
196 }
197
199 int *newcentroid_p)
200 {
201 cell *tempcell;
202 int *
min = newcentroid_i;
203 int *
max = newcentroid_p;
205
206 for (
i=0;
i< elbg->
dim;
i++) {
209 }
210
211 for (tempcell = elbg->
cells[huc]; tempcell; tempcell = tempcell->next)
212 for(
i=0;
i<elbg->
dim;
i++) {
215 }
216
217 for (
i=0;
i<elbg->
dim;
i++) {
220 newcentroid_i[
i] = ni;
221 newcentroid_p[
i] = np;
222 }
223 }
224
225 /**
226 * Add the points in the low utility cell to its closest cell. Split the high
227 * utility cell, putting the separated points in the (now empty) low utility
228 * cell.
229 *
230 * @param elbg Internal elbg data
231 * @param indexes {luc, huc, cluc}
232 * @param newcentroid A vector with the position of the new centroids
233 */
235 int *newcentroid[3])
236 {
237 cell *tempdata;
238 cell **pp = &elbg->
cells[indexes[2]];
239
240 while(*pp)
241 pp= &(*pp)->next;
242
243 *pp = elbg->
cells[indexes[0]];
244
246 tempdata = elbg->
cells[indexes[1]];
248
249 while(tempdata) {
250 cell *tempcell2 = tempdata->next;
252 newcentroid[0], elbg->
dim, INT_MAX) >
254 newcentroid[1], elbg->
dim, INT_MAX);
255
256 tempdata->next = elbg->
cells[indexes[idx]];
257 elbg->
cells[indexes[idx]] = tempdata;
258 tempdata = tempcell2;
259 }
260 }
261
263 {
265
270 }
271 }
272
273
275 {
276 cell *tempcell;
277
278 elbg->
utility[idx] = newutility;
279 for (tempcell=elbg->
cells[idx]; tempcell; tempcell=tempcell->next)
281 }
282
283 /**
284 * Evaluate if a shift lower the error. If it does, call shift_codebooks
285 * and update elbg->error, elbg->utility and elbg->nearest_cb.
286 *
287 * @param elbg Internal elbg data
288 * @param idx {luc (low utility cell, huc (high utility cell), cluc (closest cell to low utility cell)}
289 */
291 {
292 int j, k, cont=0,
tmp;
294 int newutility[3];
295 int *newcentroid[3] = {
299 };
300 cell *tempcell;
301
302 for (j=0; j<3; j++)
303 olderror += elbg->
utility[idx[j]];
304
305 memset(newcentroid[2], 0, elbg->
dim*
sizeof(
int));
306
307 for (k=0; k<2; k++)
308 for (tempcell=elbg->
cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
309 cont++;
310 for (j=0; j<elbg->
dim; j++)
311 newcentroid[2][j] += elbg->
points[tempcell->index*elbg->
dim + j];
312 }
313
315
317
320 newutility[2] = (
tmp >= INT_MAX - newutility[2]) ? INT_MAX : newutility[2] +
tmp;
321
322 newerror = newutility[2];
323
325 elbg->
cells[idx[1]]);
326 if (
tmp >= INT_MAX - newerror)
327 newerror = INT_MAX;
328 else
330
331 if (olderror > newerror) {
333
334 elbg->
error += newerror - olderror;
335
336 for (j=0; j<3; j++)
338
340 }
341 }
342
343 /**
344 * Implementation of the ELBG block
345 */
347 {
348 int idx[3];
349
351
352 for (idx[0]=0; idx[0] < elbg->
num_cb; idx[0]++)
355 return;
356
359
360 if (idx[1] != idx[0] && idx[1] != idx[2])
362 }
363 }
364
366 int max_steps)
367 {
368 int *const size_part = elbg->size_part;
370 int best_idx = 0;
371 int last_error;
372
373 elbg->error = INT_MAX;
374 elbg->points = points;
375
376 do {
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));
382
383 elbg->error = 0;
384
385 /* This loop evaluate the actual Voronoi partition. It is the most
386 costly part of the algorithm. */
387 for (
i=0;
i < numpoints;
i++) {
389 elbg->codebook + best_idx * elbg->dim,
390 elbg->dim, INT_MAX);
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) {
396 best_dist = dist;
397 best_idx = k;
398 }
399 }
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;
407 free_cells++;
408 }
409
411
412 memset(size_part, 0, elbg->num_cb * sizeof(*size_part));
413
414 memset(elbg->codebook, 0, elbg->num_cb * elbg->dim * sizeof(*elbg->codebook));
415
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];
421 }
422
423 for (
int i = 0;
i < elbg->num_cb;
i++)
425 elbg->codebook +
i*elbg->dim, size_part[
i], elbg->dim);
426
427 }
while(((last_error - elbg->error) >
DELTA_ERR_MAX*elbg->error) &&
428 (
steps < max_steps));
429 }
430
431 #define BIG_PRIME 433494437LL
432
433 /**
434 * Initialize the codebook vector for the elbg algorithm.
435 * If numpoints <= 24 * num_cb this function fills codebook with random numbers.
436 * If not, it calls do_elbg for a (smaller) random sample of the points in
437 * points.
438 */
440 int numpoints, int max_steps)
441 {
443
444 if (numpoints > 24LL * elbg->num_cb) {
445 /* ELBG is very costly for a big number of points. So if we have a lot
446 of them, get a good initial codebook to save on iterations */
447 for (
int i = 0;
i < numpoints / 8;
i++) {
449 memcpy(temp_points +
i*
dim, points + k*
dim,
dim *
sizeof(*temp_points));
450 }
451
452 /* If anything is changed in the recursion parameters,
453 * the allocated size of temp_points will also need to be updated. */
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);
457 } else // If not, initialize the codebook with random positions
458 for (
int i = 0;
i < elbg->num_cb;
i++)
460 dim *
sizeof(*elbg->codebook));
461 }
462
464 int *
codebook,
int num_cb,
int max_steps,
465 int *closest_cb,
AVLFG *rand_state, uintptr_t
flags)
466 {
468
469 if (!elbg)
471 *elbgp = elbg;
472
474 elbg->rand_state = rand_state;
476 elbg->num_cb = num_cb;
478
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); \
487 } \
488 elbg->field ## _allocated = new_elements; \
489 }
490 /* Allocating the buffers for do_elbg() here once relies
491 * on their size being always the same even when do_elbg()
492 * is called from init_elbg(). It also relies on do_elbg()
493 * never calling itself recursively. */
500 if (numpoints > 24LL * elbg->num_cb) {
501 /* The first step in the recursion in init_elbg() needs a buffer with
502 * (numpoints / 8) * dim elements; the next step needs numpoints / 8 / 8
503 * * dim elements etc. The geometric series leads to an upper bound of
504 * numpoints / 8 * 8 / 7 * dim elements. */
505 uint64_t prod =
dim * (uint64_t)(numpoints / 7
U);
506 if (prod > INT_MAX)
509 }
510
511 init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps);
512 do_elbg (elbg, points, numpoints, max_steps);
513 return 0;
514 }
515
517 {
519 if (!elbg)
520 return;
521
529
531 }