FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsfgraph.c
Date: 2026-07-21 08:37:06
Exec Total Coverage
Lines: 41 202 20.3%
Functions: 4 15 26.7%
Branches: 15 136 11.0%

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 3 AVBitStreamFilterGraph *av_bsf_graph_alloc(void)
49 {
50 3 FFBitStreamFilterGraph *graph = av_mallocz(sizeof(*graph));
51 AVBitStreamFilterGraph *ret;
52
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!graph)
54 return NULL;
55
56 3 ret = &graph->p;
57 3 ret->av_class = &filtergraph_class;
58 3 av_opt_set_defaults(ret);
59 3 graph->max_packet_queue = SIZE_MAX;
60
61 3 return ret;
62 }
63
64 12 void ff_bsf_graph_remove_filter(AVBitStreamFilterGraph *graph, AVBitStreamFilterContext *filter)
65 {
66 int i, j;
67
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (i = 0; i < graph->nb_filters; i++) {
68
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (graph->filters[i] == filter) {
69 12 FFSWAP(AVBitStreamFilterContext*, graph->filters[i],
70 graph->filters[graph->nb_filters - 1]);
71 12 graph->nb_filters--;
72 12 filter->graph = NULL;
73
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 12 times.
21 for (j = 0; j<filter->nb_outputs; j++)
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (filter->outputs[j])
75 filter->outputs[j]->graph = NULL;
76
77 12 return;
78 }
79 }
80 }
81
82 AVBitStreamFilterContext *av_bsf_graph_get_filter(AVBitStreamFilterGraph *graph, const char *name)
83 {
84 int i;
85
86 for (i = 0; i < graph->nb_filters; i++)
87 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
88 return graph->filters[i];
89
90 return NULL;
91 }
92
93 75 void av_bsf_graph_free(AVBitStreamFilterGraph **graphp)
94 {
95 75 AVBitStreamFilterGraph *graph = *graphp;
96 75 FFBitStreamFilterGraph *graphi = ffbsffiltergraph(graph);
97
98
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 3 times.
75 if (!graph)
99 72 return;
100
101
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 while (graph->nb_filters)
102 12 ff_bsf_free(graph->filters[0]);
103
104 3 av_freep(&graphi->sink_links);
105 3 av_freep(&graphi->source_links);
106
107 3 av_opt_free(graph);
108
109 3 av_freep(&graph->filters);
110 3 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 12 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 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
12 if (!ff_bsf(filter)->activate && !ff_bsf(filter)->nb_inputs && !ff_bsf(filter)->nb_outputs)
148 return AVERROR(ENOTSUP);
149
150 12 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (!filters)
152 return AVERROR(ENOMEM);
153 12 graph->filters = filters;
154
155 12 ret = ff_bsf_alloc(filter, name, &s);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (ret < 0)
157 return ret;
158
159 12 graph->filters[graph->nb_filters++] = s;
160
161 12 s->graph = graph;
162
163
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 if (filt_ctx)
164 9 *filt_ctx = s;
165
166 12 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 static int graph_check_validity(AVBitStreamFilterGraph *graph, void *log_ctx)
178 {
179 AVBitStreamFilterContext *filt;
180 int i, j;
181
182 for (i = 0; i < graph->nb_filters; i++) {
183 const AVBitStreamFilterPad *pad;
184 filt = graph->filters[i];
185
186 for (j = 0; j < filt->nb_inputs; j++) {
187 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 for (j = 0; j < filt->nb_outputs; j++) {
197 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 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 static int graph_config_links(AVBitStreamFilterGraph *graph, void *log_ctx)
216 {
217 AVBitStreamFilterContext *filt;
218 int i, ret;
219
220 for (i = 0; i < graph->nb_filters; i++) {
221 filt = graph->filters[i];
222
223 if (!filt->nb_outputs) {
224 if ((ret = ff_bsf_config_links(filt)))
225 return ret;
226 }
227 }
228
229 return 0;
230 }
231
232 static int graph_config_pointers(AVBitStreamFilterGraph *graph, void *log_ctx)
233 {
234 unsigned i, j;
235 int sink_links_count = 0, source_links_count = 0, n = 0;
236 AVBitStreamFilterContext *f;
237 BitStreamFilterLinkInternal **sinks, **sources;
238
239 for (i = 0; i < graph->nb_filters; i++) {
240 f = graph->filters[i];
241 for (j = 0; j < f->nb_inputs; j++) {
242 ff_link_internal(f->inputs[j])->age_index = -1;
243 }
244 for (j = 0; j < f->nb_outputs; j++) {
245 ff_link_internal(f->outputs[j])->age_index = -1;
246 }
247 if (!f->nb_outputs) {
248 if (f->nb_inputs > INT_MAX - sink_links_count)
249 return AVERROR(EINVAL);
250 sink_links_count += f->nb_inputs;
251 }
252 if (!f->nb_inputs && !strcmp(f->filter->name, "source")) {
253 if (f->nb_outputs > INT_MAX - source_links_count)
254 return AVERROR(EINVAL);
255 source_links_count += f->nb_outputs;
256 }
257 }
258 sinks = av_calloc(sink_links_count, sizeof(*sinks));
259 if (!sinks)
260 return AVERROR(ENOMEM);
261 for (i = 0; i < graph->nb_filters; i++) {
262 f = graph->filters[i];
263 if (!f->nb_outputs) {
264 for (j = 0; j < f->nb_inputs; j++) {
265 sinks[n] = ff_link_internal(f->inputs[j]);
266 sinks[n]->age_index = n;
267 n++;
268 }
269 }
270 }
271 av_assert0(n == sink_links_count);
272 ffbsffiltergraph(graph)->sink_links = sinks;
273 ffbsffiltergraph(graph)->sink_links_count = sink_links_count;
274
275 sources = av_calloc(source_links_count, sizeof(*sources));
276 if (!sources)
277 return AVERROR(ENOMEM);
278 for (i = 0, n = 0; i < graph->nb_filters; i++) {
279 f = graph->filters[i];
280 if (!f->nb_inputs && !strcmp(f->filter->name, "source")) {
281 for (j = 0; j < f->nb_outputs; j++) {
282 sources[n] = ff_link_internal(f->outputs[j]);
283 n++;
284 }
285 }
286 }
287 av_assert0(n == source_links_count);
288 ffbsffiltergraph(graph)->source_links = sources;
289 ffbsffiltergraph(graph)->source_links_count = source_links_count;
290
291 return 0;
292 }
293
294 int av_bsf_graph_config(AVBitStreamFilterGraph *graphctx, void *log_ctx)
295 {
296 int ret;
297
298 if (graphctx->max_buffered_packets)
299 ffbsffiltergraph(graphctx)->max_packet_queue = graphctx->max_buffered_packets;
300 if ((ret = graph_check_validity(graphctx, log_ctx)))
301 return ret;
302 if ((ret = graph_config_links(graphctx, log_ctx)))
303 return ret;
304 if ((ret = graph_config_pointers(graphctx, log_ctx)))
305 return ret;
306
307 return 0;
308 }
309
310 static void heap_bubble_up(FFBitStreamFilterGraph *graph,
311 BitStreamFilterLinkInternal *li, int index)
312 {
313 BitStreamFilterLinkInternal **links = graph->sink_links;
314
315 av_assert0(index >= 0);
316
317 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 links[index] = li;
326 li->age_index = index;
327 }
328
329 static void heap_bubble_down(FFBitStreamFilterGraph *graph,
330 BitStreamFilterLinkInternal *li, int index)
331 {
332 BitStreamFilterLinkInternal **links = graph->sink_links;
333
334 av_assert0(index >= 0);
335
336 while (1) {
337 int child = 2 * index + 1;
338 if (child >= graph->sink_links_count)
339 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 links[index] = li;
350 li->age_index = index;
351 }
352
353 void ff_bsf_graph_update_heap(AVBitStreamFilterGraph *graph, BitStreamFilterLinkInternal *li)
354 {
355 FFBitStreamFilterGraph *graphi = ffbsffiltergraph(graph);
356
357 heap_bubble_up (graphi, li, li->age_index);
358 heap_bubble_down(graphi, li, li->age_index);
359 }
360
361 int ff_bsf_graph_run_once(AVBitStreamFilterGraph *graph)
362 {
363 FFBitStreamFilterContext *ctxi;
364 unsigned i;
365
366 av_assert0(graph->nb_filters);
367 ctxi = ffbsfctx(graph->filters[0]);
368 for (i = 1; i < graph->nb_filters; i++) {
369 FFBitStreamFilterContext *ctxi_other = ffbsfctx(graph->filters[i]);
370
371 if (ctxi_other->ready > ctxi->ready)
372 ctxi = ctxi_other;
373 }
374
375 if (!ctxi->ready)
376 return AVERROR(EAGAIN);
377
378 ctxi->ready = 0;
379
380 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