FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavdevice/lavfi.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 195 254 76.8%
Functions: 6 6 100.0%
Branches: 96 146 65.8%

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 ret = avfilter_graph_create_filter(&sink, abuffersink,
253 24 inout->name, NULL,
254 NULL, lavfi->graph);
255
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (ret >= 0)
256 24 ret = av_opt_set_bin(sink, "sample_fmts", (const uint8_t*)sample_fmts,
257 sizeof(sample_fmts), AV_OPT_SEARCH_CHILDREN);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
259 goto end;
260 24 ret = av_opt_set_int(sink, "all_channel_counts", 1,
261 AV_OPT_SEARCH_CHILDREN);
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
263 goto end;
264 } else {
265 av_log(avctx, AV_LOG_ERROR,
266 "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
267 FAIL(AVERROR(EINVAL));
268 }
269
270 45 lavfi->sinks[i] = sink;
271
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)
272 goto end;
273 }
274
275 /* configure the graph */
276
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
277 goto end;
278
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (lavfi->dump_graph) {
280 char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph);
281 if (dump != NULL) {
282 fputs(dump, stderr);
283 fflush(stderr);
284 av_free(dump);
285 } else {
286 FAIL(AVERROR(ENOMEM));
287 }
288 }
289
290 /* fill each stream with the information in the corresponding sink */
291
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 43 times.
88 for (i = 0; i < lavfi->nb_sinks; i++) {
292 45 AVFilterContext *sink = lavfi->sinks[lavfi->stream_sink_map[i]];
293 45 AVRational time_base = av_buffersink_get_time_base(sink);
294 45 AVRational frame_rate = av_buffersink_get_frame_rate(sink);
295 45 AVStream *st = avctx->streams[i];
296 45 AVCodecParameters *const par = st->codecpar;
297 45 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
298 45 par->codec_type = av_buffersink_get_type(sink);
299
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 24 times.
45 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
300 21 par->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME;
301 21 par->format = av_buffersink_get_format(sink);
302 21 par->width = av_buffersink_get_w(sink);
303 21 par->height = av_buffersink_get_h(sink);
304 21 avctx->probesize = FFMAX(avctx->probesize, sizeof(AVFrame) * 30);
305 21 st ->sample_aspect_ratio =
306 21 par->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
307
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) {
308 21 st->avg_frame_rate = frame_rate;
309 21 st->r_frame_rate = frame_rate;
310 }
311
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
312 24 par->sample_rate = av_buffersink_get_sample_rate(sink);
313 24 ret = av_buffersink_get_ch_layout(sink, &par->ch_layout);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0)
315 goto end;
316 24 par->format = av_buffersink_get_format(sink);
317 24 par->codec_id = av_get_pcm_codec(par->format, -1);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (par->codec_id == AV_CODEC_ID_NONE)
319 av_log(avctx, AV_LOG_ERROR,
320 "Could not find PCM codec for sample format %s.\n",
321 av_get_sample_fmt_name(par->format));
322 }
323 }
324
325
1/2
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
43 if ((ret = create_subcc_streams(avctx)) < 0)
326 goto end;
327
328 43 end:
329 43 avfilter_inout_free(&input_links);
330 43 avfilter_inout_free(&output_links);
331 43 return ret;
332 }
333
334 10659 static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame,
335 int sink_idx)
336 {
337 10659 LavfiContext *lavfi = avctx->priv_data;
338 AVFrameSideData *sd;
339 int stream_idx, ret;
340
341
2/2
✓ Branch 0 taken 9917 times.
✓ Branch 1 taken 742 times.
10659 if ((stream_idx = lavfi->sink_stream_subcc_map[sink_idx]) < 0)
342 9917 return 0;
343
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)))
344 return 0;
345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 742 times.
742 if ((ret = av_new_packet(&lavfi->subcc_packet, sd->size)) < 0)
346 return ret;
347 742 memcpy(lavfi->subcc_packet.data, sd->data, sd->size);
348 742 lavfi->subcc_packet.stream_index = stream_idx;
349 742 lavfi->subcc_packet.pts = frame->pts;
350 #if FF_API_FRAME_PKT
351 FF_DISABLE_DEPRECATION_WARNINGS
352 742 lavfi->subcc_packet.pos = frame->pkt_pos;
353 FF_ENABLE_DEPRECATION_WARNINGS
354 #endif
355 742 return 0;
356 }
357
358 1333 static void lavfi_free_frame(void *opaque, uint8_t *data)
359 {
360 1333 AVFrame *frame = (AVFrame*)data;
361 1333 av_frame_free(&frame);
362 1333 }
363
364 11440 static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
365 {
366 11440 LavfiContext *lavfi = avctx->priv_data;
367 11440 double min_pts = DBL_MAX;
368 11440 int stream_idx, min_pts_sink_idx = 0;
369 AVFrame *frame, *frame_to_free;
370 AVDictionary *frame_metadata;
371 int ret, i;
372 AVStream *st;
373
374
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 10698 times.
11440 if (lavfi->subcc_packet.size) {
375 742 av_packet_move_ref(pkt, &lavfi->subcc_packet);
376 742 return pkt->size;
377 }
378
379 10698 frame = av_frame_alloc();
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10698 times.
10698 if (!frame)
381 return AVERROR(ENOMEM);
382 10698 frame_to_free = frame;
383
384 /* iterate through all the graph sinks. Select the sink with the
385 * minimum PTS */
386
2/2
✓ Branch 0 taken 10728 times.
✓ Branch 1 taken 10698 times.
21426 for (i = 0; i < lavfi->nb_sinks; i++) {
387 10728 AVRational tb = av_buffersink_get_time_base(lavfi->sinks[i]);
388 double d;
389
390
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10725 times.
10728 if (lavfi->sink_eof[i])
391 44 continue;
392
393 10725 ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame,
394 AV_BUFFERSINK_FLAG_PEEK);
395
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 10684 times.
10725 if (ret == AVERROR_EOF) {
396 ff_dlog(avctx, "EOF sink_idx:%d\n", i);
397 41 lavfi->sink_eof[i] = 1;
398 41 continue;
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10684 times.
10684 } else if (ret < 0)
400 goto fail;
401 10684 d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
402 ff_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
403 10684 av_frame_unref(frame);
404
405
2/2
✓ Branch 0 taken 10665 times.
✓ Branch 1 taken 19 times.
10684 if (d < min_pts) {
406 10665 min_pts = d;
407 10665 min_pts_sink_idx = i;
408 }
409 }
410
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10659 times.
10698 if (min_pts == DBL_MAX) {
411 39 ret = AVERROR_EOF;
412 39 goto fail;
413 }
414
415 ff_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
416
417 10659 av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0);
418 10659 stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
419 10659 st = avctx->streams[stream_idx];
420
421
2/2
✓ Branch 0 taken 1333 times.
✓ Branch 1 taken 9326 times.
10659 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
422 1333 pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
423 &lavfi_free_frame, NULL, 0);
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1333 times.
1333 if (!pkt->buf) {
425 ret = AVERROR(ENOMEM);
426 goto fail;
427 }
428 1333 frame_to_free = NULL;
429
430 1333 pkt->data = pkt->buf->data;
431 1333 pkt->size = pkt->buf->size;
432 1333 pkt->flags |= AV_PKT_FLAG_TRUSTED;
433
1/2
✓ Branch 0 taken 9326 times.
✗ Branch 1 not taken.
9326 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
434 9326 int size = frame->nb_samples * av_get_bytes_per_sample(frame->format) *
435 9326 frame->ch_layout.nb_channels;
436
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9326 times.
9326 if ((ret = av_new_packet(pkt, size)) < 0)
437 goto fail;
438 9326 memcpy(pkt->data, frame->data[0], size);
439 }
440
441 10659 frame_metadata = frame->metadata;
442
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 10195 times.
10659 if (frame_metadata) {
443 size_t size;
444 464 uint8_t *metadata = av_packet_pack_dictionary(frame_metadata, &size);
445
446
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 464 times.
464 if (!metadata) {
447 ret = AVERROR(ENOMEM);
448 goto fail;
449 }
450
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,
451 metadata, size)) < 0) {
452 av_freep(&metadata);
453 goto fail;
454 }
455 }
456
457
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10659 times.
10659 if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) {
458 goto fail;
459 }
460
461 10659 pkt->stream_index = stream_idx;
462 10659 pkt->pts = frame->pts;
463 #if FF_API_FRAME_PKT
464 FF_DISABLE_DEPRECATION_WARNINGS
465 10659 pkt->pos = frame->pkt_pos;
466 FF_ENABLE_DEPRECATION_WARNINGS
467 #endif
468
469 10659 av_frame_free(&frame_to_free);
470
471 10659 return pkt->size;
472 39 fail:
473 39 av_frame_free(&frame_to_free);
474 39 return ret;
475
476 }
477
478 #define OFFSET(x) offsetof(LavfiContext, x)
479
480 #define DEC AV_OPT_FLAG_DECODING_PARAM
481
482 static const AVOption options[] = {
483 { "graph", "set libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
484 { "graph_file","set libavfilter graph filename", OFFSET(graph_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
485 { "dumpgraph", "dump graph to stderr", OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
486 { NULL },
487 };
488
489 static const AVClass lavfi_class = {
490 .class_name = "lavfi indev",
491 .item_name = av_default_item_name,
492 .option = options,
493 .version = LIBAVUTIL_VERSION_INT,
494 .category = AV_CLASS_CATEGORY_DEVICE_INPUT,
495 };
496
497 const FFInputFormat ff_lavfi_demuxer = {
498 .p.name = "lavfi",
499 .p.long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
500 .p.flags = AVFMT_NOFILE,
501 .p.priv_class = &lavfi_class,
502 .priv_data_size = sizeof(LavfiContext),
503 .read_header = lavfi_read_header,
504 .read_packet = lavfi_read_packet,
505 .read_close = lavfi_read_close,
506 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
507 };
508