You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
13KB

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