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