FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/elbg.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 226 232 97.4%
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 2512940434 static inline int distance_limited(int *a, int *b, int dim, int limit)
75 {
76 2512940434 int i, dist=0;
77
2/2
✓ Branch 0 taken 5028816911 times.
✓ Branch 1 taken 343042159 times.
5371859070 for (i=0; i<dim; i++) {
78 5028816911 dist += (a[i] - b[i])*(a[i] - b[i]);
79
2/2
✓ Branch 0 taken 2169898275 times.
✓ Branch 1 taken 2858918636 times.
5028816911 if (dist > limit)
80 2169898275 return INT_MAX;
81 }
82
83 343042159 return dist;
84 }
85
86 17887608 static inline void vect_division(int *res, int *vect, int div, int dim)
87 {
88 int i;
89
2/2
✓ Branch 0 taken 13070260 times.
✓ Branch 1 taken 4817348 times.
17887608 if (div > 1)
90
2/2
✓ Branch 0 taken 56803470 times.
✓ Branch 1 taken 13070260 times.
69873730 for (i=0; i<dim; i++)
91
1/2
✓ Branch 0 taken 56803470 times.
✗ Branch 1 not taken.
56803470 res[i] = ROUNDED_DIV(vect[i],div);
92
2/2
✓ Branch 0 taken 749779 times.
✓ Branch 1 taken 4067569 times.
4817348 else if (res != vect)
93 749779 memcpy(res, vect, dim*sizeof(int));
94
95 17887608 }
96
97 2955954 static int eval_error_cell(ELBGContext *elbg, int *centroid, cell *cells)
98 {
99 2955954 int error=0;
100
2/2
✓ Branch 0 taken 25574305 times.
✓ Branch 1 taken 2955954 times.
28530259 for (; cells; cells=cells->next)
101 25574305 error += distance_limited(centroid, elbg->points + cells->index*elbg->dim, elbg->dim, INT_MAX);
102
103 2955954 return error;
104 }
105
106 5909519 static int get_closest_codebook(ELBGContext *elbg, int index)
107 {
108 5909519 int pick = 0;
109
2/2
✓ Branch 0 taken 232738546 times.
✓ Branch 1 taken 5909519 times.
238648065 for (int i = 0, diff_min = INT_MAX; i < elbg->num_cb; i++)
110
2/2
✓ Branch 0 taken 226829027 times.
✓ Branch 1 taken 5909519 times.
232738546 if (i != index) {
111 int diff;
112 226829027 diff = distance_limited(elbg->codebook + i*elbg->dim, elbg->codebook + index*elbg->dim, elbg->dim, diff_min);
113
2/2
✓ Branch 0 taken 12118877 times.
✓ Branch 1 taken 214710150 times.
226829027 if (diff < diff_min) {
114 12118877 pick = i;
115 12118877 diff_min = diff;
116 }
117 }
118 5909519 return pick;
119 }
120
121 5909519 static int get_high_utility_cell(ELBGContext *elbg)
122 {
123 5909519 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 5909519 times.
✗ Branch 1 not taken.
5909519 if (elbg->utility_inc[elbg->num_cb - 1] < INT_MAX) {
128 5909519 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 112090477 times.
✓ Branch 1 taken 5909519 times.
117999996 while (elbg->utility_inc[i] < r) {
135 112090477 i++;
136 }
137
138 av_assert2(elbg->cells[i]);
139
140 5909519 return i;
141 }
142
143 /**
144 * Implementation of the simple LBG algorithm for just two codebooks
145 */
146 1477977 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 1477977 int numpoints[2] = {0,0};
155 1477977 int *newcentroid[2] = {
156 1477977 elbg->scratchbuf + 3*dim,
157 1477977 elbg->scratchbuf + 4*dim
158 };
159 cell *tempcell;
160
161 1477977 memset(newcentroid[0], 0, 2 * dim * sizeof(*newcentroid[0]));
162
163 1477977 newutility[0] =
164 1477977 newutility[1] = 0;
165
166
2/2
✓ Branch 0 taken 20188081 times.
✓ Branch 1 taken 1477977 times.
21666058 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
167 20188081 idx = distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX)>=
168 20188081 distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX);
169 20188081 numpoints[idx]++;
170
2/2
✓ Branch 0 taken 124244430 times.
✓ Branch 1 taken 20188081 times.
144432511 for (i=0; i<dim; i++)
171 124244430 newcentroid[idx][i] += points[tempcell->index*dim + i];
172 }
173
174 1477977 vect_division(centroid[0], newcentroid[0], numpoints[0], dim);
175 1477977 vect_division(centroid[1], newcentroid[1], numpoints[1], dim);
176
177
2/2
✓ Branch 0 taken 20188081 times.
✓ Branch 1 taken 1477977 times.
21666058 for (tempcell = cells; tempcell; tempcell=tempcell->next) {
178 20188081 int dist[2] = {distance_limited(centroid[0], points + tempcell->index*dim, dim, INT_MAX),
179 20188081 distance_limited(centroid[1], points + tempcell->index*dim, dim, INT_MAX)};
180 20188081 int idx = dist[0] > dist[1];
181 20188081 newutility[idx] += dist[idx];
182 }
183
184 1477977 return newutility[0] + newutility[1];
185 }
186
187 1477977 static void get_new_centroids(ELBGContext *elbg, int huc, int *newcentroid_i,
188 int *newcentroid_p)
189 {
190 cell *tempcell;
191 1477977 int *min = newcentroid_i;
192 1477977 int *max = newcentroid_p;
193 int i;
194
195
2/2
✓ Branch 0 taken 9057744 times.
✓ Branch 1 taken 1477977 times.
10535721 for (i=0; i< elbg->dim; i++) {
196 9057744 min[i]=INT_MAX;
197 9057744 max[i]=0;
198 }
199
200
2/2
✓ Branch 0 taken 20188081 times.
✓ Branch 1 taken 1477977 times.
21666058 for (tempcell = elbg->cells[huc]; tempcell; tempcell = tempcell->next)
201
2/2
✓ Branch 0 taken 124244430 times.
✓ Branch 1 taken 20188081 times.
144432511 for(i=0; i<elbg->dim; i++) {
202 124244430 min[i]=FFMIN(min[i], elbg->points[tempcell->index*elbg->dim + i]);
203 124244430 max[i]=FFMAX(max[i], elbg->points[tempcell->index*elbg->dim + i]);
204 }
205
206
2/2
✓ Branch 0 taken 9057744 times.
✓ Branch 1 taken 1477977 times.
10535721 for (i=0; i<elbg->dim; i++) {
207 9057744 int ni = min[i] + (max[i] - min[i])/3;
208 9057744 int np = min[i] + (2*(max[i] - min[i]))/3;
209 9057744 newcentroid_i[i] = ni;
210 9057744 newcentroid_p[i] = np;
211 }
212 1477977 }
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 759468 static void shift_codebook(ELBGContext *elbg, int *indexes,
224 int *newcentroid[3])
225 {
226 cell *tempdata;
227 759468 cell **pp = &elbg->cells[indexes[2]];
228
229
2/2
✓ Branch 0 taken 6043993 times.
✓ Branch 1 taken 759468 times.
6803461 while(*pp)
230 6043993 pp= &(*pp)->next;
231
232 759468 *pp = elbg->cells[indexes[0]];
233
234 759468 elbg->cells[indexes[0]] = NULL;
235 759468 tempdata = elbg->cells[indexes[1]];
236 759468 elbg->cells[indexes[1]] = NULL;
237
238
2/2
✓ Branch 0 taken 8604324 times.
✓ Branch 1 taken 759468 times.
9363792 while(tempdata) {
239 8604324 cell *tempcell2 = tempdata->next;
240 8604324 int idx = distance_limited(elbg->points + tempdata->index*elbg->dim,
241 8604324 newcentroid[0], elbg->dim, INT_MAX) >
242 8604324 distance_limited(elbg->points + tempdata->index*elbg->dim,
243 8604324 newcentroid[1], elbg->dim, INT_MAX);
244
245 8604324 tempdata->next = elbg->cells[indexes[idx]];
246 8604324 elbg->cells[indexes[idx]] = tempdata;
247 8604324 tempdata = tempcell2;
248 }
249 759468 }
250
251 6589862 static void evaluate_utility_inc(ELBGContext *elbg)
252 {
253 6589862 int64_t inc=0;
254
255
2/2
✓ Branch 0 taken 131951189 times.
✓ Branch 1 taken 6589862 times.
138541051 for (int i = 0; i < elbg->num_cb; i++) {
256
2/2
✓ Branch 0 taken 45667224 times.
✓ Branch 1 taken 86283965 times.
131951189 if (elbg->num_cb * elbg->utility[i] > elbg->error)
257 45667224 inc += elbg->utility[i];
258 131951189 elbg->utility_inc[i] = inc;
259 }
260 6589862 }
261
262
263 2278404 static void update_utility_and_n_cb(ELBGContext *elbg, int idx, int newutility)
264 {
265 cell *tempcell;
266
267 2278404 elbg->utility[idx] = newutility;
268
2/2
✓ Branch 0 taken 18842281 times.
✓ Branch 1 taken 2278404 times.
21120685 for (tempcell=elbg->cells[idx]; tempcell; tempcell=tempcell->next)
269 18842281 elbg->nearest_cb[tempcell->index] = idx;
270 2278404 }
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 1477977 static void try_shift_candidate(ELBGContext *elbg, int idx[3])
280 {
281 1477977 int j, k, cont=0;
282 1477977 int64_t olderror=0, newerror;
283 int newutility[3];
284 1477977 int *newcentroid[3] = {
285 1477977 elbg->scratchbuf,
286 1477977 elbg->scratchbuf + elbg->dim,
287 1477977 elbg->scratchbuf + 2*elbg->dim
288 };
289 cell *tempcell;
290
291
2/2
✓ Branch 0 taken 4433931 times.
✓ Branch 1 taken 1477977 times.
5911908 for (j=0; j<3; j++)
292 4433931 olderror += elbg->utility[idx[j]];
293
294 1477977 memset(newcentroid[2], 0, elbg->dim*sizeof(int));
295
296
2/2
✓ Branch 0 taken 2955954 times.
✓ Branch 1 taken 1477977 times.
4433931 for (k=0; k<2; k++)
297
2/2
✓ Branch 0 taken 25574305 times.
✓ Branch 1 taken 2955954 times.
28530259 for (tempcell=elbg->cells[idx[2*k]]; tempcell; tempcell=tempcell->next) {
298 25574305 cont++;
299
2/2
✓ Branch 0 taken 160866492 times.
✓ Branch 1 taken 25574305 times.
186440797 for (j=0; j<elbg->dim; j++)
300 160866492 newcentroid[2][j] += elbg->points[tempcell->index*elbg->dim + j];
301 }
302
303 1477977 vect_division(newcentroid[2], newcentroid[2], cont, elbg->dim);
304
305 1477977 get_new_centroids(elbg, idx[1], newcentroid[0], newcentroid[1]);
306
307 1477977 newutility[2] = eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[0]]);
308 1477977 newutility[2] += eval_error_cell(elbg, newcentroid[2], elbg->cells[idx[2]]);
309
310 1477977 newerror = newutility[2];
311
312 2955954 newerror += simple_lbg(elbg, elbg->dim, newcentroid, newutility, elbg->points,
313 1477977 elbg->cells[idx[1]]);
314
315
2/2
✓ Branch 0 taken 759468 times.
✓ Branch 1 taken 718509 times.
1477977 if (olderror > newerror) {
316 759468 shift_codebook(elbg, idx, newcentroid);
317
318 759468 elbg->error += newerror - olderror;
319
320
2/2
✓ Branch 0 taken 2278404 times.
✓ Branch 1 taken 759468 times.
3037872 for (j=0; j<3; j++)
321 2278404 update_utility_and_n_cb(elbg, idx[j], newutility[j]);
322
323 759468 evaluate_utility_inc(elbg);
324 }
325 1477977 }
326
327 /**
328 * Implementation of the ELBG block
329 */
330 5830394 static void do_shiftings(ELBGContext *elbg)
331 {
332 int idx[3];
333
334 5830394 evaluate_utility_inc(elbg);
335
336
2/2
✓ Branch 0 taken 13453677 times.
✓ Branch 1 taken 5830394 times.
19284071 for (idx[0]=0; idx[0] < elbg->num_cb; idx[0]++)
337
2/2
✓ Branch 0 taken 5909519 times.
✓ Branch 1 taken 7544158 times.
13453677 if (elbg->num_cb * elbg->utility[idx[0]] < elbg->error) {
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5909519 times.
5909519 if (elbg->utility_inc[elbg->num_cb - 1] == 0)
339 return;
340
341 5909519 idx[1] = get_high_utility_cell(elbg);
342 5909519 idx[2] = get_closest_codebook(elbg, idx[0]);
343
344
3/4
✓ Branch 0 taken 5909519 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1477977 times.
✓ Branch 3 taken 4431542 times.
5909519 if (idx[1] != idx[0] && idx[1] != idx[2])
345 1477977 try_shift_candidate(elbg, idx);
346 }
347 }
348
349 5792078 static void do_elbg(ELBGContext *av_restrict elbg, int *points, int numpoints,
350 int max_steps)
351 {
352 5792078 int *const size_part = elbg->size_part;
353 5792078 int i, j, steps = 0;
354 5792078 int best_idx = 0;
355 int64_t last_error;
356
357 5792078 elbg->error = INT64_MAX;
358 5792078 elbg->points = points;
359
360 do {
361 5830394 cell *free_cells = elbg->cell_buffer;
362 5830394 last_error = elbg->error;
363 5830394 steps++;
364 5830394 memset(elbg->utility, 0, elbg->num_cb * sizeof(*elbg->utility));
365 5830394 memset(elbg->cells, 0, elbg->num_cb * sizeof(*elbg->cells));
366
367 5830394 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 77232845 times.
✓ Branch 1 taken 5830394 times.
83063239 for (i=0; i < numpoints; i++) {
372 77232845 int best_dist = distance_limited(elbg->points + i * elbg->dim,
373 77232845 elbg->codebook + best_idx * elbg->dim,
374 elbg->dim, INT_MAX);
375
2/2
✓ Branch 0 taken 2085343285 times.
✓ Branch 1 taken 77232845 times.
2162576130 for (int k = 0; k < elbg->num_cb; k++) {
376 2085343285 int dist = distance_limited(elbg->points + i * elbg->dim,
377 2085343285 elbg->codebook + k * elbg->dim,
378 elbg->dim, best_dist);
379
2/2
✓ Branch 0 taken 66209922 times.
✓ Branch 1 taken 2019133363 times.
2085343285 if (dist < best_dist) {
380 66209922 best_dist = dist;
381 66209922 best_idx = k;
382 }
383 }
384 77232845 elbg->nearest_cb[i] = best_idx;
385 77232845 elbg->error += best_dist;
386 77232845 elbg->utility[elbg->nearest_cb[i]] += best_dist;
387 77232845 free_cells->index = i;
388 77232845 free_cells->next = elbg->cells[elbg->nearest_cb[i]];
389 77232845 elbg->cells[elbg->nearest_cb[i]] = free_cells;
390 77232845 free_cells++;
391 }
392
393 5830394 do_shiftings(elbg);
394
395 5830394 memset(size_part, 0, elbg->num_cb * sizeof(*size_part));
396
397 5830394 memset(elbg->codebook, 0, elbg->num_cb * elbg->dim * sizeof(*elbg->codebook));
398
399
2/2
✓ Branch 0 taken 77232845 times.
✓ Branch 1 taken 5830394 times.
83063239 for (i=0; i < numpoints; i++) {
400 77232845 size_part[elbg->nearest_cb[i]]++;
401
2/2
✓ Branch 0 taken 330889710 times.
✓ Branch 1 taken 77232845 times.
408122555 for (j=0; j < elbg->dim; j++)
402 330889710 elbg->codebook[elbg->nearest_cb[i]*elbg->dim + j] +=
403 330889710 elbg->points[i*elbg->dim + j];
404 }
405
406
2/2
✓ Branch 0 taken 13453677 times.
✓ Branch 1 taken 5830394 times.
19284071 for (int i = 0; i < elbg->num_cb; i++)
407 13453677 vect_division(elbg->codebook + i*elbg->dim,
408 13453677 elbg->codebook + i*elbg->dim, size_part[i], elbg->dim);
409
410
4/4
✓ Branch 0 taken 5817605 times.
✓ Branch 1 taken 12789 times.
✓ Branch 2 taken 38316 times.
✓ Branch 3 taken 5779289 times.
5830394 } while(((last_error - elbg->error) > DELTA_ERR_MAX*elbg->error) &&
411 (steps < max_steps));
412 5792078 }
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 5792078 static void init_elbg(ELBGContext *av_restrict elbg, int *points, int *temp_points,
423 int numpoints, int max_steps)
424 {
425 5792078 int dim = elbg->dim;
426
427
2/2
✓ Branch 0 taken 31211 times.
✓ Branch 1 taken 5760867 times.
5792078 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 1958656 times.
✓ Branch 1 taken 31211 times.
1989867 for (int i = 0; i < numpoints / 8; i++) {
431 1958656 int k = (i*BIG_PRIME) % numpoints;
432 1958656 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 31211 init_elbg(elbg, temp_points, temp_points + numpoints / 8 * dim,
438 numpoints / 8, 2 * max_steps);
439 31211 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 13097086 times.
✓ Branch 1 taken 5760867 times.
18857953 for (int i = 0; i < elbg->num_cb; i++)
442 13097086 memcpy(elbg->codebook + i * dim, points + ((i*BIG_PRIME)%numpoints)*dim,
443 dim * sizeof(*elbg->codebook));
444 5792078 }
445
446 5760867 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 5760857 times.
✓ Branch 1 taken 10 times.
5760867 ELBGContext *const av_restrict elbg = *elbgp ? *elbgp : av_mallocz(sizeof(*elbg));
451
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5760867 times.
5760867 if (!elbg)
453 return AVERROR(ENOMEM);
454 5760867 *elbgp = elbg;
455
456 5760867 elbg->nearest_cb = closest_cb;
457 5760867 elbg->rand_state = rand_state;
458 5760867 elbg->codebook = codebook;
459 5760867 elbg->num_cb = num_cb;
460 5760867 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 5760838 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
5760867 ALLOCATE_IF_NECESSARY(cells, num_cb, 1)
478
3/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5760838 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
5760867 ALLOCATE_IF_NECESSARY(utility, num_cb, 1)
479
3/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5760838 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
5760867 ALLOCATE_IF_NECESSARY(utility_inc, num_cb, 1)
480
3/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5760838 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
5760867 ALLOCATE_IF_NECESSARY(size_part, num_cb, 1)
481
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 5760850 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 17 times.
5760867 ALLOCATE_IF_NECESSARY(cell_buffer, numpoints, 1)
482
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 5760857 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
5760867 ALLOCATE_IF_NECESSARY(scratchbuf, dim, 5)
483
2/2
✓ Branch 0 taken 23799 times.
✓ Branch 1 taken 5737068 times.
5760867 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 23799 uint64_t prod = dim * (uint64_t)(numpoints / 7U);
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23799 times.
23799 if (prod > INT_MAX)
490 return AVERROR(ERANGE);
491
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 23788 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
23799 ALLOCATE_IF_NECESSARY(temp_points, prod, 1)
492 }
493
494 5760867 init_elbg(elbg, points, elbg->temp_points, numpoints, max_steps);
495 5760867 do_elbg (elbg, points, numpoints, max_steps);
496 5760867 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