PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/chromium/third_party/ffmpeg/libavcodec/elbg.c

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