FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/elbg.c
Date: 2023-03-31 03:41:15
Exec Total Coverage
Lines: 226 232 97.4%
Functions: 16 16 100.0%
Branches: 116 130 89.2%

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 int64_t error;
48 int dim;
49 int num_cb;
50 int *codebook;
51 cell **cells;
52 int64_t *utility;
53 int64_t *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 2467212951 static inline int distance_limited(int *a, int *b, int dim, int limit)
75 {
76 2467212951 int i, dist=0;
77
2/2
✓ Branch 0 taken 4943956811 times.
✓ Branch 1 taken 338587126 times.
5282543937 for (i=0; i<dim; i++) {
78 4943956811 dist += (a[i] - b[i])*(a[i] - b[i]);
79
2/2
✓ Branch 0 taken 2128625825 times.
✓ Branch 1 taken 2815330986 times.
4943956811 if (dist > limit)
80 2128625825 return INT_MAX;
81 }
82
83 338587126 return dist;
84 }
85
86 17717628 static inline void vect_division(int *res, int *vect, int div, int dim)
87 {
88 int i;
89
2/2
✓ Branch 0 taken 12933807 times.
✓ Branch 1 taken 4783821 times.
17717628 if (div > 1)
90
2/2
✓ Branch 0 taken 55984752 times.
✓ Branch 1 taken 12933807 times.
68918559 for (i=0; i<dim; i++)
91
1/2
✓ Branch 0 taken 55984752 times.
✗ Branch 1 not taken.
55984752 res[i] = ROUNDED_DIV(vect[i],div);
92
2/2
✓ Branch 0 taken 733234 times.
✓ Branch 1 taken 4050587 times.
4783821 else if (res != vect)
93 733234 memcpy(res, vect, dim*sizeof(int));
94
95 17717628 }
96
97 2884740 static int eval_error_cell(ELBGContext *elbg, int *centroid, cell *cells)
98 {
99 2884740 int error=0;
100
2/2
✓ Branch 0 taken 25103264 times.
✓ Branch 1 taken 2884740 times.
27988004 for (; cells; cells=cells->next)
101 25103264 error += distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX);
102
103 2884740 return error;
104 }
105
106 5872679 static int get_closest_codebook(ELBGContext *elbg, int index)
107 {
108 5872679 int pick = 0;
109
2/2
✓ Branch 0 taken 227271958 times.
✓ Branch 1 taken 5872679 times.
233144637 for (int i = 0, diff_min = INT_MAX; i < elbg->num_cb; i++)
110
2/2
✓ Branch 0 taken 221399279 times.
✓ Branch 1 taken 5872679 times.
227271958 if (i != index) {
111 int diff;
112 221399279 diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min);
113
2/2
✓ Branch 0 taken 11935071 times.
✓ Branch 1 taken 209464208 times.
221399279 if (diff < diff_min) {
114 11935071 pick = i;
115 11935071 diff_min = diff;
116 }
117 }
118 5872679 return pick;
119 }
120
121 5872679 static int get_high_utility_cell(ELBGContext *elbg)
122 {
123 5872679 int i=0;
124 /* Using linear search, do binary if it ever turns to be speed critical */
125 uint64_t r;
126
127
1/2
✓ Branch 0 taken 5872679 times.
✗ Branch 1 not taken.
5872679 if (elbg->utility_inc[elbg->num_cb - 1] < INT_MAX) {
128 5872679 r = av_lfg_get(elbg->rand_state) % (unsigned int)elbg->utility_inc[elbg->num_cb - 1] + 1;
129 } else {
130 r = av_lfg_get(elbg->rand_state);
131 r = (av_lfg_get(elbg->rand_state) + (r<<32)) % elbg->utility_inc[elbg->num_cb - 1] + 1;
132 }
133
134
2/2
✓ Branch 0 taken 109362161 times.
✓ Branch 1 taken 5872679 times.
115234840 while (elbg->utility_inc[i] < r) {
135 109362161 i++;
136 }
137
138 av_assert2(elbg->cells[i]);
139
140 5872679 return i;
141 }
142
143 /**
144 * Implementation of the simple LBG algorithm for just two codebooks
145 */
146 1442370 static int simple_lbg(ELBGContext *elbg,
147 int dim,
148 int *centroid[3],
149 int newutility[3],
150 int *points,
151 cell *cells)
152 {
153 int i, idx;
154 1442370 int numpoints[2] = {0,0};
155 1442370 int *newcentroid[2] = {
156 1442370 elbg->scratchbuf + 3*dim,
157 1442370 elbg->scratchbuf + 4*dim
158 };
159 cell *tempcell;
160
161 1442370 memset(newcentroid[0], 0, 2 * dim * sizeof(*newcentroid[0]));
162
163 1442370 newutility[0] =
164 1442370 newutility[1] = 0;
165
166
2/2
✓ Branch 0 taken 19778653 times.
✓ Branch 1 taken 1442370 times.
21221023 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
167 19778653 idx = distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX)>=
168 19778653 distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX);
169 19778653 numpoints[idx]++;
170
2/2
✓ Branch 0 taken 121787862 times.
✓ Branch 1 taken 19778653 times.
141566515 for (i=0; i<dim; i++)
171 121787862 newcentroid[idx][i] += points[tempcell->index*dim + i];
172 }
173
174 1442370 vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
175 1442370 vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
176
177
2/2
✓ Branch 0 taken 19778653 times.
✓ Branch 1 taken 1442370 times.
21221023 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
178 19778653 int dist[2] = {distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX),
179 19778653 distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX)};
180 19778653 int idx = dist[0] > dist[1];
181 19778653 newutility[idx] += dist[idx];
182 }
183
184 1442370 return newutility[0] + newutility[1];
185 }
186
187 1442370 static void get_new_centroids(ELBGContext *elbg, int huc, int *newcentroid_i,
188 int *newcentroid_p)
189 {
190 cell *tempcell;
191 1442370 int *min = newcentroid_i;
192 1442370 int *max = newcentroid_p;
193 int i;
194
195
2/2
✓ Branch 0 taken 8844102 times.
✓ Branch 1 taken 1442370 times.
10286472 for (i=0; i< elbg->dim; i++) {
196 8844102 min[i]=INT_MAX;
197 8844102 max[i]=0;
198 }
199
200
2/2
✓ Branch 0 taken 19778653 times.
✓ Branch 1 taken 1442370 times.
21221023 for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
201
2/2
✓ Branch 0 taken 121787862 times.
✓ Branch 1 taken 19778653 times.
141566515 for(i=0; i<elbg->dim; i++) {
202 121787862 min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
203 121787862 max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
204 }
205
206
2/2
✓ Branch 0 taken 8844102 times.
✓ Branch 1 taken 1442370 times.
10286472 for (i=0; i<elbg->dim; i++) {
207 8844102 int ni = min[i] + (max[i] - min[i])/3;
208 8844102 int np = min[i] + (2*(max[i] - min[i]))/3;
209 8844102 newcentroid_i[i] = ni;
210 8844102 newcentroid_p[i] = np;
211 }
212 1442370 }
213
214 /**
215 * Add the points in the low utility cell to its closest cell. Split the high
216 * utility cell, putting the separated points in the (now empty) low utility
217 * cell.
218 *
219 * @param elbg Internal elbg data
220 * @param indexes {luc, huc, cluc}
221 * @param newcentroid A vector with the position of the new centroids
222 */
223 740303 static void shift_codebook(ELBGContext *elbg, int *indexes,
224 int *newcentroid[3])
225 {
226 cell *tempdata;
227 740303 cell **pp = &elbg->cells[indexes[2]];
228
229
2/2
✓ Branch 0 taken 5923809 times.
✓ Branch 1 taken 740303 times.
6664112 while(*pp)
230 5923809 pp= &(*pp)->next;
231
232 740303 *pp = elbg->cells[indexes[0]];
233
234 740303 elbg->cells[indexes[0]] = NULL;
235 740303 tempdata = elbg->cells[indexes[1]];
236 740303 elbg->cells[indexes[1]] = NULL;
237
238
2/2
✓ Branch 0 taken 8411750 times.
✓ Branch 1 taken 740303 times.
9152053 while(tempdata) {
239 8411750 cell *tempcell2 = tempdata->next;
240 8411750 int idx = distance_limited(elbg->points + tempdata->index*elbg->dim,
241 8411750 newcentroid[0], elbg->dim, INT_MAX) >
242 8411750 distance_limited(elbg->points + tempdata->index*elbg->dim,
243 8411750 newcentroid[1], elbg->dim, INT_MAX);
244
245 8411750 tempdata->next = elbg->cells[indexes[idx]];
246 8411750 elbg->cells[indexes[idx]] = tempdata;
247 8411750 tempdata = tempcell2;
248 }
249 740303 }
250
251 6568243 static void evaluate_utility_inc(ELBGContext *elbg)
252 {
253 6568243 int64_t inc=0;
254
255
2/2
✓ Branch 0 taken 128850034 times.
✓ Branch 1 taken 6568243 times.
135418277 for (int i = 0; i < elbg->num_cb; i++) {
256
2/2
✓ Branch 0 taken 44581646 times.
✓ Branch 1 taken 84268388 times.
128850034 if (elbg->num_cb * elbg->utility[i] > elbg->error)
257 44581646 inc += elbg->utility[i];
258 128850034 elbg->utility_inc[i] = inc;
259 }
260 6568243 }
261
262
263 2220909 static void update_utility_and_n_cb(ELBGContext *elbg, int idx, int newutility)
264 {
265 cell *tempcell;
266
267 2220909 elbg->utility[idx] = newutility;
268
2/2
✓ Branch 0 taken 18442294 times.
✓ Branch 1 taken 2220909 times.
20663203 for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next)
269 18442294 elbg->nearest_cb[tempcell->index] = idx;
270 2220909 }
271
272 /**
273 * Evaluate if a shift lower the error. If it does, call shift_codebooks
274 * and update elbg->error, elbg->utility and elbg->nearest_cb.
275 *
276 * @param elbg Internal elbg data
277 * @param idx {luc (low utility cell, huc (high utility cell), cluc (closest cell to low utility cell)}
278 */
279 1442370 static void try_shift_candidate(ELBGContext *elbg, int idx[3])
280 {
281 1442370 int j, k, cont=0;
282 1442370 int64_t olderror=0, newerror;
283 int newutility[3];
284 1442370 int *newcentroid[3] = {
285 1442370 elbg->scratchbuf,
286 1442370 elbg->scratchbuf + elbg->dim,
287 1442370 elbg->scratchbuf + 2*elbg->dim
288 };
289 cell *tempcell;
290
291
2/2
✓ Branch 0 taken 4327110 times.
✓ Branch 1 taken 1442370 times.
5769480 for (j=0; j<3; j++)
292 4327110 olderror += elbg->utility[idx[j]];
293
294 1442370 memset(newcentroid[2], 0, elbg->dim*sizeof(int));
295
296
2/2
✓ Branch 0 taken 2884740 times.
✓ Branch 1 taken 1442370 times.
4327110 for (k=0; k<2; k++)
297
2/2
✓ Branch 0 taken 25103264 times.
✓ Branch 1 taken 2884740 times.
27988004 for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
298 25103264 cont++;
299
2/2
✓ Branch 0 taken 158040246 times.
✓ Branch 1 taken 25103264 times.
183143510 for (j=0; j<elbg->dim; j++)
300 158040246 newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j];
301 }
302
303 1442370 vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim);
304
305 1442370 get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]);
306
307 1442370 newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]);
308 1442370 newutility[2] += eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]);
309
310 1442370 newerror = newutility[2];
311
312 2884740 newerror += simple_lbg(elbg, elbg->dim, newcentroid, newutility, elbg->points,
313 1442370 elbg->cells[idx[1]]);
314
315
2/2
✓ Branch 0 taken 740303 times.
✓ Branch 1 taken 702067 times.
1442370 if (olderror > newerror) {
316 740303 shift_codebook(elbg, idx, newcentroid);
317
318 740303 elbg->error += newerror - olderror;
319
320
2/2
✓ Branch 0 taken 2220909 times.
✓ Branch 1 taken 740303 times.
2961212 for (j=0; j<3; j++)
321 2220909 update_utility_and_n_cb(elbg, idx[j], newutility[j]);
322
323 740303 evaluate_utility_inc(elbg);
324 }
325 1442370 }
326
327 /**
328 * Implementation of the ELBG block
329 */
330 5827940 static void do_shiftings(ELBGContext *elbg)
331 {
332 int idx[3];
333
334 5827940 evaluate_utility_inc(elbg);
335
336
2/2
✓ Branch 0 taken 13390518 times.
✓ Branch 1 taken 5827940 times.
19218458 for (idx[0]=0; idx[0] < elbg->num_cb; idx[0]++)
337
2/2
✓ Branch 0 taken 5872679 times.
✓ Branch 1 taken 7517839 times.
13390518 if (elbg->num_cb * elbg->utility[idx[0]] < elbg->error) {
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5872679 times.
5872679 if (elbg->utility_inc[elbg->num_cb - 1] == 0)
339 return;
340
341 5872679 idx[1] = get_high_utility_cell(elbg);
342 5872679 idx[2] = get_closest_codebook(elbg, idx[0]);
343
344
3/4
✓ Branch 0 taken 5872679 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1442370 times.
✓ Branch 3 taken 4430309 times.
5872679 if (idx[1] != idx[0] && idx[1] != idx[2])
345 1442370 try_shift_candidate(elbg, idx);
346 }
347 }
348
349 5790190 static void do_elbg(ELBGContext *av_restrict elbg, int *points, int numpoints,
350 int max_steps)
351 {
352 5790190 int *const size_part = elbg->size_part;
353 5790190 int i, j, steps = 0;
354 5790190 int best_idx = 0;
355 int64_t last_error;
356
357 5790190 elbg->error = INT64_MAX;
358 5790190 elbg->points = points;
359
360 do {
361 5827940 cell *free_cells = elbg->cell_buffer;
362 5827940 last_error = elbg->error;
363 5827940 steps++;
364 5827940 memset(elbg->utility, 0, elbg->num_cb * sizeof(*elbg->utility));
365 5827940 memset(elbg->cells, 0, elbg->num_cb * sizeof(*elbg->cells));
366
367 5827940 elbg->error = 0;
368
369 /* This loop evaluate the actual Voronoi partition. It is the most
370 costly part of the algorithm. */
371
2/2
✓ Branch 0 taken 76741726 times.
✓ Branch 1 taken 5827940 times.
82569666 for (i=0; i < numpoints; i++) {
372 76741726 int best_dist = distance_limited(elbg->points + i * elbg->dim,
373 76741726 elbg->codebook + best_idx * elbg->dim,
374 elbg->dim, INT_MAX);
375
2/2
✓ Branch 0 taken 2048030570 times.
✓ Branch 1 taken 76741726 times.
2124772296 for (int k = 0; k < elbg->num_cb; k++) {
376 2048030570 int dist = distance_limited(elbg->points + i * elbg->dim,
377 2048030570 elbg->codebook + k * elbg->dim,
378 elbg->dim, best_dist);
379
2/2
✓ Branch 0 taken 65114193 times.
✓ Branch 1 taken 1982916377 times.
2048030570 if (dist < best_dist) {
380 65114193 best_dist = dist;
381 65114193 best_idx = k;
382 }
383 }
384 76741726 elbg->nearest_cb[i] = best_idx;
385 76741726 elbg->error += best_dist;
386 76741726 elbg->utility[elbg->nearest_cb[i]] += best_dist;
387 76741726 free_cells->index = i;
388 76741726 free_cells->next = elbg->cells[elbg->nearest_cb[i]];
389 76741726 elbg->cells[elbg->nearest_cb[i]] = free_cells;
390 76741726 free_cells++;
391 }
392
393 5827940 do_shiftings(elbg);
394
395 5827940 memset(size_part, 0, elbg->num_cb * sizeof(*size_part));
396
397 5827940 memset(elbg->codebook, 0, elbg->num_cb * elbg->dim * sizeof(*elbg->codebook));
398
399
2/2
✓ Branch 0 taken 76741726 times.
✓ Branch 1 taken 5827940 times.
82569666 for (i=0; i < numpoints; i++) {
400 76741726 size_part[elbg->nearest_cb[i]]++;
401
2/2
✓ Branch 0 taken 327942996 times.
✓ Branch 1 taken 76741726 times.
404684722 for (j=0; j < elbg->dim; j++)
402 327942996 elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] +=
403 327942996 elbg->points[i*elbg->dim + j];
404 }
405
406
2/2
✓ Branch 0 taken 13390518 times.
✓ Branch 1 taken 5827940 times.
19218458 for (int i = 0; i < elbg->num_cb; i++)
407 13390518 vect_division(elbg->codebook + i*elbg->dim,
408 13390518 elbg->codebook + i*elbg->dim, size_part[i], elbg->dim);
409
410
4/4
✓ Branch 0 taken 5815245 times.
✓ Branch 1 taken 12695 times.
✓ Branch 2 taken 37750 times.
✓ Branch 3 taken 5777495 times.
5827940 } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) &&
411 (steps < max_steps));
412 5790190 }
413
414 #define BIG_PRIME 433494437LL
415
416 /**
417 * Initialize the codebook vector for the elbg algorithm.
418 * If numpoints <= 24 * num_cb this function fills codebook with random numbers.
419 * If not, it calls do_elbg for a (smaller) random sample of the points in
420 * points.
421 */
422 5790190 static void init_elbg(ELBGContext *av_restrict elbg, int *points, int *temp_points,
423 int numpoints, int max_steps)
424 {
425 5790190 int dim = elbg->dim;
426
427
2/2
✓ Branch 0 taken 30673 times.
✓ Branch 1 taken 5759517 times.
5790190 if (numpoints > 24LL * elbg->num_cb) {
428 /* ELBG is very costly for a big number of points. So if we have a lot
429 of them, get a good initial codebook to save on iterations */
430
2/2
✓ Branch 0 taken 1934047 times.
✓ Branch 1 taken 30673 times.
1964720 for (int i = 0; i < numpoints / 8; i++) {
431 1934047 int k = (i*BIG_PRIME) % numpoints;
432 1934047 memcpy(temp_points + i*dim, points + k*dim, dim * sizeof(*temp_points));
433 }
434
435 /* If anything is changed in the recursion parameters,
436 * the allocated size of temp_points will also need to be updated. */
437 30673 init_elbg(elbg, temp_points, temp_points + numpoints / 8 * dim,
438 numpoints / 8, 2 * max_steps);
439 30673 do_elbg(elbg, temp_points, numpoints / 8, 2 * max_steps);
440 } else // If not, initialize the codebook with random positions
441
2/2
✓ Branch 0 taken 13040848 times.
✓ Branch 1 taken 5759517 times.
18800365 for (int i = 0; i < elbg->num_cb; i++)
442 13040848 memcpy(elbg->codebook + i * dim, points + ((i*BIG_PRIME)%numpoints)*dim,
443 dim * sizeof(*elbg->codebook));
444 5790190 }
445
446 5759517 int avpriv_elbg_do(ELBGContext **elbgp, int *points, int dim, int numpoints,
447 int *codebook, int num_cb, int max_steps,
448 int *closest_cb, AVLFG *rand_state, uintptr_t flags)
449 {
450
2/2
✓ Branch 0 taken 5759507 times.
✓ Branch 1 taken 10 times.
5759517 ELBGContext *const av_restrict elbg = *elbgp ? *elbgp : av_mallocz(sizeof(*elbg));
451
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5759517 times.
5759517 if (!elbg)
453 return AVERROR(ENOMEM);
454 5759517 *elbgp = elbg;
455
456 5759517 elbg->nearest_cb = closest_cb;
457 5759517 elbg->rand_state = rand_state;
458 5759517 elbg->codebook = codebook;
459 5759517 elbg->num_cb = num_cb;
460 5759517 elbg->dim = dim;
461
462 #define ALLOCATE_IF_NECESSARY(field, new_elements, multiplicator) \
463 if (elbg->field ## _allocated < new_elements) { \
464 av_freep(&elbg->field); \
465 elbg->field = av_malloc_array(new_elements, \
466 multiplicator * sizeof(*elbg->field)); \
467 if (!elbg->field) { \
468 elbg->field ## _allocated = 0; \
469 return AVERROR(ENOMEM); \
470 } \
471 elbg->field ## _allocated = new_elements; \
472 }
473 /* Allocating the buffers for do_elbg() here once relies
474 * on their size being always the same even when do_elbg()
475 * is called from init_elbg(). It also relies on do_elbg()
476 * never calling itself recursively. */
477
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)
478
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)
479
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)
480
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)
481
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)
482
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)
483
2/2
✓ Branch 0 taken 23325 times.
✓ Branch 1 taken 5736192 times.
5759517 if (numpoints > 24LL * elbg->num_cb) {
484 /* The first step in the recursion in init_elbg() needs a buffer with
485 * (numpoints / 8) * dim elements; the next step needs numpoints / 8 / 8
486 * * dim elements etc. The geometric series leads to an upper bound of
487 * numpoints / 8 * 8 / 7 * dim elements. */
488 23325 uint64_t prod = dim * (uint64_t)(numpoints / 7U);
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23325 times.
23325 if (prod > INT_MAX)
490 return AVERROR(ERANGE);
491
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)
492 }
493
494 5759517 init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps);
495 5759517 do_elbg (elbg, points, numpoints, max_steps);
496 5759517 return 0;
497 }
498
499 10 av_cold void avpriv_elbg_free(ELBGContext **elbgp)
500 {
501 10 ELBGContext *elbg = *elbgp;
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!elbg)
503 return;
504
505 10 av_freep(&elbg->size_part);
506 10 av_freep(&elbg->utility);
507 10 av_freep(&elbg->cell_buffer);
508 10 av_freep(&elbg->cells);
509 10 av_freep(&elbg->utility_inc);
510 10 av_freep(&elbg->scratchbuf);
511 10 av_freep(&elbg->temp_points);
512
513 10 av_freep(elbgp);
514 }
515