FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/elbg.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 235 244 96.3%
Functions: 16 16 100.0%
Branches: 123 144 85.4%

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 "elbg.h"
32
33 #define DELTA_ERR_MAX 0.1 ///< Precision of the ELBG algorithm (as percentage error)
34
35 /**
36 * In the ELBG jargon, a cell is the set of points that are closest to a
37 * codebook entry. Not to be confused with a RoQ Video cell. */
38 typedef struct cell_s {
39 int index;
40 struct cell_s *next;
41 } cell;
42
43 /**
44 * ELBG internal data
45 */
46 typedef struct ELBGContext {
47 int error;
48 int dim;
49 int num_cb;
50 int *codebook;
51 cell **cells;
52 int *utility;
53 int *utility_inc;
54 int *nearest_cb;
55 int *points;
56 int *temp_points;
57 int *size_part;
58 AVLFG *rand_state;
59 int *scratchbuf;
60 cell *cell_buffer;
61
62 /* Sizes for the buffers above. Pointers without such a field
63 * are not allocated by us and only valid for the duration
64 * of a single call to avpriv_elbg_do(). */
65 unsigned utility_allocated;
66 unsigned utility_inc_allocated;
67 unsigned size_part_allocated;
68 unsigned cells_allocated;
69 unsigned scratchbuf_allocated;
70 unsigned cell_buffer_allocated;
71 unsigned temp_points_allocated;
72 } ELBGContext;
73
74 2466940895 static inline int distance_limited(int *a, int *b, int dim, int limit)
75 {
76 2466940895 int i, dist=0;
77
2/2
✓ Branch 0 taken 4854074942 times.
✓ Branch 1 taken 274545884 times.
5128620826 for (i=0; i<dim; i++) {
78 4854074942 int64_t distance = a[i] - b[i];
79
80 4854074942 distance *= distance;
81
2/2
✓ Branch 0 taken 2192395011 times.
✓ Branch 1 taken 2661679931 times.
4854074942 if (dist >= limit - distance)
82 2192395011 return limit;
83 2661679931 dist += distance;
84 }
85
86 274545884 return dist;
87 }
88
89 17717679 static inline void vect_division(int *res, int *vect, int div, int dim)
90 {
91 int i;
92
2/2
✓ Branch 0 taken 12934060 times.
✓ Branch 1 taken 4783619 times.
17717679 if (div > 1)
93
2/2
✓ Branch 0 taken 55987062 times.
✓ Branch 1 taken 12934060 times.
68921122 for (i=0; i<dim; i++)
94
1/2
✓ Branch 0 taken 55987062 times.
✗ Branch 1 not taken.
55987062 res[i] = ROUNDED_DIV(vect[i],div);
95
2/2
✓ Branch 0 taken 733096 times.
✓ Branch 1 taken 4050523 times.
4783619 else if (res != vect)
96 733096 memcpy(res, vect, dim*sizeof(int));
97
98 17717679 }
99
100 2884774 static int eval_error_cell(ELBGContext *elbg, int *centroid, cell *cells)
101 {
102 2884774 int error=0;
103
2/2
✓ Branch 0 taken 25169204 times.
✓ Branch 1 taken 2884774 times.
28053978 for (; cells; cells=cells->next) {
104 25169204 int distance = distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25169204 times.
25169204 if (error >= INT_MAX - distance)
106 return INT_MAX;
107 25169204 error += distance;
108 }
109
110 2884774 return error;
111 }
112
113 5872699 static int get_closest_codebook(ELBGContext *elbg, int index)
114 {
115 5872699 int pick = 0;
116
2/2
✓ Branch 0 taken 227277060 times.
✓ Branch 1 taken 5872699 times.
233149759 for (int i = 0, diff_min = INT_MAX; i < elbg->num_cb; i++)
117
2/2
✓ Branch 0 taken 221404361 times.
✓ Branch 1 taken 5872699 times.
227277060 if (i != index) {
118 int diff;
119 221404361 diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min);
120
2/2
✓ Branch 0 taken 11934164 times.
✓ Branch 1 taken 209470197 times.
221404361 if (diff < diff_min) {
121 11934164 pick = i;
122 11934164 diff_min = diff;
123 }
124 }
125 5872699 return pick;
126 }
127
128 5872699 static int get_high_utility_cell(ELBGContext *elbg)
129 {
130 5872699 int i=0;
131 /* Using linear search, do binary if it ever turns to be speed critical */
132 uint64_t r;
133
134
1/2
✓ Branch 0 taken 5872699 times.
✗ Branch 1 not taken.
5872699 if (elbg->utility_inc[elbg->num_cb - 1] < INT_MAX) {
135 5872699 r = av_lfg_get(elbg->rand_state) % (unsigned int)elbg->utility_inc[elbg->num_cb - 1] + 1;
136 } else {
137 r = av_lfg_get(elbg->rand_state);
138 r = (av_lfg_get(elbg->rand_state) + (r<<32)) % elbg->utility_inc[elbg->num_cb - 1] + 1;
139 }
140
141
2/2
✓ Branch 0 taken 109370285 times.
✓ Branch 1 taken 5872699 times.
115242984 while (elbg->utility_inc[i] < r) {
142 109370285 i++;
143 }
144
145 av_assert2(elbg->cells[i]);
146
147 5872699 return i;
148 }
149
150 /**
151 * Implementation of the simple LBG algorithm for just two codebooks
152 */
153 1442387 static int simple_lbg(ELBGContext *elbg,
154 int dim,
155 int *centroid[3],
156 int newutility[3],
157 int *points,
158 cell *cells)
159 {
160 int i, idx;
161 1442387 int numpoints[2] = {0,0};
162 1442387 int *newcentroid[2] = {
163 1442387 elbg->scratchbuf + 3*dim,
164 1442387 elbg->scratchbuf + 4*dim
165 };
166 cell *tempcell;
167
168 1442387 memset(newcentroid[0], 0, 2 * dim * sizeof(*newcentroid[0]));
169
170 1442387 newutility[0] =
171 1442387 newutility[1] = 0;
172
173
2/2
✓ Branch 0 taken 19693397 times.
✓ Branch 1 taken 1442387 times.
21135784 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
174 19693397 idx = distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX)>=
175 19693397 distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX);
176 19693397 numpoints[idx]++;
177
2/2
✓ Branch 0 taken 121291626 times.
✓ Branch 1 taken 19693397 times.
140985023 for (i=0; i<dim; i++)
178 121291626 newcentroid[idx][i] += points[tempcell->index*dim + i];
179 }
180
181 1442387 vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
182 1442387 vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
183
184
2/2
✓ Branch 0 taken 19693397 times.
✓ Branch 1 taken 1442387 times.
21135784 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
185 19693397 int dist[2] = {distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX),
186 19693397 distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX)};
187 19693397 int idx = dist[0] > dist[1];
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19693397 times.
19693397 if (newutility[idx] >= INT_MAX - dist[idx])
189 newutility[idx] = INT_MAX;
190 else
191 19693397 newutility[idx] += dist[idx];
192 }
193
194
1/2
✓ Branch 0 taken 1442387 times.
✗ Branch 1 not taken.
1442387 return (newutility[0] >= INT_MAX - newutility[1]) ? INT_MAX : newutility[0] + newutility[1];
195 }
196
197 1442387 static void get_new_centroids(ELBGContext *elbg, int huc, int *newcentroid_i,
198 int *newcentroid_p)
199 {
200 cell *tempcell;
201 1442387 int *min = newcentroid_i;
202 1442387 int *max = newcentroid_p;
203 int i;
204
205
2/2
✓ Branch 0 taken 8844582 times.
✓ Branch 1 taken 1442387 times.
10286969 for (i=0; i< elbg->dim; i++) {
206 8844582 min[i]=INT_MAX;
207 8844582 max[i]=0;
208 }
209
210
2/2
✓ Branch 0 taken 19693397 times.
✓ Branch 1 taken 1442387 times.
21135784 for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
211
2/2
✓ Branch 0 taken 121291626 times.
✓ Branch 1 taken 19693397 times.
140985023 for(i=0; i<elbg->dim; i++) {
212 121291626 min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
213 121291626 max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
214 }
215
216
2/2
✓ Branch 0 taken 8844582 times.
✓ Branch 1 taken 1442387 times.
10286969 for (i=0; i<elbg->dim; i++) {
217 8844582 int ni = min[i] + (max[i] - min[i])/3;
218 8844582 int np = min[i] + (2*(max[i] - min[i]))/3;
219 8844582 newcentroid_i[i] = ni;
220 8844582 newcentroid_p[i] = np;
221 }
222 1442387 }
223
224 /**
225 * Add the points in the low utility cell to its closest cell. Split the high
226 * utility cell, putting the separated points in the (now empty) low utility
227 * cell.
228 *
229 * @param elbg Internal elbg data
230 * @param indexes {luc, huc, cluc}
231 * @param newcentroid A vector with the position of the new centroids
232 */
233 740259 static void shift_codebook(ELBGContext *elbg, int *indexes,
234 int *newcentroid[3])
235 {
236 cell *tempdata;
237 740259 cell **pp = &elbg->cells[indexes[2]];
238
239
2/2
✓ Branch 0 taken 5906853 times.
✓ Branch 1 taken 740259 times.
6647112 while(*pp)
240 5906853 pp= &(*pp)->next;
241
242 740259 *pp = elbg->cells[indexes[0]];
243
244 740259 elbg->cells[indexes[0]] = NULL;
245 740259 tempdata = elbg->cells[indexes[1]];
246 740259 elbg->cells[indexes[1]] = NULL;
247
248
2/2
✓ Branch 0 taken 8410723 times.
✓ Branch 1 taken 740259 times.
9150982 while(tempdata) {
249 8410723 cell *tempcell2 = tempdata->next;
250 8410723 int idx = distance_limited(elbg->points + tempdata->index*elbg->dim,
251 8410723 newcentroid[0], elbg->dim, INT_MAX) >
252 8410723 distance_limited(elbg->points + tempdata->index*elbg->dim,
253 8410723 newcentroid[1], elbg->dim, INT_MAX);
254
255 8410723 tempdata->next = elbg->cells[indexes[idx]];
256 8410723 elbg->cells[indexes[idx]] = tempdata;
257 8410723 tempdata = tempcell2;
258 }
259 740259 }
260
261 6568199 static void evaluate_utility_inc(ELBGContext *elbg)
262 {
263 6568199 int64_t inc=0;
264
265
2/2
✓ Branch 0 taken 128838782 times.
✓ Branch 1 taken 6568199 times.
135406981 for (int i = 0; i < elbg->num_cb; i++) {
266
2/2
✓ Branch 0 taken 44577128 times.
✓ Branch 1 taken 84261654 times.
128838782 if (elbg->num_cb * (int64_t)elbg->utility[i] > elbg->error)
267 44577128 inc += elbg->utility[i];
268 128838782 elbg->utility_inc[i] = FFMIN(inc, INT_MAX);
269 }
270 6568199 }
271
272
273 2220777 static void update_utility_and_n_cb(ELBGContext *elbg, int idx, int newutility)
274 {
275 cell *tempcell;
276
277 2220777 elbg->utility[idx] = newutility;
278
2/2
✓ Branch 0 taken 18424544 times.
✓ Branch 1 taken 2220777 times.
20645321 for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next)
279 18424544 elbg->nearest_cb[tempcell->index] = idx;
280 2220777 }
281
282 /**
283 * Evaluate if a shift lower the error. If it does, call shift_codebooks
284 * and update elbg->error, elbg->utility and elbg->nearest_cb.
285 *
286 * @param elbg Internal elbg data
287 * @param idx {luc (low utility cell, huc (high utility cell), cluc (closest cell to low utility cell)}
288 */
289 1442387 static void try_shift_candidate(ELBGContext *elbg, int idx[3])
290 {
291 1442387 int j, k, cont=0, tmp;
292 1442387 int64_t olderror=0, newerror;
293 int newutility[3];
294 1442387 int *newcentroid[3] = {
295 1442387 elbg->scratchbuf,
296 1442387 elbg->scratchbuf + elbg->dim,
297 1442387 elbg->scratchbuf + 2*elbg->dim
298 };
299 cell *tempcell;
300
301
2/2
✓ Branch 0 taken 4327161 times.
✓ Branch 1 taken 1442387 times.
5769548 for (j=0; j<3; j++)
302 4327161 olderror += elbg->utility[idx[j]];
303
304 1442387 memset(newcentroid[2], 0, elbg->dim*sizeof(int));
305
306
2/2
✓ Branch 0 taken 2884774 times.
✓ Branch 1 taken 1442387 times.
4327161 for (k=0; k<2; k++)
307
2/2
✓ Branch 0 taken 25169204 times.
✓ Branch 1 taken 2884774 times.
28053978 for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
308 25169204 cont++;
309
2/2
✓ Branch 0 taken 158109438 times.
✓ Branch 1 taken 25169204 times.
183278642 for (j=0; j<elbg->dim; j++)
310 158109438 newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j];
311 }
312
313 1442387 vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim);
314
315 1442387 get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]);
316
317 1442387 newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]);
318 1442387 tmp = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]);
319
1/2
✓ Branch 0 taken 1442387 times.
✗ Branch 1 not taken.
1442387 newutility[2] = (tmp >= INT_MAX - newutility[2]) ? INT_MAX : newutility[2] + tmp;
320
321 1442387 newerror = newutility[2];
322
323 1442387 tmp = simple_lbg(elbg, elbg->dim, newcentroid, newutility, elbg->points,
324 1442387 elbg->cells[idx[1]]);
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1442387 times.
1442387 if (tmp >= INT_MAX - newerror)
326 newerror = INT_MAX;
327 else
328 1442387 newerror += tmp;
329
330
2/2
✓ Branch 0 taken 740259 times.
✓ Branch 1 taken 702128 times.
1442387 if (olderror > newerror) {
331 740259 shift_codebook(elbg, idx, newcentroid);
332
333 740259 elbg->error += newerror - olderror;
334
335
2/2
✓ Branch 0 taken 2220777 times.
✓ Branch 1 taken 740259 times.
2961036 for (j=0; j<3; j++)
336 2220777 update_utility_and_n_cb(elbg, idx[j], newutility[j]);
337
338 740259 evaluate_utility_inc(elbg);
339 }
340 1442387 }
341
342 /**
343 * Implementation of the ELBG block
344 */
345 5827940 static void do_shiftings(ELBGContext *elbg)
346 {
347 int idx[3];
348
349 5827940 evaluate_utility_inc(elbg);
350
351
2/2
✓ Branch 0 taken 13390518 times.
✓ Branch 1 taken 5827940 times.
19218458 for (idx[0]=0; idx[0] < elbg->num_cb; idx[0]++)
352
2/2
✓ Branch 0 taken 5872699 times.
✓ Branch 1 taken 7517819 times.
13390518 if (elbg->num_cb * (int64_t)elbg->utility[idx[0]] < elbg->error) {
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5872699 times.
5872699 if (elbg->utility_inc[elbg->num_cb - 1] == 0)
354 return;
355
356 5872699 idx[1] = get_high_utility_cell(elbg);
357 5872699 idx[2] = get_closest_codebook(elbg, idx[0]);
358
359
3/4
✓ Branch 0 taken 5872699 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1442387 times.
✓ Branch 3 taken 4430312 times.
5872699 if (idx[1] != idx[0] && idx[1] != idx[2])
360 1442387 try_shift_candidate(elbg, idx);
361 }
362 }
363
364 5790190 static void do_elbg(ELBGContext *av_restrict elbg, int *points, int numpoints,
365 int max_steps)
366 {
367 5790190 int *const size_part = elbg->size_part;
368 5790190 int i, j, steps = 0;
369 5790190 int best_idx = 0;
370 int last_error;
371
372 5790190 elbg->error = INT_MAX;
373 5790190 elbg->points = points;
374
375 do {
376 5827940 cell *free_cells = elbg->cell_buffer;
377 5827940 last_error = elbg->error;
378 5827940 steps++;
379 5827940 memset(elbg->utility, 0, elbg->num_cb * sizeof(*elbg->utility));
380 5827940 memset(elbg->cells, 0, elbg->num_cb * sizeof(*elbg->cells));
381
382 5827940 elbg->error = 0;
383
384 /* This loop evaluate the actual Voronoi partition. It is the most
385 costly part of the algorithm. */
386
2/2
✓ Branch 0 taken 76741726 times.
✓ Branch 1 taken 5827940 times.
82569666 for (i=0; i < numpoints; i++) {
387 76741726 int best_dist = distance_limited(elbg->points + i * elbg->dim,
388 76741726 elbg->codebook + best_idx * elbg->dim,
389 elbg->dim, INT_MAX);
390
2/2
✓ Branch 0 taken 2048030570 times.
✓ Branch 1 taken 76741726 times.
2124772296 for (int k = 0; k < elbg->num_cb; k++) {
391 2048030570 int dist = distance_limited(elbg->points + i * elbg->dim,
392 2048030570 elbg->codebook + k * elbg->dim,
393 elbg->dim, best_dist);
394
2/2
✓ Branch 0 taken 65105756 times.
✓ Branch 1 taken 1982924814 times.
2048030570 if (dist < best_dist) {
395 65105756 best_dist = dist;
396 65105756 best_idx = k;
397 }
398 }
399 76741726 elbg->nearest_cb[i] = best_idx;
400
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;
401 153483452 elbg->utility[elbg->nearest_cb[i]] = (elbg->utility[elbg->nearest_cb[i]] >= INT_MAX - best_dist) ?
402
1/2
✓ Branch 0 taken 76741726 times.
✗ Branch 1 not taken.
76741726 INT_MAX : elbg->utility[elbg->nearest_cb[i]] + best_dist;
403 76741726 free_cells->index = i;
404 76741726 free_cells->next = elbg->cells[elbg->nearest_cb[i]];
405 76741726 elbg->cells[elbg->nearest_cb[i]] = free_cells;
406 76741726 free_cells++;
407 }
408
409 5827940 do_shiftings(elbg);
410
411 5827940 memset(size_part, 0, elbg->num_cb * sizeof(*size_part));
412
413 5827940 memset(elbg->codebook, 0, elbg->num_cb * elbg->dim * sizeof(*elbg->codebook));
414
415
2/2
✓ Branch 0 taken 76741726 times.
✓ Branch 1 taken 5827940 times.
82569666 for (i=0; i < numpoints; i++) {
416 76741726 size_part[elbg->nearest_cb[i]]++;
417
2/2
✓ Branch 0 taken 327942996 times.
✓ Branch 1 taken 76741726 times.
404684722 for (j=0; j < elbg->dim; j++)
418 327942996 elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] +=
419 327942996 elbg->points[i*elbg->dim + j];
420 }
421
422
2/2
✓ Branch 0 taken 13390518 times.
✓ Branch 1 taken 5827940 times.
19218458 for (int i = 0; i < elbg->num_cb; i++)
423 13390518 vect_division(elbg->codebook + i*elbg->dim,
424 13390518 elbg->codebook + i*elbg->dim, size_part[i], elbg->dim);
425
426
4/4
✓ Branch 0 taken 5815243 times.
✓ Branch 1 taken 12697 times.
✓ Branch 2 taken 37750 times.
✓ Branch 3 taken 5777493 times.
5827940 } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) &&
427 (steps < max_steps));
428 5790190 }
429
430 #define BIG_PRIME 433494437LL
431
432 /**
433 * Initialize the codebook vector for the elbg algorithm.
434 * If numpoints <= 24 * num_cb this function fills codebook with random numbers.
435 * If not, it calls do_elbg for a (smaller) random sample of the points in
436 * points.
437 */
438 5790190 static void init_elbg(ELBGContext *av_restrict elbg, int *points, int *temp_points,
439 int numpoints, int max_steps)
440 {
441 5790190 int dim = elbg->dim;
442
443
2/2
✓ Branch 0 taken 30673 times.
✓ Branch 1 taken 5759517 times.
5790190 if (numpoints > 24LL * elbg->num_cb) {
444 /* ELBG is very costly for a big number of points. So if we have a lot
445 of them, get a good initial codebook to save on iterations */
446
2/2
✓ Branch 0 taken 1934047 times.
✓ Branch 1 taken 30673 times.
1964720 for (int i = 0; i < numpoints / 8; i++) {
447 1934047 int k = (i*BIG_PRIME) % numpoints;
448 1934047 memcpy(temp_points + i*dim, points + k*dim, dim * sizeof(*temp_points));
449 }
450
451 /* If anything is changed in the recursion parameters,
452 * the allocated size of temp_points will also need to be updated. */
453 30673 init_elbg(elbg, temp_points, temp_points + numpoints / 8 * dim,
454 numpoints / 8, 2 * max_steps);
455 30673 do_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps);
456 } else // If not, initialize the codebook with random positions
457
2/2
✓ Branch 0 taken 13040848 times.
✓ Branch 1 taken 5759517 times.
18800365 for (int i = 0; i < elbg->num_cb; i++)
458 13040848 memcpy(elbg->codebook + i * dim, points + ((i*BIG_PRIME)%numpoints)*dim,
459 dim * sizeof(*elbg->codebook));
460 5790190 }
461
462 5759517 int avpriv_elbg_do(ELBGContext **elbgp, int *points, int dim, int numpoints,
463 int *codebook, int num_cb, int max_steps,
464 int *closest_cb, AVLFG *rand_state, uintptr_t flags)
465 {
466
2/2
✓ Branch 0 taken 5759507 times.
✓ Branch 1 taken 10 times.
5759517 ELBGContext *const av_restrict elbg = *elbgp ? *elbgp : av_mallocz(sizeof(*elbg));
467
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5759517 times.
5759517 if (!elbg)
469 return AVERROR(ENOMEM);
470 5759517 *elbgp = elbg;
471
472 5759517 elbg->nearest_cb = closest_cb;
473 5759517 elbg->rand_state = rand_state;
474 5759517 elbg->codebook = codebook;
475 5759517 elbg->num_cb = num_cb;
476 5759517 elbg->dim = dim;
477
478 #define ALLOCATE_IF_NECESSARY(field, new_elements, multiplicator) \
479 if (elbg->field ## _allocated < new_elements) { \
480 av_freep(&elbg->field); \
481 elbg->field = av_malloc_array(new_elements, \
482 multiplicator * sizeof(*elbg->field)); \
483 if (!elbg->field) { \
484 elbg->field ## _allocated = 0; \
485 return AVERROR(ENOMEM); \
486 } \
487 elbg->field ## _allocated = new_elements; \
488 }
489 /* Allocating the buffers for do_elbg() here once relies
490 * on their size being always the same even when do_elbg()
491 * is called from init_elbg(). It also relies on do_elbg()
492 * never calling itself recursively. */
493
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)
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(utility, 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_inc, 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(size_part, num_cb, 1)
497
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)
498
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)
499
2/2
✓ Branch 0 taken 23325 times.
✓ Branch 1 taken 5736192 times.
5759517 if (numpoints > 24LL * elbg->num_cb) {
500 /* The first step in the recursion in init_elbg() needs a buffer with
501 * (numpoints / 8) * dim elements; the next step needs numpoints / 8 / 8
502 * * dim elements etc. The geometric series leads to an upper bound of
503 * numpoints / 8 * 8 / 7 * dim elements. */
504 23325 uint64_t prod = dim * (uint64_t)(numpoints / 7U);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23325 times.
23325 if (prod > INT_MAX)
506 return AVERROR(ERANGE);
507
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)
508 }
509
510 5759517 init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps);
511 5759517 do_elbg (elbg, points, numpoints, max_steps);
512 5759517 return 0;
513 }
514
515 10 av_cold void avpriv_elbg_free(ELBGContext **elbgp)
516 {
517 10 ELBGContext *elbg = *elbgp;
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!elbg)
519 return;
520
521 10 av_freep(&elbg->size_part);
522 10 av_freep(&elbg->utility);
523 10 av_freep(&elbg->cell_buffer);
524 10 av_freep(&elbg->cells);
525 10 av_freep(&elbg->utility_inc);
526 10 av_freep(&elbg->scratchbuf);
527 10 av_freep(&elbg->temp_points);
528
529 10 av_freep(elbgp);
530 }
531