FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavdevice/lavfi.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 194 256 75.8%
Functions: 6 6 100.0%
Branches: 97 148 65.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2011 Stefano Sabatini
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 * libavfilter virtual input device
24 */
25
26 /* #define DEBUG */
27
28 #include <float.h> /* DBL_MIN, DBL_MAX */
29
30 #include "libavutil/bprint.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/file.h"
33 #include "libavutil/imgutils.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/log.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/parseutils.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavfilter/avfilter.h"
41 #include "libavfilter/buffersink.h"
42 #include "libavformat/demux.h"
43 #include "libavformat/internal.h"
44 #include "avdevice.h"
45
46 typedef struct {
47 AVClass *class; ///< class for private options
48 char *graph_str;
49 char *graph_filename;
50 char *dump_graph;
51 AVFilterGraph *graph;
52 AVFilterContext **sinks;
53 int *sink_stream_map;
54 int *sink_eof;
55 int *stream_sink_map;
56 int *sink_stream_subcc_map;
57 int nb_sinks;
58 AVPacket subcc_packet;
59 } LavfiContext;
60
61 43 av_cold static int lavfi_read_close(AVFormatContext *avctx)
62 {
63 43 LavfiContext *lavfi = avctx->priv_data;
64
65 43 av_freep(&lavfi->sink_stream_map);
66 43 av_freep(&lavfi->sink_eof);
67 43 av_freep(&lavfi->stream_sink_map);
68 43 av_freep(&lavfi->sink_stream_subcc_map);
69 43 av_freep(&lavfi->sinks);
70 43 avfilter_graph_free(&lavfi->graph);
71
72 43 return 0;
73 }
74
75 43 static int create_subcc_streams(AVFormatContext *avctx)
76 {
77 43 LavfiContext *lavfi = avctx->priv_data;
78 AVStream *st;
79 int stream_idx, sink_idx;
80 AVRational *time_base;
81
82
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (stream_idx = 0; stream_idx < lavfi->nb_sinks; stream_idx++) {
83 45 sink_idx = lavfi->stream_sink_map[stream_idx];
84
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 40 times.
45 if (lavfi->sink_stream_subcc_map[sink_idx]) {
85 5 lavfi->sink_stream_subcc_map[sink_idx] = avctx->nb_streams;
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (!(st = avformat_new_stream(avctx, NULL)))
87 return AVERROR(ENOMEM);
88 5 st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
89 5 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
90 5 time_base = &avctx->streams[stream_idx]->time_base;
91 5 st->time_base.num = time_base->num;
92 5 st->time_base.den = time_base->den;
93 } else {
94 40 lavfi->sink_stream_subcc_map[sink_idx] = -1;
95 }
96 }
97 43 return 0;
98 }
99
100 43 av_cold static int lavfi_read_header(AVFormatContext *avctx)
101 {
102 43 LavfiContext *lavfi = avctx->priv_data;
103 43 AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
104 const AVFilter *buffersink, *abuffersink;
105 enum AVMediaType type;
106 43 int ret = 0, i, n;
107
108 #define FAIL(ERR) { ret = ERR; goto end; }
109
110 43 buffersink = avfilter_get_by_name("buffersink");
111 43 abuffersink = avfilter_get_by_name("abuffersink");
112
113
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
43 if (lavfi->graph_filename && lavfi->graph_str) {
114 av_log(avctx, AV_LOG_ERROR,
115 "Only one of the graph or graph_file options must be specified\n");
116 FAIL(AVERROR(EINVAL));
117 }
118
119
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 42 times.
43 if (lavfi->graph_filename) {
120 AVBPrint graph_file_pb;
121 1 AVIOContext *avio = NULL;
122 1 AVDictionary *options = NULL;
123
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 if (avctx->protocol_whitelist && (ret = av_dict_set(&options, "protocol_whitelist", avctx->protocol_whitelist, 0)) < 0)
124 goto end;
125 1 ret = avio_open2(&avio, lavfi->graph_filename, AVIO_FLAG_READ, &avctx->interrupt_callback, &options);
126 1 av_dict_free(&options);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
128 goto end;
129 1 av_bprint_init(&graph_file_pb, 0, AV_BPRINT_SIZE_UNLIMITED);
130 1 ret = avio_read_to_bprint(avio, &graph_file_pb, INT_MAX);
131 1 avio_closep(&avio);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret) {
133 av_bprint_finalize(&graph_file_pb, NULL);
134 goto end;
135 }
136
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = av_bprint_finalize(&graph_file_pb, &lavfi->graph_str)))
137 goto end;
138 }
139
140
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 2 times.
43 if (!lavfi->graph_str)
141 41 lavfi->graph_str = av_strdup(avctx->url);
142
143 /* parse the graph, create a stream for each open output */
144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (!(lavfi->graph = avfilter_graph_alloc()))
145 FAIL(AVERROR(ENOMEM));
146
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if ((ret = avfilter_graph_parse_ptr(lavfi->graph, lavfi->graph_str,
148 &input_links, &output_links, avctx)) < 0)
149 goto end;
150
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (input_links) {
152 av_log(avctx, AV_LOG_ERROR,
153 "Open inputs in the filtergraph are not acceptable\n");
154 FAIL(AVERROR(EINVAL));
155 }
156
157 /* count the outputs */
158
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (n = 0, inout = output_links; inout; n++, inout = inout->next);
159 43 lavfi->nb_sinks = n;
160
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
162 FAIL(AVERROR(ENOMEM));
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (!(lavfi->sink_eof = av_mallocz(sizeof(int) * n)))
164 FAIL(AVERROR(ENOMEM));
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
166 FAIL(AVERROR(ENOMEM));
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (!(lavfi->sink_stream_subcc_map = av_malloc(sizeof(int) * n)))
168 FAIL(AVERROR(ENOMEM));
169
170
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (i = 0; i < n; i++)
171 45 lavfi->stream_sink_map[i] = -1;
172
173 /* parse the output link names - they need to be of the form out0, out1, ...
174 * create a mapping between them and the streams */
175
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
176 45 int stream_idx = 0, suffix = 0, use_subcc = 0;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (!inout->name) {
178 av_log(avctx, AV_LOG_ERROR, "Missing %d outpad name\n", i);
179 FAIL(AVERROR(EINVAL));
180 }
181 45 sscanf(inout->name, "out%n%d%n", &suffix, &stream_idx, &suffix);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (!suffix) {
183 av_log(avctx, AV_LOG_ERROR,
184 "Invalid outpad name '%s'\n", inout->name);
185 FAIL(AVERROR(EINVAL));
186 }
187
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 40 times.
45 if (inout->name[suffix]) {
188
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!strcmp(inout->name + suffix, "+subcc")) {
189 5 use_subcc = 1;
190 } else {
191 av_log(avctx, AV_LOG_ERROR,
192 "Invalid outpad suffix '%s'\n", inout->name);
193 FAIL(AVERROR(EINVAL));
194 }
195 }
196
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if ((unsigned)stream_idx >= n) {
198 av_log(avctx, AV_LOG_ERROR,
199 "Invalid index was specified in output '%s', "
200 "must be a non-negative value < %d\n",
201 inout->name, n);
202 FAIL(AVERROR(EINVAL));
203 }
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (lavfi->stream_sink_map[stream_idx] != -1) {
206 av_log(avctx, AV_LOG_ERROR,
207 "An output with stream index %d was already specified\n",
208 stream_idx);
209 FAIL(AVERROR(EINVAL));
210 }
211 45 lavfi->sink_stream_map[i] = stream_idx;
212 45 lavfi->stream_sink_map[stream_idx] = i;
213 45 lavfi->sink_stream_subcc_map[i] = !!use_subcc;
214 }
215
216 /* for each open output create a corresponding stream */
217
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
218 AVStream *st;
219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
45 if (!(st = avformat_new_stream(avctx, NULL)))
220 FAIL(AVERROR(ENOMEM));
221 45 st->id = i;
222 }
223
224 /* create a sink for each output and connect them to the graph */
225 43 lavfi->sinks = av_malloc_array(lavfi->nb_sinks, sizeof(AVFilterContext *));
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (!lavfi->sinks)
227 FAIL(AVERROR(ENOMEM));
228
229
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
230 AVFilterContext *sink;
231
232 45 type = avfilter_pad_get_type(inout->filter_ctx->output_pads, inout->pad_idx);
233
234
5/6
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 21 times.
45 if (type == AVMEDIA_TYPE_VIDEO && ! buffersink ||
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 type == AVMEDIA_TYPE_AUDIO && ! abuffersink) {
236 av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n");
237 FAIL(AVERROR_FILTER_NOT_FOUND);
238 }
239
240
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 24 times.
45 if (type == AVMEDIA_TYPE_VIDEO) {
241 21 ret = avfilter_graph_create_filter(&sink, buffersink,
242 21 inout->name, NULL,
243 NULL, lavfi->graph);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (ret < 0)
245 goto end;
246
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 } else if (type == AVMEDIA_TYPE_AUDIO) {
247 static const enum AVSampleFormat sample_fmts[] = {
248 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
249 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL,
250 };
251
252 24 sink = avfilter_graph_alloc_filter(lavfi->graph, abuffersink, inout->name);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!sink) {
254 ret = AVERROR(ENOMEM);
255 goto end;
256 }
257
258 24 ret = av_opt_set_bin(sink, "sample_fmts", (const uint8_t*)sample_fmts,
259 sizeof(sample_fmts), AV_OPT_SEARCH_CHILDREN);
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
261 goto end;
262 24 ret = av_opt_set_int(sink, "all_channel_counts", 1,
263 AV_OPT_SEARCH_CHILDREN);
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
265 goto end;
266
267 24 ret = avfilter_init_dict(sink, NULL);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
269 goto end;
270 } else {
271 av_log(avctx, AV_LOG_ERROR,
272 "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
273 FAIL(AVERROR(EINVAL));
274 }
275
276 45 lavfi->sinks[i] = sink;
277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
45 if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
278 goto end;
279 }
280
281 /* configure the graph */
282
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
283 goto end;
284
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (lavfi->dump_graph) {
286 char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph);
287 if (dump != NULL) {
288 fputs(dump, stderr);
289 fflush(stderr);
290 av_free(dump);
291 } else {
292 FAIL(AVERROR(ENOMEM));
293 }
294 }
295
296 /* fill each stream with the information in the corresponding sink */
297
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (i = 0; i < lavfi->nb_sinks; i++) {
298 45 AVFilterContext *sink = lavfi->sinks[lavfi->stream_sink_map[i]];
299 45 AVRational time_base = av_buffersink_get_time_base(sink);
300 45 AVRational frame_rate = av_buffersink_get_frame_rate(sink);
301 45 AVStream *st = avctx->streams[i];
302 45 AVCodecParameters *const par = st->codecpar;
303 45 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
304 45 par->codec_type = av_buffersink_get_type(sink);
305
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 24 times.
45 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
306 21 par->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME;
307 21 par->format = av_buffersink_get_format(sink);
308 21 par->width = av_buffersink_get_w(sink);
309 21 par->height = av_buffersink_get_h(sink);
310 21 avctx->probesize = FFMAX(avctx->probesize, sizeof(AVFrame) * 30);
311 21 st ->sample_aspect_ratio =
312 21 par->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
313
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 if (frame_rate.num > 0 && frame_rate.den > 0) {
314 21 st->avg_frame_rate = frame_rate;
315 21 st->r_frame_rate = frame_rate;
316 }
317
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
318 24 par->sample_rate = av_buffersink_get_sample_rate(sink);
319 24 ret = av_buffersink_get_ch_layout(sink, &par->ch_layout);
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
321 goto end;
322 24 par->format = av_buffersink_get_format(sink);
323 24 par->codec_id = av_get_pcm_codec(par->format, -1);
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (par->codec_id == AV_CODEC_ID_NONE)
325 av_log(avctx, AV_LOG_ERROR,
326 "Could not find PCM codec for sample format %s.\n",
327 av_get_sample_fmt_name(par->format));
328 }
329 }
330
331
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 if ((ret = create_subcc_streams(avctx)) < 0)
332 goto end;
333
334 43 end:
335 43 avfilter_inout_free(&input_links);
336 43 avfilter_inout_free(&output_links);
337 43 return ret;
338 }
339
340 10642 static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame,
341 int sink_idx)
342 {
343 10642 LavfiContext *lavfi = avctx->priv_data;
344 AVFrameSideData *sd;
345 int stream_idx, ret;
346
347
2/2
✓ Branch 0 taken 9900 times.
✓ Branch 1 taken 742 times.
10642 if ((stream_idx = lavfi->sink_stream_subcc_map[sink_idx]) < 0)
348 9900 return 0;
349
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 742 times.
742 if (!(sd = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)))
350 return 0;
351
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 742 times.
742 if ((ret = av_new_packet(&lavfi->subcc_packet, sd->size)) < 0)
352 return ret;
353 742 memcpy(lavfi->subcc_packet.data, sd->data, sd->size);
354 742 lavfi->subcc_packet.stream_index = stream_idx;
355 742 lavfi->subcc_packet.pts = frame->pts;
356 742 return 0;
357 }
358
359 1330 static void lavfi_free_frame(void *opaque, uint8_t *data)
360 {
361 1330 AVFrame *frame = (AVFrame*)data;
362 1330 av_frame_free(&frame);
363 1330 }
364
365 11423 static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
366 {
367 11423 LavfiContext *lavfi = avctx->priv_data;
368 11423 double min_pts = DBL_MAX;
369 11423 int stream_idx, min_pts_sink_idx = 0;
370 AVFrame *frame, *frame_to_free;
371 AVDictionary *frame_metadata;
372 int ret, i;
373 AVStream *st;
374
375
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 10681 times.
11423 if (lavfi->subcc_packet.size) {
376 742 av_packet_move_ref(pkt, &lavfi->subcc_packet);
377 742 return pkt->size;
378 }
379
380 10681 frame = av_frame_alloc();
381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10681 times.
10681 if (!frame)
382 return AVERROR(ENOMEM);
383 10681 frame_to_free = frame;
384
385 /* iterate through all the graph sinks. Select the sink with the
386 * minimum PTS */
387
2/2
✓ Branch 0 taken 10711 times.
✓ Branch 1 taken 10681 times.
21392 for (i = 0; i < lavfi->nb_sinks; i++) {
388 10711 AVRational tb = av_buffersink_get_time_base(lavfi->sinks[i]);
389 double d;
390
391
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10708 times.
10711 if (lavfi->sink_eof[i])
392 44 continue;
393
394 10708 ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame,
395 AV_BUFFERSINK_FLAG_PEEK);
396
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 10667 times.
10708 if (ret == AVERROR_EOF) {
397 ff_dlog(avctx, "EOF sink_idx:%d\n", i);
398 41 lavfi->sink_eof[i] = 1;
399 41 continue;
400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10667 times.
10667 } else if (ret < 0)
401 goto fail;
402 10667 d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
403 ff_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
404 10667 av_frame_unref(frame);
405
406
2/2
✓ Branch 0 taken 10648 times.
✓ Branch 1 taken 19 times.
10667 if (d < min_pts) {
407 10648 min_pts = d;
408 10648 min_pts_sink_idx = i;
409 }
410 }
411
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10642 times.
10681 if (min_pts == DBL_MAX) {
412 39 ret = AVERROR_EOF;
413 39 goto fail;
414 }
415
416 ff_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
417
418 10642 av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0);
419 10642 stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
420 10642 st = avctx->streams[stream_idx];
421
422
2/2
✓ Branch 0 taken 1330 times.
✓ Branch 1 taken 9312 times.
10642 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
423 1330 pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
424 &lavfi_free_frame, NULL, 0);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1330 times.
1330 if (!pkt->buf) {
426 ret = AVERROR(ENOMEM);
427 goto fail;
428 }
429 1330 frame_to_free = NULL;
430
431 1330 pkt->data = pkt->buf->data;
432 1330 pkt->size = pkt->buf->size;
433 1330 pkt->flags |= AV_PKT_FLAG_TRUSTED;
434
1/2
✓ Branch 0 taken 9312 times.
✗ Branch 1 not taken.
9312 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
435 9312 int size = frame->nb_samples * av_get_bytes_per_sample(frame->format) *
436 9312 frame->ch_layout.nb_channels;
437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9312 times.
9312 if ((ret = av_new_packet(pkt, size)) < 0)
438 goto fail;
439 9312 memcpy(pkt->data, frame->data[0], size);
440 }
441
442 10642 frame_metadata = frame->metadata;
443
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 10178 times.
10642 if (frame_metadata) {
444 size_t size;
445 464 uint8_t *metadata = av_packet_pack_dictionary(frame_metadata, &size);
446
447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 464 times.
464 if (!metadata) {
448 ret = AVERROR(ENOMEM);
449 goto fail;
450 }
451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 464 times.
464 if ((ret = av_packet_add_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,
452 metadata, size)) < 0) {
453 av_freep(&metadata);
454 goto fail;
455 }
456 }
457
458
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10642 times.
10642 if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) {
459 goto fail;
460 }
461
462 10642 pkt->stream_index = stream_idx;
463 10642 pkt->pts = frame->pts;
464
465 10642 av_frame_free(&frame_to_free);
466
467 10642 return pkt->size;
468 39 fail:
469 39 av_frame_free(&frame_to_free);
470 39 return ret;
471
472 }
473
474 #define OFFSET(x) offsetof(LavfiContext, x)
475
476 #define DEC AV_OPT_FLAG_DECODING_PARAM
477
478 static const AVOption options[] = {
479 { "graph", "set libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
480 { "graph_file","set libavfilter graph filename", OFFSET(graph_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
481 { "dumpgraph", "dump graph to stderr", OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
482 { NULL },
483 };
484
485 static const AVClass lavfi_class = {
486 .class_name = "lavfi indev",
487 .item_name = av_default_item_name,
488 .option = options,
489 .version = LIBAVUTIL_VERSION_INT,
490 .category = AV_CLASS_CATEGORY_DEVICE_INPUT,
491 };
492
493 const FFInputFormat ff_lavfi_demuxer = {
494 .p.name = "lavfi",
495 .p.long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
496 .p.flags = AVFMT_NOFILE,
497 .p.priv_class = &lavfi_class,
498 .priv_data_size = sizeof(LavfiContext),
499 .read_header = lavfi_read_header,
500 .read_packet = lavfi_read_packet,
501 .read_close = lavfi_read_close,
502 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
503 };
504