elbg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. * @file libavcodec/elbg.c
  22. * Codebook Generator using the ELBG algorithm
  23. */
  24. #include <string.h>
  25. #include "libavutil/random.h"
  26. #include "elbg.h"
  27. #include "avcodec.h"
  28. #define DELTA_ERR_MAX 0.1 ///< Precision of the ELBG algorithm (as percentual error)
  29. /**
  30. * In the ELBG jargon, a cell is the set of points that are closest to a
  31. * codebook entry. Not to be confused with a RoQ Video cell. */
  32. typedef struct cell_s {
  33. int index;
  34. struct cell_s *next;
  35. } cell;
  36. /**
  37. * ELBG internal data
  38. */
  39. typedef struct{
  40. int error;
  41. int dim;
  42. int numCB;
  43. int *codebook;
  44. cell **cells;
  45. int *utility;
  46. int *utility_inc;
  47. int *nearest_cb;
  48. int *points;
  49. AVRandomState *rand_state;
  50. } elbg_data;
  51. static inline int distance_limited(int *a, int *b, int dim, int limit)
  52. {
  53. int i, dist=0;
  54. for (i=0; i<dim; i++) {
  55. dist += (a[i] - b[i])*(a[i] - b[i]);
  56. if (dist > limit)
  57. return INT_MAX;
  58. }
  59. return dist;
  60. }
  61. static inline void vect_division(int *res, int *vect, int div, int dim)
  62. {
  63. int i;
  64. if (div > 1)
  65. for (i=0; i<dim; i++)
  66. res[i] = ROUNDED_DIV(vect[i],div);
  67. else if (res != vect)
  68. memcpy(res, vect, dim*sizeof(int));
  69. }
  70. static int eval_error_cell(elbg_data *elbg, int *centroid, cell *cells)
  71. {
  72. int error=0;
  73. for (; cells; cells=cells->next)
  74. error += distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX);
  75. return error;
  76. }
  77. static int get_closest_codebook(elbg_data *elbg, int index)
  78. {
  79. int i, pick=0, diff, diff_min = INT_MAX;
  80. for (i=0; i<elbg->numCB; i++)
  81. if (i != index) {
  82. diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min);
  83. if (diff < diff_min) {
  84. pick = i;
  85. diff_min = diff;
  86. }
  87. }
  88. return pick;
  89. }
  90. static int get_high_utility_cell(elbg_data *elbg)
  91. {
  92. int i=0;
  93. /* Using linear search, do binary if it ever turns to be speed critical */
  94. int r = av_random(elbg->rand_state)%(elbg->utility_inc[elbg->numCB-1]-1) + 1;
  95. while (elbg->utility_inc[i] < r)
  96. i++;
  97. assert(!elbg->cells[i]);
  98. return i;
  99. }
  100. /**
  101. * Implementation of the simple LBG algorithm for just two codebooks
  102. */
  103. static int simple_lbg(int dim,
  104. int *centroid[3],
  105. int newutility[3],
  106. int *points,
  107. cell *cells)
  108. {
  109. int i, idx;
  110. int numpoints[2] = {0,0};
  111. int newcentroid[2][dim];
  112. cell *tempcell;
  113. memset(newcentroid, 0, sizeof(newcentroid));
  114. newutility[0] =
  115. newutility[1] = 0;
  116. for (tempcell = cells; tempcell; tempcell=tempcell->next) {
  117. idx = distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX)>=
  118. distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX);
  119. numpoints[idx]++;
  120. for (i=0; i<dim; i++)
  121. newcentroid[idx][i] += points[tempcell->index*dim + i];
  122. }
  123. vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
  124. vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
  125. for (tempcell = cells; tempcell; tempcell=tempcell->next) {
  126. int dist[2] = {distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX),
  127. distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX)};
  128. int idx = dist[0] > dist[1];
  129. newutility[idx] += dist[idx];
  130. }
  131. return newutility[0] + newutility[1];
  132. }
  133. static void get_new_centroids(elbg_data *elbg, int huc, int *newcentroid_i,
  134. int *newcentroid_p)
  135. {
  136. cell *tempcell;
  137. int min[elbg->dim];
  138. int max[elbg->dim];
  139. int i;
  140. for (i=0; i< elbg->dim; i++) {
  141. min[i]=INT_MAX;
  142. max[i]=0;
  143. }
  144. for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
  145. for(i=0; i<elbg->dim; i++) {
  146. min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
  147. max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
  148. }
  149. for (i=0; i<elbg->dim; i++) {
  150. newcentroid_i[i] = min[i] + (max[i] - min[i])/3;
  151. newcentroid_p[i] = min[i] + (2*(max[i] - min[i]))/3;
  152. }
  153. }
  154. /**
  155. * Add the points in the low utility cell to its closest cell. Split the high
  156. * utility cell, putting the separed points in the (now empty) low utility
  157. * cell.
  158. *
  159. * @param elbg Internal elbg data
  160. * @param indexes {luc, huc, cluc}
  161. * @param newcentroid A vector with the position of the new centroids
  162. */
  163. static void shift_codebook(elbg_data *elbg, int *indexes,
  164. int *newcentroid[3])
  165. {
  166. cell *tempdata;
  167. cell **pp = &elbg->cells[indexes[2]];
  168. while(*pp)
  169. pp= &(*pp)->next;
  170. *pp = elbg->cells[indexes[0]];
  171. elbg->cells[indexes[0]] = NULL;
  172. tempdata = elbg->cells[indexes[1]];
  173. elbg->cells[indexes[1]] = NULL;
  174. while(tempdata) {
  175. cell *tempcell2 = tempdata->next;
  176. int idx = distance_limited(elbg->points + tempdata->index*elbg->dim,
  177. newcentroid[0], elbg->dim, INT_MAX) >
  178. distance_limited(elbg->points + tempdata->index*elbg->dim,
  179. newcentroid[1], elbg->dim, INT_MAX);
  180. tempdata->next = elbg->cells[indexes[idx]];
  181. elbg->cells[indexes[idx]] = tempdata;
  182. tempdata = tempcell2;
  183. }
  184. }
  185. static void evaluate_utility_inc(elbg_data *elbg)
  186. {
  187. int i, inc=0;
  188. for (i=0; i < elbg->numCB; i++) {
  189. if (elbg->numCB*elbg->utility[i] > elbg->error)
  190. inc += elbg->utility[i];
  191. elbg->utility_inc[i] = inc;
  192. }
  193. }
  194. static void update_utility_and_n_cb(elbg_data *elbg, int idx, int newutility)
  195. {
  196. cell *tempcell;
  197. elbg->utility[idx] = newutility;
  198. for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next)
  199. elbg->nearest_cb[tempcell->index] = idx;
  200. }
  201. /**
  202. * Evaluate if a shift lower the error. If it does, call shift_codebooks
  203. * and update elbg->error, elbg->utility and elbg->nearest_cb.
  204. *
  205. * @param elbg Internal elbg data
  206. * @param indexes {luc (low utility cell, huc (high utility cell), cluc (closest cell to low utility cell)}
  207. */
  208. static void try_shift_candidate(elbg_data *elbg, int idx[3])
  209. {
  210. int j, k, olderror=0, newerror, cont=0;
  211. int newutility[3];
  212. int newcentroid[3][elbg->dim];
  213. int *newcentroid_ptrs[3];
  214. cell *tempcell;
  215. newcentroid_ptrs[0] = newcentroid[0];
  216. newcentroid_ptrs[1] = newcentroid[1];
  217. newcentroid_ptrs[2] = newcentroid[2];
  218. for (j=0; j<3; j++)
  219. olderror += elbg->utility[idx[j]];
  220. memset(newcentroid[2], 0, elbg->dim*sizeof(int));
  221. for (k=0; k<2; k++)
  222. for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
  223. cont++;
  224. for (j=0; j<elbg->dim; j++)
  225. newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j];
  226. }
  227. vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim);
  228. get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]);
  229. newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]);
  230. newutility[2] += eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]);
  231. newerror = newutility[2];
  232. newerror += simple_lbg(elbg->dim, newcentroid_ptrs, newutility, elbg->points,
  233. elbg->cells[idx[1]]);
  234. if (olderror > newerror) {
  235. shift_codebook(elbg, idx, newcentroid_ptrs);
  236. elbg->error += newerror - olderror;
  237. for (j=0; j<3; j++)
  238. update_utility_and_n_cb(elbg, idx[j], newutility[j]);
  239. evaluate_utility_inc(elbg);
  240. }
  241. }
  242. /**
  243. * Implementation of the ELBG block
  244. */
  245. static void do_shiftings(elbg_data *elbg)
  246. {
  247. int idx[3];
  248. evaluate_utility_inc(elbg);
  249. for (idx[0]=0; idx[0] < elbg->numCB; idx[0]++)
  250. if (elbg->numCB*elbg->utility[idx[0]] < elbg->error) {
  251. if (elbg->utility_inc[elbg->numCB-1] == 0)
  252. return;
  253. idx[1] = get_high_utility_cell(elbg);
  254. idx[2] = get_closest_codebook(elbg, idx[0]);
  255. if (idx[1] != idx[0] && idx[1] != idx[2])
  256. try_shift_candidate(elbg, idx);
  257. }
  258. }
  259. #define BIG_PRIME 433494437LL
  260. void ff_init_elbg(int *points, int dim, int numpoints, int *codebook,
  261. int numCB, int max_steps, int *closest_cb,
  262. AVRandomState *rand_state)
  263. {
  264. int i, k;
  265. if (numpoints > 24*numCB) {
  266. /* ELBG is very costly for a big number of points. So if we have a lot
  267. of them, get a good initial codebook to save on iterations */
  268. int *temp_points = av_malloc(dim*(numpoints/8)*sizeof(int));
  269. for (i=0; i<numpoints/8; i++) {
  270. k = (i*BIG_PRIME) % numpoints;
  271. memcpy(temp_points + i*dim, points + k*dim, dim*sizeof(int));
  272. }
  273. ff_init_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state);
  274. ff_do_elbg(temp_points, dim, numpoints/8, codebook, numCB, 2*max_steps, closest_cb, rand_state);
  275. av_free(temp_points);
  276. } else // If not, initialize the codebook with random positions
  277. for (i=0; i < numCB; i++)
  278. memcpy(codebook + i*dim, points + ((i*BIG_PRIME)%numpoints)*dim,
  279. dim*sizeof(int));
  280. }
  281. void ff_do_elbg(int *points, int dim, int numpoints, int *codebook,
  282. int numCB, int max_steps, int *closest_cb,
  283. AVRandomState *rand_state)
  284. {
  285. int dist;
  286. elbg_data elbg_d;
  287. elbg_data *elbg = &elbg_d;
  288. int i, j, k, last_error, steps=0;
  289. int *dist_cb = av_malloc(numpoints*sizeof(int));
  290. int *size_part = av_malloc(numCB*sizeof(int));
  291. cell *list_buffer = av_malloc(numpoints*sizeof(cell));
  292. cell *free_cells;
  293. elbg->error = INT_MAX;
  294. elbg->dim = dim;
  295. elbg->numCB = numCB;
  296. elbg->codebook = codebook;
  297. elbg->cells = av_malloc(numCB*sizeof(cell *));
  298. elbg->utility = av_malloc(numCB*sizeof(int));
  299. elbg->nearest_cb = closest_cb;
  300. elbg->points = points;
  301. elbg->utility_inc = av_malloc(numCB*sizeof(int));
  302. elbg->rand_state = rand_state;
  303. do {
  304. free_cells = list_buffer;
  305. last_error = elbg->error;
  306. steps++;
  307. memset(elbg->utility, 0, numCB*sizeof(int));
  308. memset(elbg->cells, 0, numCB*sizeof(cell *));
  309. elbg->error = 0;
  310. /* This loop evaluate the actual Voronoi partition. It is the most
  311. costly part of the algorithm. */
  312. for (i=0; i < numpoints; i++) {
  313. dist_cb[i] = INT_MAX;
  314. for (k=0; k < elbg->numCB; k++) {
  315. dist = distance_limited(elbg->points + i*elbg->dim, elbg->codebook + k*elbg->dim, dim, dist_cb[i]);
  316. if (dist < dist_cb[i]) {
  317. dist_cb[i] = dist;
  318. elbg->nearest_cb[i] = k;
  319. }
  320. }
  321. elbg->error += dist_cb[i];
  322. elbg->utility[elbg->nearest_cb[i]] += dist_cb[i];
  323. free_cells->index = i;
  324. free_cells->next = elbg->cells[elbg->nearest_cb[i]];
  325. elbg->cells[elbg->nearest_cb[i]] = free_cells;
  326. free_cells++;
  327. }
  328. do_shiftings(elbg);
  329. memset(size_part, 0, numCB*sizeof(int));
  330. memset(elbg->codebook, 0, elbg->numCB*dim*sizeof(int));
  331. for (i=0; i < numpoints; i++) {
  332. size_part[elbg->nearest_cb[i]]++;
  333. for (j=0; j < elbg->dim; j++)
  334. elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] +=
  335. elbg->points[i*elbg->dim + j];
  336. }
  337. for (i=0; i < elbg->numCB; i++)
  338. vect_division(elbg->codebook + i*elbg->dim,
  339. elbg->codebook + i*elbg->dim, size_part[i], elbg->dim);
  340. } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) &&
  341. (steps < max_steps));
  342. av_free(dist_cb);
  343. av_free(size_part);
  344. av_free(elbg->utility);
  345. av_free(list_buffer);
  346. av_free(elbg->cells);
  347. av_free(elbg->utility_inc);
  348. }