| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 28 | #include "libavutil/avassert.h" | ||
| 29 | #include "libavutil/common.h" | ||
| 30 | #include "libavutil/lfg.h" | ||
| 31 | #include "libavutil/mem.h" | ||
| 32 | #include "elbg.h" | ||
| 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. */ | ||
| 39 | typedef struct cell_s { | ||
| 40 | int index; | ||
| 41 | struct cell_s *next; | ||
| 42 | } cell; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * ELBG internal data | ||
| 46 | */ | ||
| 47 | typedef struct ELBGContext { | ||
| 48 | int error; | ||
| 49 | int dim; | ||
| 50 | int num_cb; | ||
| 51 | int *codebook; | ||
| 52 | cell **cells; | ||
| 53 | int *utility; | ||
| 54 | int *utility_inc; | ||
| 55 | int *nearest_cb; | ||
| 56 | int *points; | ||
| 57 | int *temp_points; | ||
| 58 | int *size_part; | ||
| 59 | AVLFG *rand_state; | ||
| 60 | int *scratchbuf; | ||
| 61 | cell *cell_buffer; | ||
| 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(). */ | ||
| 66 | unsigned utility_allocated; | ||
| 67 | unsigned utility_inc_allocated; | ||
| 68 | unsigned size_part_allocated; | ||
| 69 | unsigned cells_allocated; | ||
| 70 | unsigned scratchbuf_allocated; | ||
| 71 | unsigned cell_buffer_allocated; | ||
| 72 | unsigned temp_points_allocated; | ||
| 73 | } ELBGContext; | ||
| 74 | |||
| 75 | 2467099302 | static inline int distance_limited(int *a, int *b, int dim, int limit) | |
| 76 | { | ||
| 77 | 2467099302 | int i, dist=0; | |
| 78 |
2/2✓ Branch 0 taken 4856309129 times.
✓ Branch 1 taken 274726025 times.
|
5131035154 | for (i=0; i<dim; i++) { |
| 79 | 4856309129 | int64_t distance = a[i] - b[i]; | |
| 80 | |||
| 81 | 4856309129 | distance *= distance; | |
| 82 |
2/2✓ Branch 0 taken 2192373277 times.
✓ Branch 1 taken 2663935852 times.
|
4856309129 | if (dist >= limit - distance) |
| 83 | 2192373277 | return limit; | |
| 84 | 2663935852 | dist += distance; | |
| 85 | } | ||
| 86 | |||
| 87 | 274726025 | return dist; | |
| 88 | } | ||
| 89 | |||
| 90 | 17717892 | static inline void vect_division(int *res, int *vect, int div, int dim) | |
| 91 | { | ||
| 92 | int i; | ||
| 93 |
2/2✓ Branch 0 taken 12934031 times.
✓ Branch 1 taken 4783861 times.
|
17717892 | if (div > 1) |
| 94 |
2/2✓ Branch 0 taken 55982082 times.
✓ Branch 1 taken 12934031 times.
|
68916113 | for (i=0; i<dim; i++) |
| 95 |
1/2✓ Branch 0 taken 55982082 times.
✗ Branch 1 not taken.
|
55982082 | res[i] = ROUNDED_DIV(vect[i],div); |
| 96 |
2/2✓ Branch 0 taken 733261 times.
✓ Branch 1 taken 4050600 times.
|
4783861 | else if (res != vect) |
| 97 | 733261 | memcpy(res, vect, dim*sizeof(int)); | |
| 98 | |||
| 99 | 17717892 | } | |
| 100 | |||
| 101 | 2884916 | static int eval_error_cell(ELBGContext *elbg, int *centroid, cell *cells) | |
| 102 | { | ||
| 103 | 2884916 | int error=0; | |
| 104 |
2/2✓ Branch 0 taken 25212535 times.
✓ Branch 1 taken 2884916 times.
|
28097451 | for (; cells; cells=cells->next) { |
| 105 | 25212535 | int distance = distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX); | |
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25212535 times.
|
25212535 | if (error >= INT_MAX - distance) |
| 107 | ✗ | return INT_MAX; | |
| 108 | 25212535 | error += distance; | |
| 109 | } | ||
| 110 | |||
| 111 | 2884916 | return error; | |
| 112 | } | ||
| 113 | |||
| 114 | 5872777 | static int get_closest_codebook(ELBGContext *elbg, int index) | |
| 115 | { | ||
| 116 | 5872777 | int pick = 0; | |
| 117 |
2/2✓ Branch 0 taken 227297072 times.
✓ Branch 1 taken 5872777 times.
|
233169849 | for (int i = 0, diff_min = INT_MAX; i < elbg->num_cb; i++) |
| 118 |
2/2✓ Branch 0 taken 221424295 times.
✓ Branch 1 taken 5872777 times.
|
227297072 | if (i != index) { |
| 119 | int diff; | ||
| 120 | 221424295 | diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min); | |
| 121 |
2/2✓ Branch 0 taken 11935603 times.
✓ Branch 1 taken 209488692 times.
|
221424295 | if (diff < diff_min) { |
| 122 | 11935603 | pick = i; | |
| 123 | 11935603 | diff_min = diff; | |
| 124 | } | ||
| 125 | } | ||
| 126 | 5872777 | return pick; | |
| 127 | } | ||
| 128 | |||
| 129 | 5872777 | static int get_high_utility_cell(ELBGContext *elbg) | |
| 130 | { | ||
| 131 | 5872777 | int i=0; | |
| 132 | /* Using linear search, do binary if it ever turns to be speed critical */ | ||
| 133 | uint64_t r; | ||
| 134 | |||
| 135 |
1/2✓ Branch 0 taken 5872777 times.
✗ Branch 1 not taken.
|
5872777 | if (elbg->utility_inc[elbg->num_cb - 1] < INT_MAX) { |
| 136 | 5872777 | r = av_lfg_get(elbg->rand_state) % (unsigned int)elbg->utility_inc[elbg->num_cb - 1] + 1; | |
| 137 | } else { | ||
| 138 | ✗ | r = av_lfg_get(elbg->rand_state); | |
| 139 | ✗ | r = (av_lfg_get(elbg->rand_state) + (r<<32)) % elbg->utility_inc[elbg->num_cb - 1] + 1; | |
| 140 | } | ||
| 141 | |||
| 142 |
2/2✓ Branch 0 taken 109344535 times.
✓ Branch 1 taken 5872777 times.
|
115217312 | while (elbg->utility_inc[i] < r) { |
| 143 | 109344535 | i++; | |
| 144 | } | ||
| 145 | |||
| 146 | av_assert2(elbg->cells[i]); | ||
| 147 | |||
| 148 | 5872777 | return i; | |
| 149 | } | ||
| 150 | |||
| 151 | /** | ||
| 152 | * Implementation of the simple LBG algorithm for just two codebooks | ||
| 153 | */ | ||
| 154 | 1442458 | static int simple_lbg(ELBGContext *elbg, | |
| 155 | int dim, | ||
| 156 | int *centroid[3], | ||
| 157 | int newutility[3], | ||
| 158 | int *points, | ||
| 159 | cell *cells) | ||
| 160 | { | ||
| 161 | int i, idx; | ||
| 162 | 1442458 | int numpoints[2] = {0,0}; | |
| 163 | 1442458 | int *newcentroid[2] = { | |
| 164 | 1442458 | elbg->scratchbuf + 3*dim, | |
| 165 | 1442458 | elbg->scratchbuf + 4*dim | |
| 166 | }; | ||
| 167 | cell *tempcell; | ||
| 168 | |||
| 169 | 1442458 | memset(newcentroid[0], 0, 2 * dim * sizeof(*newcentroid[0])); | |
| 170 | |||
| 171 | 1442458 | newutility[0] = | |
| 172 | 1442458 | newutility[1] = 0; | |
| 173 | |||
| 174 |
2/2✓ Branch 0 taken 19715357 times.
✓ Branch 1 taken 1442458 times.
|
21157815 | for (tempcell = cells; tempcell; tempcell=tempcell->next) { |
| 175 | 19715357 | idx = distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX)>= | |
| 176 | 19715357 | distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX); | |
| 177 | 19715357 | numpoints[idx]++; | |
| 178 |
2/2✓ Branch 0 taken 121479870 times.
✓ Branch 1 taken 19715357 times.
|
141195227 | for (i=0; i<dim; i++) |
| 179 | 121479870 | newcentroid[idx][i] += points[tempcell->index*dim + i]; | |
| 180 | } | ||
| 181 | |||
| 182 | 1442458 | vect_division(centroid[0], newcentroid[0], numpoints[0], dim); | |
| 183 | 1442458 | vect_division(centroid[1], newcentroid[1], numpoints[1], dim); | |
| 184 | |||
| 185 |
2/2✓ Branch 0 taken 19715357 times.
✓ Branch 1 taken 1442458 times.
|
21157815 | for (tempcell = cells; tempcell; tempcell=tempcell->next) { |
| 186 | 19715357 | int dist[2] = {distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX), | |
| 187 | 19715357 | distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX)}; | |
| 188 | 19715357 | int idx = dist[0] > dist[1]; | |
| 189 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19715357 times.
|
19715357 | if (newutility[idx] >= INT_MAX - dist[idx]) |
| 190 | ✗ | newutility[idx] = INT_MAX; | |
| 191 | else | ||
| 192 | 19715357 | newutility[idx] += dist[idx]; | |
| 193 | } | ||
| 194 | |||
| 195 |
1/2✓ Branch 0 taken 1442458 times.
✗ Branch 1 not taken.
|
1442458 | return (newutility[0] >= INT_MAX - newutility[1]) ? INT_MAX : newutility[0] + newutility[1]; |
| 196 | } | ||
| 197 | |||
| 198 | 1442458 | static void get_new_centroids(ELBGContext *elbg, int huc, int *newcentroid_i, | |
| 199 | int *newcentroid_p) | ||
| 200 | { | ||
| 201 | cell *tempcell; | ||
| 202 | 1442458 | int *min = newcentroid_i; | |
| 203 | 1442458 | int *max = newcentroid_p; | |
| 204 | int i; | ||
| 205 | |||
| 206 |
2/2✓ Branch 0 taken 8844054 times.
✓ Branch 1 taken 1442458 times.
|
10286512 | for (i=0; i< elbg->dim; i++) { |
| 207 | 8844054 | min[i]=INT_MAX; | |
| 208 | 8844054 | max[i]=0; | |
| 209 | } | ||
| 210 | |||
| 211 |
2/2✓ Branch 0 taken 19715357 times.
✓ Branch 1 taken 1442458 times.
|
21157815 | for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next) |
| 212 |
2/2✓ Branch 0 taken 121479870 times.
✓ Branch 1 taken 19715357 times.
|
141195227 | for(i=0; i<elbg->dim; i++) { |
| 213 | 121479870 | min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]); | |
| 214 | 121479870 | max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]); | |
| 215 | } | ||
| 216 | |||
| 217 |
2/2✓ Branch 0 taken 8844054 times.
✓ Branch 1 taken 1442458 times.
|
10286512 | for (i=0; i<elbg->dim; i++) { |
| 218 | 8844054 | int ni = min[i] + (max[i] - min[i])/3; | |
| 219 | 8844054 | int np = min[i] + (2*(max[i] - min[i]))/3; | |
| 220 | 8844054 | newcentroid_i[i] = ni; | |
| 221 | 8844054 | newcentroid_p[i] = np; | |
| 222 | } | ||
| 223 | 1442458 | } | |
| 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 | */ | ||
| 234 | 740324 | static void shift_codebook(ELBGContext *elbg, int *indexes, | |
| 235 | int *newcentroid[3]) | ||
| 236 | { | ||
| 237 | cell *tempdata; | ||
| 238 | 740324 | cell **pp = &elbg->cells[indexes[2]]; | |
| 239 | |||
| 240 |
2/2✓ Branch 0 taken 5867837 times.
✓ Branch 1 taken 740324 times.
|
6608161 | while(*pp) |
| 241 | 5867837 | pp= &(*pp)->next; | |
| 242 | |||
| 243 | 740324 | *pp = elbg->cells[indexes[0]]; | |
| 244 | |||
| 245 | 740324 | elbg->cells[indexes[0]] = NULL; | |
| 246 | 740324 | tempdata = elbg->cells[indexes[1]]; | |
| 247 | 740324 | elbg->cells[indexes[1]] = NULL; | |
| 248 | |||
| 249 |
2/2✓ Branch 0 taken 8414374 times.
✓ Branch 1 taken 740324 times.
|
9154698 | while(tempdata) { |
| 250 | 8414374 | cell *tempcell2 = tempdata->next; | |
| 251 | 8414374 | int idx = distance_limited(elbg->points + tempdata->index*elbg->dim, | |
| 252 | 8414374 | newcentroid[0], elbg->dim, INT_MAX) > | |
| 253 | 8414374 | distance_limited(elbg->points + tempdata->index*elbg->dim, | |
| 254 | 8414374 | newcentroid[1], elbg->dim, INT_MAX); | |
| 255 | |||
| 256 | 8414374 | tempdata->next = elbg->cells[indexes[idx]]; | |
| 257 | 8414374 | elbg->cells[indexes[idx]] = tempdata; | |
| 258 | 8414374 | tempdata = tempcell2; | |
| 259 | } | ||
| 260 | 740324 | } | |
| 261 | |||
| 262 | 6568264 | static void evaluate_utility_inc(ELBGContext *elbg) | |
| 263 | { | ||
| 264 | 6568264 | int64_t inc=0; | |
| 265 | |||
| 266 |
2/2✓ Branch 0 taken 128855417 times.
✓ Branch 1 taken 6568264 times.
|
135423681 | for (int i = 0; i < elbg->num_cb; i++) { |
| 267 |
2/2✓ Branch 0 taken 44579234 times.
✓ Branch 1 taken 84276183 times.
|
128855417 | if (elbg->num_cb * (int64_t)elbg->utility[i] > elbg->error) |
| 268 | 44579234 | inc += elbg->utility[i]; | |
| 269 | 128855417 | elbg->utility_inc[i] = FFMIN(inc, INT_MAX); | |
| 270 | } | ||
| 271 | 6568264 | } | |
| 272 | |||
| 273 | |||
| 274 | 2220972 | static void update_utility_and_n_cb(ELBGContext *elbg, int idx, int newutility) | |
| 275 | { | ||
| 276 | cell *tempcell; | ||
| 277 | |||
| 278 | 2220972 | elbg->utility[idx] = newutility; | |
| 279 |
2/2✓ Branch 0 taken 18404535 times.
✓ Branch 1 taken 2220972 times.
|
20625507 | for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next) |
| 280 | 18404535 | elbg->nearest_cb[tempcell->index] = idx; | |
| 281 | 2220972 | } | |
| 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 | */ | ||
| 290 | 1442458 | static void try_shift_candidate(ELBGContext *elbg, int idx[3]) | |
| 291 | { | ||
| 292 | 1442458 | int j, k, cont=0, tmp; | |
| 293 | 1442458 | int64_t olderror=0, newerror; | |
| 294 | int newutility[3]; | ||
| 295 | 1442458 | int *newcentroid[3] = { | |
| 296 | 1442458 | elbg->scratchbuf, | |
| 297 | 1442458 | elbg->scratchbuf + elbg->dim, | |
| 298 | 1442458 | elbg->scratchbuf + 2*elbg->dim | |
| 299 | }; | ||
| 300 | cell *tempcell; | ||
| 301 | |||
| 302 |
2/2✓ Branch 0 taken 4327374 times.
✓ Branch 1 taken 1442458 times.
|
5769832 | for (j=0; j<3; j++) |
| 303 | 4327374 | olderror += elbg->utility[idx[j]]; | |
| 304 | |||
| 305 | 1442458 | memset(newcentroid[2], 0, elbg->dim*sizeof(int)); | |
| 306 | |||
| 307 |
2/2✓ Branch 0 taken 2884916 times.
✓ Branch 1 taken 1442458 times.
|
4327374 | for (k=0; k<2; k++) |
| 308 |
2/2✓ Branch 0 taken 25212535 times.
✓ Branch 1 taken 2884916 times.
|
28097451 | for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) { |
| 309 | 25212535 | cont++; | |
| 310 |
2/2✓ Branch 0 taken 158361162 times.
✓ Branch 1 taken 25212535 times.
|
183573697 | for (j=0; j<elbg->dim; j++) |
| 311 | 158361162 | newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j]; | |
| 312 | } | ||
| 313 | |||
| 314 | 1442458 | vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim); | |
| 315 | |||
| 316 | 1442458 | get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]); | |
| 317 | |||
| 318 | 1442458 | newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]); | |
| 319 | 1442458 | tmp = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]); | |
| 320 |
1/2✓ Branch 0 taken 1442458 times.
✗ Branch 1 not taken.
|
1442458 | newutility[2] = (tmp >= INT_MAX - newutility[2]) ? INT_MAX : newutility[2] + tmp; |
| 321 | |||
| 322 | 1442458 | newerror = newutility[2]; | |
| 323 | |||
| 324 | 1442458 | tmp = simple_lbg(elbg, elbg->dim, newcentroid, newutility, elbg->points, | |
| 325 | 1442458 | elbg->cells[idx[1]]); | |
| 326 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1442458 times.
|
1442458 | if (tmp >= INT_MAX - newerror) |
| 327 | ✗ | newerror = INT_MAX; | |
| 328 | else | ||
| 329 | 1442458 | newerror += tmp; | |
| 330 | |||
| 331 |
2/2✓ Branch 0 taken 740324 times.
✓ Branch 1 taken 702134 times.
|
1442458 | if (olderror > newerror) { |
| 332 | 740324 | shift_codebook(elbg, idx, newcentroid); | |
| 333 | |||
| 334 | 740324 | elbg->error += newerror - olderror; | |
| 335 | |||
| 336 |
2/2✓ Branch 0 taken 2220972 times.
✓ Branch 1 taken 740324 times.
|
2961296 | for (j=0; j<3; j++) |
| 337 | 2220972 | update_utility_and_n_cb(elbg, idx[j], newutility[j]); | |
| 338 | |||
| 339 | 740324 | evaluate_utility_inc(elbg); | |
| 340 | } | ||
| 341 | 1442458 | } | |
| 342 | |||
| 343 | /** | ||
| 344 | * Implementation of the ELBG block | ||
| 345 | */ | ||
| 346 | 5827940 | static void do_shiftings(ELBGContext *elbg) | |
| 347 | { | ||
| 348 | int idx[3]; | ||
| 349 | |||
| 350 | 5827940 | evaluate_utility_inc(elbg); | |
| 351 | |||
| 352 |
2/2✓ Branch 0 taken 13390518 times.
✓ Branch 1 taken 5827940 times.
|
19218458 | for (idx[0]=0; idx[0] < elbg->num_cb; idx[0]++) |
| 353 |
2/2✓ Branch 0 taken 5872777 times.
✓ Branch 1 taken 7517741 times.
|
13390518 | if (elbg->num_cb * (int64_t)elbg->utility[idx[0]] < elbg->error) { |
| 354 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5872777 times.
|
5872777 | if (elbg->utility_inc[elbg->num_cb - 1] == 0) |
| 355 | ✗ | return; | |
| 356 | |||
| 357 | 5872777 | idx[1] = get_high_utility_cell(elbg); | |
| 358 | 5872777 | idx[2] = get_closest_codebook(elbg, idx[0]); | |
| 359 | |||
| 360 |
3/4✓ Branch 0 taken 5872777 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1442458 times.
✓ Branch 3 taken 4430319 times.
|
5872777 | if (idx[1] != idx[0] && idx[1] != idx[2]) |
| 361 | 1442458 | try_shift_candidate(elbg, idx); | |
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | 5790190 | static void do_elbg(ELBGContext *restrict elbg, int *points, int numpoints, | |
| 366 | int max_steps) | ||
| 367 | { | ||
| 368 | 5790190 | int *const size_part = elbg->size_part; | |
| 369 | 5790190 | int i, j, steps = 0; | |
| 370 | 5790190 | int best_idx = 0; | |
| 371 | int last_error; | ||
| 372 | |||
| 373 | 5790190 | elbg->error = INT_MAX; | |
| 374 | 5790190 | elbg->points = points; | |
| 375 | |||
| 376 | do { | ||
| 377 | 5827940 | cell *free_cells = elbg->cell_buffer; | |
| 378 | 5827940 | last_error = elbg->error; | |
| 379 | 5827940 | steps++; | |
| 380 | 5827940 | memset(elbg->utility, 0, elbg->num_cb * sizeof(*elbg->utility)); | |
| 381 | 5827940 | memset(elbg->cells, 0, elbg->num_cb * sizeof(*elbg->cells)); | |
| 382 | |||
| 383 | 5827940 | elbg->error = 0; | |
| 384 | |||
| 385 | /* This loop evaluate the actual Voronoi partition. It is the most | ||
| 386 | costly part of the algorithm. */ | ||
| 387 |
2/2✓ Branch 0 taken 76741726 times.
✓ Branch 1 taken 5827940 times.
|
82569666 | for (i=0; i < numpoints; i++) { |
| 388 | 76741726 | int best_dist = distance_limited(elbg->points + i * elbg->dim, | |
| 389 | 76741726 | elbg->codebook + best_idx * elbg->dim, | |
| 390 | elbg->dim, INT_MAX); | ||
| 391 |
2/2✓ Branch 0 taken 2048030570 times.
✓ Branch 1 taken 76741726 times.
|
2124772296 | for (int k = 0; k < elbg->num_cb; k++) { |
| 392 | 2048030570 | int dist = distance_limited(elbg->points + i * elbg->dim, | |
| 393 | 2048030570 | elbg->codebook + k * elbg->dim, | |
| 394 | elbg->dim, best_dist); | ||
| 395 |
2/2✓ Branch 0 taken 65145985 times.
✓ Branch 1 taken 1982884585 times.
|
2048030570 | if (dist < best_dist) { |
| 396 | 65145985 | best_dist = dist; | |
| 397 | 65145985 | best_idx = k; | |
| 398 | } | ||
| 399 | } | ||
| 400 | 76741726 | elbg->nearest_cb[i] = best_idx; | |
| 401 |
1/2✓ Branch 0 taken 76741726 times.
✗ Branch 1 not taken.
|
76741726 | elbg->error = (elbg->error >= INT_MAX - best_dist) ? INT_MAX : elbg->error + best_dist; |
| 402 | 153483452 | elbg->utility[elbg->nearest_cb[i]] = (elbg->utility[elbg->nearest_cb[i]] >= INT_MAX - best_dist) ? | |
| 403 |
1/2✓ Branch 0 taken 76741726 times.
✗ Branch 1 not taken.
|
76741726 | INT_MAX : elbg->utility[elbg->nearest_cb[i]] + best_dist; |
| 404 | 76741726 | free_cells->index = i; | |
| 405 | 76741726 | free_cells->next = elbg->cells[elbg->nearest_cb[i]]; | |
| 406 | 76741726 | elbg->cells[elbg->nearest_cb[i]] = free_cells; | |
| 407 | 76741726 | free_cells++; | |
| 408 | } | ||
| 409 | |||
| 410 | 5827940 | do_shiftings(elbg); | |
| 411 | |||
| 412 | 5827940 | memset(size_part, 0, elbg->num_cb * sizeof(*size_part)); | |
| 413 | |||
| 414 | 5827940 | memset(elbg->codebook, 0, elbg->num_cb * elbg->dim * sizeof(*elbg->codebook)); | |
| 415 | |||
| 416 |
2/2✓ Branch 0 taken 76741726 times.
✓ Branch 1 taken 5827940 times.
|
82569666 | for (i=0; i < numpoints; i++) { |
| 417 | 76741726 | size_part[elbg->nearest_cb[i]]++; | |
| 418 |
2/2✓ Branch 0 taken 327942996 times.
✓ Branch 1 taken 76741726 times.
|
404684722 | for (j=0; j < elbg->dim; j++) |
| 419 | 327942996 | elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] += | |
| 420 | 327942996 | elbg->points[i*elbg->dim + j]; | |
| 421 | } | ||
| 422 | |||
| 423 |
2/2✓ Branch 0 taken 13390518 times.
✓ Branch 1 taken 5827940 times.
|
19218458 | for (int i = 0; i < elbg->num_cb; i++) |
| 424 | 13390518 | vect_division(elbg->codebook + i*elbg->dim, | |
| 425 | 13390518 | elbg->codebook + i*elbg->dim, size_part[i], elbg->dim); | |
| 426 | |||
| 427 |
4/4✓ Branch 0 taken 5815244 times.
✓ Branch 1 taken 12696 times.
✓ Branch 2 taken 37750 times.
✓ Branch 3 taken 5777494 times.
|
5827940 | } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) && |
| 428 | (steps < max_steps)); | ||
| 429 | 5790190 | } | |
| 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 | */ | ||
| 439 | 5790190 | static void init_elbg(ELBGContext *restrict elbg, int *points, int *temp_points, | |
| 440 | int numpoints, int max_steps) | ||
| 441 | { | ||
| 442 | 5790190 | int dim = elbg->dim; | |
| 443 | |||
| 444 |
2/2✓ Branch 0 taken 30673 times.
✓ Branch 1 taken 5759517 times.
|
5790190 | 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 |
2/2✓ Branch 0 taken 1934047 times.
✓ Branch 1 taken 30673 times.
|
1964720 | for (int i = 0; i < numpoints / 8; i++) { |
| 448 | 1934047 | int k = (i*BIG_PRIME) % numpoints; | |
| 449 | 1934047 | 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 | 30673 | init_elbg(elbg, temp_points, temp_points + numpoints / 8 * dim, | |
| 455 | numpoints / 8, 2 * max_steps); | ||
| 456 | 30673 | do_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps); | |
| 457 | } else // If not, initialize the codebook with random positions | ||
| 458 |
2/2✓ Branch 0 taken 13040848 times.
✓ Branch 1 taken 5759517 times.
|
18800365 | for (int i = 0; i < elbg->num_cb; i++) |
| 459 | 13040848 | memcpy(elbg->codebook + i * dim, points + ((i*BIG_PRIME)%numpoints)*dim, | |
| 460 | dim * sizeof(*elbg->codebook)); | ||
| 461 | 5790190 | } | |
| 462 | |||
| 463 | 5759517 | int avpriv_elbg_do(ELBGContext **elbgp, int *points, int dim, int numpoints, | |
| 464 | int *codebook, int num_cb, int max_steps, | ||
| 465 | int *closest_cb, AVLFG *rand_state, uintptr_t flags) | ||
| 466 | { | ||
| 467 |
2/2✓ Branch 0 taken 5759507 times.
✓ Branch 1 taken 10 times.
|
5759517 | ELBGContext *const restrict elbg = *elbgp ? *elbgp : av_mallocz(sizeof(*elbg)); |
| 468 | |||
| 469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5759517 times.
|
5759517 | if (!elbg) |
| 470 | ✗ | return AVERROR(ENOMEM); | |
| 471 | 5759517 | *elbgp = elbg; | |
| 472 | |||
| 473 | 5759517 | elbg->nearest_cb = closest_cb; | |
| 474 | 5759517 | elbg->rand_state = rand_state; | |
| 475 | 5759517 | elbg->codebook = codebook; | |
| 476 | 5759517 | elbg->num_cb = num_cb; | |
| 477 | 5759517 | elbg->dim = dim; | |
| 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. */ | ||
| 494 |
3/4✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5759488 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
|
5759517 | ALLOCATE_IF_NECESSARY(cells, num_cb, 1) |
| 495 |
3/4✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5759488 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
|
5759517 | ALLOCATE_IF_NECESSARY(utility, num_cb, 1) |
| 496 |
3/4✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5759488 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
|
5759517 | ALLOCATE_IF_NECESSARY(utility_inc, num_cb, 1) |
| 497 |
3/4✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5759488 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
|
5759517 | ALLOCATE_IF_NECESSARY(size_part, num_cb, 1) |
| 498 |
3/4✓ Branch 0 taken 17 times.
✓ Branch 1 taken 5759500 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 17 times.
|
5759517 | ALLOCATE_IF_NECESSARY(cell_buffer, numpoints, 1) |
| 499 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5759507 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
|
5759517 | ALLOCATE_IF_NECESSARY(scratchbuf, dim, 5) |
| 500 |
2/2✓ Branch 0 taken 23325 times.
✓ Branch 1 taken 5736192 times.
|
5759517 | 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 | 23325 | uint64_t prod = dim * (uint64_t)(numpoints / 7U); | |
| 506 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23325 times.
|
23325 | if (prod > INT_MAX) |
| 507 | ✗ | return AVERROR(ERANGE); | |
| 508 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 23314 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
|
23325 | ALLOCATE_IF_NECESSARY(temp_points, prod, 1) |
| 509 | } | ||
| 510 | |||
| 511 | 5759517 | init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps); | |
| 512 | 5759517 | do_elbg (elbg, points, numpoints, max_steps); | |
| 513 | 5759517 | return 0; | |
| 514 | } | ||
| 515 | |||
| 516 | 10 | av_cold void avpriv_elbg_free(ELBGContext **elbgp) | |
| 517 | { | ||
| 518 | 10 | ELBGContext *elbg = *elbgp; | |
| 519 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!elbg) |
| 520 | ✗ | return; | |
| 521 | |||
| 522 | 10 | av_freep(&elbg->size_part); | |
| 523 | 10 | av_freep(&elbg->utility); | |
| 524 | 10 | av_freep(&elbg->cell_buffer); | |
| 525 | 10 | av_freep(&elbg->cells); | |
| 526 | 10 | av_freep(&elbg->utility_inc); | |
| 527 | 10 | av_freep(&elbg->scratchbuf); | |
| 528 | 10 | av_freep(&elbg->temp_points); | |
| 529 | |||
| 530 | 10 | av_freep(elbgp); | |
| 531 | } | ||
| 532 |