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.

418 lines
12KB

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