FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsfgraph.c
Date: 2026-10-19 01:23:10
Exec Total Coverage
Lines: 136 202 67.3%
Functions: 13 15 86.7%
Branches: 80 136 58.8%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "config.h"
20
21 #include <stddef.h>
22 #include <string.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/error.h"
26 #include "libavutil/mem.h"
27 #include "libavutil/opt.h"
28
29 #include "bsf.h"
30 #include "bsf_internal.h"
31
32 #define OFFSET(x) offsetof(AVBitStreamFilterGraph, x)
33 #define FLAGS (AV_OPT_FLAG_BSF_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_VIDEO_PARAM)
34 static const AVOption filtergraph_options[] = {
35 {"max_buffered_packets" , "maximum number of buffered packets allowed", OFFSET(max_buffered_packets),
36 AV_OPT_TYPE_UINT, {.i64 = 0}, 0, UINT_MAX, FLAGS },
37 { NULL },
38 };
39
40 static const AVClass filtergraph_class = {
41 .class_name = "AVBitStreamFilterGraph",
42 .item_name = av_default_item_name,
43 .version = LIBAVUTIL_VERSION_INT,
44 .option = filtergraph_options,
45 .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER,
46 };
47
48 7 AVBitStreamFilterGraph *av_bsf_graph_alloc(void)
49 {
50 7 FFBitStreamFilterGraph *graph = av_mallocz(sizeof(*graph));
51 AVBitStreamFilterGraph *ret;
52
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!graph)
54 ✗ return NULL;
55
56 7 ret = &graph->p;
57 7 ret->av_class = &filtergraph_class;
58 7 av_opt_set_defaults(ret);
59 7 graph->max_packet_queue = SIZE_MAX;
60
61 7 return ret;
62 }
63
64 28 void ff_bsf_graph_remove_filter(AVBitStreamFilterGraph *graph, AVBitStreamFilterContext *filter)
65 {
66 int i, j;
67
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 for (i = 0; i < graph->nb_filters; i++) {
68
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (graph->filters[i] == filter) {
69 28 FFSWAP(AVBitStreamFilterContext*, graph->filters[i],
70 graph->filters[graph->nb_filters - 1]);
71 28 graph->nb_filters--;
72 28 filter->graph = NULL;
73
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 28 times.
49 for (j = 0; j<filter->nb_outputs; j++)
74
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if (filter->outputs[j])
75 2 filter->outputs[j]->graph = NULL;
76
77 28 return;
78 }
79 }
80 }
81
82 2 AVBitStreamFilterContext *av_bsf_graph_get_filter(AVBitStreamFilterGraph *graph, const char *name)
83 {
84 int i;
85
86
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 for (i = 0; i < graph->nb_filters; i++)
87
3/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
6 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
88 2 return graph->filters[i];
89
90 ✗ return NULL;
91 }
92
93 87 void av_bsf_graph_free(AVBitStreamFilterGraph **graphp)
94 {
95 87 AVBitStreamFilterGraph *graph = *graphp;
96 87 FFBitStreamFilterGraph *graphi = ffbsffiltergraph(graph);
97
98
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 7 times.
87 if (!graph)
99 80 return;
100
101
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7 times.
35 while (graph->nb_filters)
102 28 ff_bsf_free(graph->filters[0]);
103
104 7 av_freep(&graphi->sink_links);
105 7 av_freep(&graphi->source_links);
106
107 7 av_opt_free(graph);
108
109 7 av_freep(&graph->filters);
110 7 av_freep(graphp);
111 }
112
113 ✗ int av_bsf_graph_create_filter(AVBitStreamFilterContext **filt_ctx, const AVBitStreamFilter *filt,
114 const char *name, AVDictionary **options, AVBitStreamFilterGraph *graph_ctx)
115 {
116 AVBitStreamFilterContext *s;
117 int ret;
118
119 ✗ ret = av_bsf_graph_alloc_filter(&s, filt, name, graph_ctx);
120 ✗ if (ret < 0)
121 ✗ return ret;
122
123 ✗ ret = av_bsf_init_dict(s, options);
124 ✗ if (ret < 0)
125 ✗ goto fail;
126
127 ✗ if (filt_ctx)
128 ✗ *filt_ctx = s;
129
130 ✗ return 0;
131
132 ✗ fail:
133 ✗ ff_bsf_free(s);
134 ✗ if (filt_ctx)
135 ✗ *filt_ctx = NULL;
136 ✗ return ret;
137 }
138
139 28 int av_bsf_graph_alloc_filter(AVBitStreamFilterContext **filt_ctx,
140 const AVBitStreamFilter *filter,
141 const char *name,
142 AVBitStreamFilterGraph *graph)
143 {
144 AVBitStreamFilterContext **filters, *s;
145 int ret;
146
147
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
28 if (!ff_bsf(filter)->activate && !ff_bsf(filter)->nb_inputs && !ff_bsf(filter)->nb_outputs)
148 ✗ return AVERROR(ENOTSUP);
149
150 28 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (!filters)
152 ✗ return AVERROR(ENOMEM);
153 28 graph->filters = filters;
154
155 28 ret = ff_bsf_alloc(filter, name, &s);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
157 ✗ return ret;
158
159 28 graph->filters[graph->nb_filters++] = s;
160
161 28 s->graph = graph;
162
163
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 7 times.
28 if (filt_ctx)
164 21 *filt_ctx = s;
165
166 28 return ret;
167 }
168
169 /**
170 * Check for the validity of graph.
171 *
172 * A graph is considered valid if all its input and output pads are
173 * connected.
174 *
175 * @return >= 0 in case of success, a negative value otherwise
176 */
177 2 static int graph_check_validity(AVBitStreamFilterGraph *graph, void *log_ctx)
178 {
179 AVBitStreamFilterContext *filt;
180 int i, j;
181
182
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < graph->nb_filters; i++) {
183 const AVBitStreamFilterPad *pad;
184 8 filt = graph->filters[i];
185
186
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 for (j = 0; j < filt->nb_inputs; j++) {
187
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!filt->inputs[j] || !filt->inputs[j]->src) {
188 ✗ pad = &filt->input_pads[j];
189 ✗ av_log(log_ctx, AV_LOG_ERROR,
190 "Input pad \"%s\" of the filter instance \"%s\" of %s not connected to any source\n",
191 ✗ pad->name, filt->name, filt->filter->name);
192 ✗ return AVERROR(EINVAL);
193 }
194 }
195
196
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 for (j = 0; j < filt->nb_outputs; j++) {
197
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
198 ✗ pad = &filt->output_pads[j];
199 ✗ av_log(log_ctx, AV_LOG_ERROR,
200 "Output pad \"%s\" of the filter instance \"%s\" of %s not connected to any destination\n",
201 ✗ pad->name, filt->name, filt->filter->name);
202 ✗ return AVERROR(EINVAL);
203 }
204 }
205 }
206
207 2 return 0;
208 }
209
210 /**
211 * Configure all the links of graphctx.
212 *
213 * @return >= 0 in case of success, a negative value otherwise
214 */
215 2 static int graph_config_links(AVBitStreamFilterGraph *graph, void *log_ctx)
216 {
217 AVBitStreamFilterContext *filt;
218 int i, ret;
219
220
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < graph->nb_filters; i++) {
221 8 filt = graph->filters[i];
222
223
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (!filt->nb_outputs) {
224
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_bsf_config_links(filt)))
225 ✗ return ret;
226 }
227 }
228
229 2 return 0;
230 }
231
232 2 static int graph_config_pointers(AVBitStreamFilterGraph *graph, void *log_ctx)
233 {
234 unsigned i, j;
235 2 int sink_links_count = 0, source_links_count = 0, n = 0;
236 AVBitStreamFilterContext *f;
237 BitStreamFilterLinkInternal **sinks, **sources;
238
239
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < graph->nb_filters; i++) {
240 8 f = graph->filters[i];
241
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 for (j = 0; j < f->nb_inputs; j++) {
242 6 ff_link_internal(f->inputs[j])->age_index = -1;
243 }
244
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8 times.
14 for (j = 0; j < f->nb_outputs; j++) {
245 6 ff_link_internal(f->outputs[j])->age_index = -1;
246 }
247
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (!f->nb_outputs) {
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (f->nb_inputs > INT_MAX - sink_links_count)
249 ✗ return AVERROR(EINVAL);
250 2 sink_links_count += f->nb_inputs;
251 }
252
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 if (!f->nb_inputs && !strcmp(f->filter->name, "source")) {
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (f->nb_outputs > INT_MAX - source_links_count)
254 ✗ return AVERROR(EINVAL);
255 4 source_links_count += f->nb_outputs;
256 }
257 }
258 2 sinks = av_calloc(sink_links_count, sizeof(*sinks));
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sinks)
260 ✗ return AVERROR(ENOMEM);
261
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < graph->nb_filters; i++) {
262 8 f = graph->filters[i];
263
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (!f->nb_outputs) {
264
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (j = 0; j < f->nb_inputs; j++) {
265 2 sinks[n] = ff_link_internal(f->inputs[j]);
266 2 sinks[n]->age_index = n;
267 2 n++;
268 }
269 }
270 }
271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(n == sink_links_count);
272 2 ffbsffiltergraph(graph)->sink_links = sinks;
273 2 ffbsffiltergraph(graph)->sink_links_count = sink_links_count;
274
275 2 sources = av_calloc(source_links_count, sizeof(*sources));
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sources)
277 ✗ return AVERROR(ENOMEM);
278
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0, n = 0; i < graph->nb_filters; i++) {
279 8 f = graph->filters[i];
280
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 if (!f->nb_inputs && !strcmp(f->filter->name, "source")) {
281
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (j = 0; j < f->nb_outputs; j++) {
282 4 sources[n] = ff_link_internal(f->outputs[j]);
283 4 n++;
284 }
285 }
286 }
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(n == source_links_count);
288 2 ffbsffiltergraph(graph)->source_links = sources;
289 2 ffbsffiltergraph(graph)->source_links_count = source_links_count;
290
291 2 return 0;
292 }
293
294 2 int av_bsf_graph_config(AVBitStreamFilterGraph *graphctx, void *log_ctx)
295 {
296 int ret;
297
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (graphctx->max_buffered_packets)
299 ✗ ffbsffiltergraph(graphctx)->max_packet_queue = graphctx->max_buffered_packets;
300
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = graph_check_validity(graphctx, log_ctx)))
301 ✗ return ret;
302
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = graph_config_links(graphctx, log_ctx)))
303 ✗ return ret;
304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = graph_config_pointers(graphctx, log_ctx)))
305 ✗ return ret;
306
307 2 return 0;
308 }
309
310 62 static void heap_bubble_up(FFBitStreamFilterGraph *graph,
311 BitStreamFilterLinkInternal *li, int index)
312 {
313 62 BitStreamFilterLinkInternal **links = graph->sink_links;
314
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 av_assert0(index >= 0);
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
62 while (index) {
318 ✗ int parent = (index - 1) >> 1;
319 ✗ if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
320 ✗ break;
321 ✗ links[index] = links[parent];
322 ✗ links[index]->age_index = index;
323 ✗ index = parent;
324 }
325 62 links[index] = li;
326 62 li->age_index = index;
327 62 }
328
329 62 static void heap_bubble_down(FFBitStreamFilterGraph *graph,
330 BitStreamFilterLinkInternal *li, int index)
331 {
332 62 BitStreamFilterLinkInternal **links = graph->sink_links;
333
334
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 av_assert0(index >= 0);
335
336 ✗ while (1) {
337 62 int child = 2 * index + 1;
338
1/2
✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
62 if (child >= graph->sink_links_count)
339 62 break;
340 ✗ if (child + 1 < graph->sink_links_count &&
341 ✗ links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
342 ✗ child++;
343 ✗ if (li->l.current_pts_us < links[child]->l.current_pts_us)
344 ✗ break;
345 ✗ links[index] = links[child];
346 ✗ links[index]->age_index = index;
347 ✗ index = child;
348 }
349 62 links[index] = li;
350 62 li->age_index = index;
351 62 }
352
353 62 void ff_bsf_graph_update_heap(AVBitStreamFilterGraph *graph, BitStreamFilterLinkInternal *li)
354 {
355 62 FFBitStreamFilterGraph *graphi = ffbsffiltergraph(graph);
356
357 62 heap_bubble_up (graphi, li, li->age_index);
358 62 heap_bubble_down(graphi, li, li->age_index);
359 62 }
360
361 542 int ff_bsf_graph_run_once(AVBitStreamFilterGraph *graph)
362 {
363 FFBitStreamFilterContext *ctxi;
364 unsigned i;
365
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 av_assert0(graph->nb_filters);
367 542 ctxi = ffbsfctx(graph->filters[0]);
368
2/2
✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 542 times.
2168 for (i = 1; i < graph->nb_filters; i++) {
369 1626 FFBitStreamFilterContext *ctxi_other = ffbsfctx(graph->filters[i]);
370
371
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 1264 times.
1626 if (ctxi_other->ready > ctxi->ready)
372 362 ctxi = ctxi_other;
373 }
374
375
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 362 times.
542 if (!ctxi->ready)
376 180 return AVERROR(EAGAIN);
377
378 362 ctxi->ready = 0;
379
380 362 return ff_bsf_activate(&ctxi->p);
381 }
382
383 ✗ int av_bsf_graph_source_needs_input(const AVBitStreamFilterGraph *graph)
384 {
385 ✗ const FFBitStreamFilterGraph *graphi = cffbsffiltergraph(graph);
386 ✗ int nb_requests, nb_requests_max = -1;
387 ✗ int best_input = AVERROR(EOF);
388
389 ✗ for (int i = 0; i < graphi->source_links_count; i++) {
390 ✗ const BitStreamFilterLinkInternal *sourcei = graphi->source_links[i];
391 ✗ const AVBitStreamFilterLink *source = &sourcei->l;
392
393 ✗ if (av_bsf_source_get_status(source->src) == AVERROR(EOF))
394 ✗ continue;
395
396 ✗ nb_requests = ff_bsf_source_get_nb_failed_requests(source->src);
397 ✗ if (nb_requests > nb_requests_max) {
398 ✗ nb_requests_max = nb_requests;
399 ✗ best_input = i;
400 }
401 }
402
403 ✗ return best_input;
404 }
405