FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavdevice/lavfi.c
Date: 2022-12-05 03:11:11
Exec Total Coverage
Lines: 193 250 77.2%
Functions: 6 6 100.0%
Branches: 97 146 66.4%

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/avio_internal.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 41 av_cold static int lavfi_read_close(AVFormatContext *avctx)
62 {
63 41 LavfiContext *lavfi = avctx->priv_data;
64
65 41 av_freep(&lavfi->sink_stream_map);
66 41 av_freep(&lavfi->sink_eof);
67 41 av_freep(&lavfi->stream_sink_map);
68 41 av_freep(&lavfi->sink_stream_subcc_map);
69 41 av_freep(&lavfi->sinks);
70 41 avfilter_graph_free(&lavfi->graph);
71
72 41 return 0;
73 }
74
75 41 static int create_subcc_streams(AVFormatContext *avctx)
76 {
77 41 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 43 times.
✓ Branch 1 taken 41 times.
84 for (stream_idx = 0; stream_idx < lavfi->nb_sinks; stream_idx++) {
83 43 sink_idx = lavfi->stream_sink_map[stream_idx];
84
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39 times.
43 if (lavfi->sink_stream_subcc_map[sink_idx]) {
85 4 lavfi->sink_stream_subcc_map[sink_idx] = avctx->nb_streams;
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!(st = avformat_new_stream(avctx, NULL)))
87 return AVERROR(ENOMEM);
88 4 st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
89 4 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
90 4 time_base = &avctx->streams[stream_idx]->time_base;
91 4 st->time_base.num = time_base->num;
92 4 st->time_base.den = time_base->den;
93 } else {
94 39 lavfi->sink_stream_subcc_map[sink_idx] = -1;
95 }
96 }
97 41 return 0;
98 }
99
100 41 av_cold static int lavfi_read_header(AVFormatContext *avctx)
101 {
102 41 LavfiContext *lavfi = avctx->priv_data;
103 41 AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
104 const AVFilter *buffersink, *abuffersink;
105 enum AVMediaType type;
106 41 int ret = 0, i, n;
107
108 #define FAIL(ERR) { ret = ERR; goto end; }
109
110 41 buffersink = avfilter_get_by_name("buffersink");
111 41 abuffersink = avfilter_get_by_name("abuffersink");
112
113
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
41 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 40 times.
41 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 39 times.
✓ Branch 1 taken 2 times.
41 if (!lavfi->graph_str)
141 39 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 41 times.
41 if (!(lavfi->graph = avfilter_graph_alloc()))
145 FAIL(AVERROR(ENOMEM));
146
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 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 41 times.
41 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 43 times.
✓ Branch 1 taken 41 times.
84 for (n = 0, inout = output_links; inout; n++, inout = inout->next);
159 41 lavfi->nb_sinks = n;
160
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if (!(lavfi->sink_stream_map = av_malloc(sizeof(int) * n)))
162 FAIL(AVERROR(ENOMEM));
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if (!(lavfi->sink_eof = av_mallocz(sizeof(int) * n)))
164 FAIL(AVERROR(ENOMEM));
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if (!(lavfi->stream_sink_map = av_malloc(sizeof(int) * n)))
166 FAIL(AVERROR(ENOMEM));
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if (!(lavfi->sink_stream_subcc_map = av_malloc(sizeof(int) * n)))
168 FAIL(AVERROR(ENOMEM));
169
170
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 41 times.
84 for (i = 0; i < n; i++)
171 43 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 43 times.
✓ Branch 1 taken 41 times.
84 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
176 43 int stream_idx = 0, suffix = 0, use_subcc = 0;
177 43 sscanf(inout->name, "out%n%d%n", &suffix, &stream_idx, &suffix);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (!suffix) {
179 av_log(avctx, AV_LOG_ERROR,
180 "Invalid outpad name '%s'\n", inout->name);
181 FAIL(AVERROR(EINVAL));
182 }
183
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39 times.
43 if (inout->name[suffix]) {
184
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!strcmp(inout->name + suffix, "+subcc")) {
185 4 use_subcc = 1;
186 } else {
187 av_log(avctx, AV_LOG_ERROR,
188 "Invalid outpad suffix '%s'\n", inout->name);
189 FAIL(AVERROR(EINVAL));
190 }
191 }
192
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if ((unsigned)stream_idx >= n) {
194 av_log(avctx, AV_LOG_ERROR,
195 "Invalid index was specified in output '%s', "
196 "must be a non-negative value < %d\n",
197 inout->name, n);
198 FAIL(AVERROR(EINVAL));
199 }
200
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (lavfi->stream_sink_map[stream_idx] != -1) {
202 av_log(avctx, AV_LOG_ERROR,
203 "An output with stream index %d was already specified\n",
204 stream_idx);
205 FAIL(AVERROR(EINVAL));
206 }
207 43 lavfi->sink_stream_map[i] = stream_idx;
208 43 lavfi->stream_sink_map[stream_idx] = i;
209 43 lavfi->sink_stream_subcc_map[i] = !!use_subcc;
210 }
211
212 /* for each open output create a corresponding stream */
213
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 41 times.
84 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
214 AVStream *st;
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (!(st = avformat_new_stream(avctx, NULL)))
216 FAIL(AVERROR(ENOMEM));
217 43 st->id = i;
218 }
219
220 /* create a sink for each output and connect them to the graph */
221 41 lavfi->sinks = av_malloc_array(lavfi->nb_sinks, sizeof(AVFilterContext *));
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (!lavfi->sinks)
223 FAIL(AVERROR(ENOMEM));
224
225
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 41 times.
84 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
226 AVFilterContext *sink;
227
228 43 type = avfilter_pad_get_type(inout->filter_ctx->output_pads, inout->pad_idx);
229
230
5/6
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✓ Branch 5 taken 20 times.
43 if (type == AVMEDIA_TYPE_VIDEO && ! buffersink ||
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 type == AVMEDIA_TYPE_AUDIO && ! abuffersink) {
232 av_log(avctx, AV_LOG_ERROR, "Missing required buffersink filter, aborting.\n");
233 FAIL(AVERROR_FILTER_NOT_FOUND);
234 }
235
236
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 23 times.
43 if (type == AVMEDIA_TYPE_VIDEO) {
237 20 ret = avfilter_graph_create_filter(&sink, buffersink,
238 20 inout->name, NULL,
239 NULL, lavfi->graph);
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (ret < 0)
241 goto end;
242
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 } else if (type == AVMEDIA_TYPE_AUDIO) {
243 static const enum AVSampleFormat sample_fmts[] = {
244 AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
245 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL,
246 };
247
248 23 ret = avfilter_graph_create_filter(&sink, abuffersink,
249 23 inout->name, NULL,
250 NULL, lavfi->graph);
251
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (ret >= 0)
252 23 ret = av_opt_set_bin(sink, "sample_fmts", (const uint8_t*)sample_fmts,
253 sizeof(sample_fmts), AV_OPT_SEARCH_CHILDREN);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
255 goto end;
256 23 ret = av_opt_set_int(sink, "all_channel_counts", 1,
257 AV_OPT_SEARCH_CHILDREN);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
259 goto end;
260 } else {
261 av_log(avctx, AV_LOG_ERROR,
262 "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
263 FAIL(AVERROR(EINVAL));
264 }
265
266 43 lavfi->sinks[i] = sink;
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if ((ret = avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
268 goto end;
269 }
270
271 /* configure the graph */
272
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
41 if ((ret = avfilter_graph_config(lavfi->graph, avctx)) < 0)
273 goto end;
274
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
41 if (lavfi->dump_graph) {
276 char *dump = avfilter_graph_dump(lavfi->graph, lavfi->dump_graph);
277 if (dump != NULL) {
278 fputs(dump, stderr);
279 fflush(stderr);
280 av_free(dump);
281 } else {
282 FAIL(AVERROR(ENOMEM));
283 }
284 }
285
286 /* fill each stream with the information in the corresponding sink */
287
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 41 times.
84 for (i = 0; i < lavfi->nb_sinks; i++) {
288 43 AVFilterContext *sink = lavfi->sinks[lavfi->stream_sink_map[i]];
289 43 AVRational time_base = av_buffersink_get_time_base(sink);
290 43 AVRational frame_rate = av_buffersink_get_frame_rate(sink);
291 43 AVStream *st = avctx->streams[i];
292 43 AVCodecParameters *const par = st->codecpar;
293 43 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
294 43 par->codec_type = av_buffersink_get_type(sink);
295
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 23 times.
43 if (par->codec_type == AVMEDIA_TYPE_VIDEO) {
296 20 par->codec_id = AV_CODEC_ID_WRAPPED_AVFRAME;
297 20 par->format = av_buffersink_get_format(sink);
298 20 par->width = av_buffersink_get_w(sink);
299 20 par->height = av_buffersink_get_h(sink);
300 20 avctx->probesize = FFMAX(avctx->probesize, sizeof(AVFrame) * 30);
301 20 st ->sample_aspect_ratio =
302 20 par->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
303
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 if (frame_rate.num > 0 && frame_rate.den > 0) {
304 20 st->avg_frame_rate = frame_rate;
305 20 st->r_frame_rate = frame_rate;
306 }
307
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 } else if (par->codec_type == AVMEDIA_TYPE_AUDIO) {
308 23 par->sample_rate = av_buffersink_get_sample_rate(sink);
309 23 ret = av_buffersink_get_ch_layout(sink, &par->ch_layout);
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (ret < 0)
311 goto end;
312 23 par->format = av_buffersink_get_format(sink);
313 23 par->codec_id = av_get_pcm_codec(par->format, -1);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
23 if (par->codec_id == AV_CODEC_ID_NONE)
315 av_log(avctx, AV_LOG_ERROR,
316 "Could not find PCM codec for sample format %s.\n",
317 av_get_sample_fmt_name(par->format));
318 }
319 }
320
321
1/2
✓ Branch 1 taken 41 times.
✗ Branch 2 not taken.
41 if ((ret = create_subcc_streams(avctx)) < 0)
322 goto end;
323
324 41 end:
325 41 avfilter_inout_free(&input_links);
326 41 avfilter_inout_free(&output_links);
327 41 return ret;
328 }
329
330 10444 static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame,
331 int sink_idx)
332 {
333 10444 LavfiContext *lavfi = avctx->priv_data;
334 AVFrameSideData *sd;
335 int stream_idx, ret;
336
337
2/2
✓ Branch 0 taken 9837 times.
✓ Branch 1 taken 607 times.
10444 if ((stream_idx = lavfi->sink_stream_subcc_map[sink_idx]) < 0)
338 9837 return 0;
339
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 607 times.
607 if (!(sd = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC)))
340 return 0;
341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 607 times.
607 if ((ret = av_new_packet(&lavfi->subcc_packet, sd->size)) < 0)
342 return ret;
343 607 memcpy(lavfi->subcc_packet.data, sd->data, sd->size);
344 607 lavfi->subcc_packet.stream_index = stream_idx;
345 607 lavfi->subcc_packet.pts = frame->pts;
346 607 lavfi->subcc_packet.pos = frame->pkt_pos;
347 607 return 0;
348 }
349
350 1183 static void lavfi_free_frame(void *opaque, uint8_t *data)
351 {
352 1183 AVFrame *frame = (AVFrame*)data;
353 1183 av_frame_free(&frame);
354 1183 }
355
356 11088 static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
357 {
358 11088 LavfiContext *lavfi = avctx->priv_data;
359 11088 double min_pts = DBL_MAX;
360 11088 int stream_idx, min_pts_sink_idx = 0;
361 AVFrame *frame;
362 AVDictionary *frame_metadata;
363 int ret, i;
364 AVStream *st;
365
366
2/2
✓ Branch 0 taken 607 times.
✓ Branch 1 taken 10481 times.
11088 if (lavfi->subcc_packet.size) {
367 607 av_packet_move_ref(pkt, &lavfi->subcc_packet);
368 607 return pkt->size;
369 }
370
371 10481 frame = av_frame_alloc();
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10481 times.
10481 if (!frame)
373 return AVERROR(ENOMEM);
374
375 /* iterate through all the graph sinks. Select the sink with the
376 * minimum PTS */
377
2/2
✓ Branch 0 taken 10511 times.
✓ Branch 1 taken 10481 times.
20992 for (i = 0; i < lavfi->nb_sinks; i++) {
378 10511 AVRational tb = av_buffersink_get_time_base(lavfi->sinks[i]);
379 double d;
380
381
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10508 times.
10511 if (lavfi->sink_eof[i])
382 42 continue;
383
384 10508 ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame,
385 AV_BUFFERSINK_FLAG_PEEK);
386
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10469 times.
10508 if (ret == AVERROR_EOF) {
387 ff_dlog(avctx, "EOF sink_idx:%d\n", i);
388 39 lavfi->sink_eof[i] = 1;
389 39 continue;
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10469 times.
10469 } else if (ret < 0)
391 goto fail;
392 10469 d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
393 ff_dlog(avctx, "sink_idx:%d time:%f\n", i, d);
394 10469 av_frame_unref(frame);
395
396
2/2
✓ Branch 0 taken 10453 times.
✓ Branch 1 taken 16 times.
10469 if (d < min_pts) {
397 10453 min_pts = d;
398 10453 min_pts_sink_idx = i;
399 }
400 }
401
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 10444 times.
10481 if (min_pts == DBL_MAX) {
402 37 ret = AVERROR_EOF;
403 37 goto fail;
404 }
405
406 ff_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx);
407
408 10444 av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0);
409 10444 stream_idx = lavfi->sink_stream_map[min_pts_sink_idx];
410 10444 st = avctx->streams[stream_idx];
411
412
2/2
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 9261 times.
10444 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
413 1183 pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame),
414 &lavfi_free_frame, NULL, 0);
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1183 times.
1183 if (!pkt->buf) {
416 ret = AVERROR(ENOMEM);
417 goto fail;
418 }
419
420 1183 pkt->data = pkt->buf->data;
421 1183 pkt->size = pkt->buf->size;
422 1183 pkt->flags |= AV_PKT_FLAG_TRUSTED;
423
1/2
✓ Branch 0 taken 9261 times.
✗ Branch 1 not taken.
9261 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
424 9261 int size = frame->nb_samples * av_get_bytes_per_sample(frame->format) *
425 9261 frame->ch_layout.nb_channels;
426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9261 times.
9261 if ((ret = av_new_packet(pkt, size)) < 0)
427 goto fail;
428 9261 memcpy(pkt->data, frame->data[0], size);
429 }
430
431 10444 frame_metadata = frame->metadata;
432
2/2
✓ Branch 0 taken 454 times.
✓ Branch 1 taken 9990 times.
10444 if (frame_metadata) {
433 size_t size;
434 454 uint8_t *metadata = av_packet_pack_dictionary(frame_metadata, &size);
435
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 454 times.
454 if (!metadata) {
437 ret = AVERROR(ENOMEM);
438 goto fail;
439 }
440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 454 times.
454 if ((ret = av_packet_add_side_data(pkt, AV_PKT_DATA_STRINGS_METADATA,
441 metadata, size)) < 0) {
442 av_freep(&metadata);
443 goto fail;
444 }
445 }
446
447
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10444 times.
10444 if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) {
448 goto fail;
449 }
450
451 10444 pkt->stream_index = stream_idx;
452 10444 pkt->pts = frame->pts;
453 10444 pkt->pos = frame->pkt_pos;
454
455
2/2
✓ Branch 0 taken 9261 times.
✓ Branch 1 taken 1183 times.
10444 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
456 9261 av_frame_free(&frame);
457
458 10444 return pkt->size;
459 37 fail:
460 37 av_frame_free(&frame);
461 37 return ret;
462
463 }
464
465 #define OFFSET(x) offsetof(LavfiContext, x)
466
467 #define DEC AV_OPT_FLAG_DECODING_PARAM
468
469 static const AVOption options[] = {
470 { "graph", "set libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
471 { "graph_file","set libavfilter graph filename", OFFSET(graph_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
472 { "dumpgraph", "dump graph to stderr", OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
473 { NULL },
474 };
475
476 static const AVClass lavfi_class = {
477 .class_name = "lavfi indev",
478 .item_name = av_default_item_name,
479 .option = options,
480 .version = LIBAVUTIL_VERSION_INT,
481 .category = AV_CLASS_CATEGORY_DEVICE_INPUT,
482 };
483
484 const AVInputFormat ff_lavfi_demuxer = {
485 .name = "lavfi",
486 .long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"),
487 .priv_data_size = sizeof(LavfiContext),
488 .read_header = lavfi_read_header,
489 .read_packet = lavfi_read_packet,
490 .read_close = lavfi_read_close,
491 .flags = AVFMT_NOFILE,
492 .priv_class = &lavfi_class,
493 .flags_internal = FF_FMT_INIT_CLEANUP,
494 };
495