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 | 10651 | static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame, | |
341 | int sink_idx) | ||
342 | { | ||
343 | 10651 | LavfiContext *lavfi = avctx->priv_data; | |
344 | AVFrameSideData *sd; | ||
345 | int stream_idx, ret; | ||
346 | |||
347 |
2/2✓ Branch 0 taken 9909 times.
✓ Branch 1 taken 742 times.
|
10651 | if ((stream_idx = lavfi->sink_stream_subcc_map[sink_idx]) < 0) |
348 | 9909 | 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 | #if FF_API_FRAME_PKT | ||
357 | FF_DISABLE_DEPRECATION_WARNINGS | ||
358 | 742 | lavfi->subcc_packet.pos = frame->pkt_pos; | |
359 | FF_ENABLE_DEPRECATION_WARNINGS | ||
360 | #endif | ||
361 | 742 | return 0; | |
362 | } | ||
363 | |||
364 | 1325 | static void lavfi_free_frame(void *opaque, uint8_t *data) | |
365 | { | ||
366 | 1325 | AVFrame *frame = (AVFrame*)data; | |
367 | 1325 | av_frame_free(&frame); | |
368 | 1325 | } | |
369 | |||
370 | 11432 | static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt) | |
371 | { | ||
372 | 11432 | LavfiContext *lavfi = avctx->priv_data; | |
373 | 11432 | double min_pts = DBL_MAX; | |
374 | 11432 | int stream_idx, min_pts_sink_idx = 0; | |
375 | AVFrame *frame, *frame_to_free; | ||
376 | AVDictionary *frame_metadata; | ||
377 | int ret, i; | ||
378 | AVStream *st; | ||
379 | |||
380 |
2/2✓ Branch 0 taken 742 times.
✓ Branch 1 taken 10690 times.
|
11432 | if (lavfi->subcc_packet.size) { |
381 | 742 | av_packet_move_ref(pkt, &lavfi->subcc_packet); | |
382 | 742 | return pkt->size; | |
383 | } | ||
384 | |||
385 | 10690 | frame = av_frame_alloc(); | |
386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10690 times.
|
10690 | if (!frame) |
387 | ✗ | return AVERROR(ENOMEM); | |
388 | 10690 | frame_to_free = frame; | |
389 | |||
390 | /* iterate through all the graph sinks. Select the sink with the | ||
391 | * minimum PTS */ | ||
392 |
2/2✓ Branch 0 taken 10720 times.
✓ Branch 1 taken 10690 times.
|
21410 | for (i = 0; i < lavfi->nb_sinks; i++) { |
393 | 10720 | AVRational tb = av_buffersink_get_time_base(lavfi->sinks[i]); | |
394 | double d; | ||
395 | |||
396 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10717 times.
|
10720 | if (lavfi->sink_eof[i]) |
397 | 44 | continue; | |
398 | |||
399 | 10717 | ret = av_buffersink_get_frame_flags(lavfi->sinks[i], frame, | |
400 | AV_BUFFERSINK_FLAG_PEEK); | ||
401 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 10676 times.
|
10717 | if (ret == AVERROR_EOF) { |
402 | ff_dlog(avctx, "EOF sink_idx:%d\n", i); | ||
403 | 41 | lavfi->sink_eof[i] = 1; | |
404 | 41 | continue; | |
405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10676 times.
|
10676 | } else if (ret < 0) |
406 | ✗ | goto fail; | |
407 | 10676 | d = av_rescale_q_rnd(frame->pts, tb, AV_TIME_BASE_Q, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX); | |
408 | ff_dlog(avctx, "sink_idx:%d time:%f\n", i, d); | ||
409 | 10676 | av_frame_unref(frame); | |
410 | |||
411 |
2/2✓ Branch 0 taken 10657 times.
✓ Branch 1 taken 19 times.
|
10676 | if (d < min_pts) { |
412 | 10657 | min_pts = d; | |
413 | 10657 | min_pts_sink_idx = i; | |
414 | } | ||
415 | } | ||
416 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 10651 times.
|
10690 | if (min_pts == DBL_MAX) { |
417 | 39 | ret = AVERROR_EOF; | |
418 | 39 | goto fail; | |
419 | } | ||
420 | |||
421 | ff_dlog(avctx, "min_pts_sink_idx:%i\n", min_pts_sink_idx); | ||
422 | |||
423 | 10651 | av_buffersink_get_frame_flags(lavfi->sinks[min_pts_sink_idx], frame, 0); | |
424 | 10651 | stream_idx = lavfi->sink_stream_map[min_pts_sink_idx]; | |
425 | 10651 | st = avctx->streams[stream_idx]; | |
426 | |||
427 |
2/2✓ Branch 0 taken 1325 times.
✓ Branch 1 taken 9326 times.
|
10651 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
428 | 1325 | pkt->buf = av_buffer_create((uint8_t*)frame, sizeof(*frame), | |
429 | &lavfi_free_frame, NULL, 0); | ||
430 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1325 times.
|
1325 | if (!pkt->buf) { |
431 | ✗ | ret = AVERROR(ENOMEM); | |
432 | ✗ | goto fail; | |
433 | } | ||
434 | 1325 | frame_to_free = NULL; | |
435 | |||
436 | 1325 | pkt->data = pkt->buf->data; | |
437 | 1325 | pkt->size = pkt->buf->size; | |
438 | 1325 | pkt->flags |= AV_PKT_FLAG_TRUSTED; | |
439 |
1/2✓ Branch 0 taken 9326 times.
✗ Branch 1 not taken.
|
9326 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
440 | 9326 | int size = frame->nb_samples * av_get_bytes_per_sample(frame->format) * | |
441 | 9326 | frame->ch_layout.nb_channels; | |
442 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9326 times.
|
9326 | if ((ret = av_new_packet(pkt, size)) < 0) |
443 | ✗ | goto fail; | |
444 | 9326 | memcpy(pkt->data, frame->data[0], size); | |
445 | } | ||
446 | |||
447 | 10651 | frame_metadata = frame->metadata; | |
448 |
2/2✓ Branch 0 taken 464 times.
✓ Branch 1 taken 10187 times.
|
10651 | if (frame_metadata) { |
449 | size_t size; | ||
450 | 464 | uint8_t *metadata = av_packet_pack_dictionary(frame_metadata, &size); | |
451 | |||
452 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 464 times.
|
464 | if (!metadata) { |
453 | ✗ | ret = AVERROR(ENOMEM); | |
454 | ✗ | goto fail; | |
455 | } | ||
456 |
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, |
457 | metadata, size)) < 0) { | ||
458 | ✗ | av_freep(&metadata); | |
459 | ✗ | goto fail; | |
460 | } | ||
461 | } | ||
462 | |||
463 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 10651 times.
|
10651 | if ((ret = create_subcc_packet(avctx, frame, min_pts_sink_idx)) < 0) { |
464 | ✗ | goto fail; | |
465 | } | ||
466 | |||
467 | 10651 | pkt->stream_index = stream_idx; | |
468 | 10651 | pkt->pts = frame->pts; | |
469 | #if FF_API_FRAME_PKT | ||
470 | FF_DISABLE_DEPRECATION_WARNINGS | ||
471 | 10651 | pkt->pos = frame->pkt_pos; | |
472 | FF_ENABLE_DEPRECATION_WARNINGS | ||
473 | #endif | ||
474 | |||
475 | 10651 | av_frame_free(&frame_to_free); | |
476 | |||
477 | 10651 | return pkt->size; | |
478 | 39 | fail: | |
479 | 39 | av_frame_free(&frame_to_free); | |
480 | 39 | return ret; | |
481 | |||
482 | } | ||
483 | |||
484 | #define OFFSET(x) offsetof(LavfiContext, x) | ||
485 | |||
486 | #define DEC AV_OPT_FLAG_DECODING_PARAM | ||
487 | |||
488 | static const AVOption options[] = { | ||
489 | { "graph", "set libavfilter graph", OFFSET(graph_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, | ||
490 | { "graph_file","set libavfilter graph filename", OFFSET(graph_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC}, | ||
491 | { "dumpgraph", "dump graph to stderr", OFFSET(dump_graph), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC }, | ||
492 | { NULL }, | ||
493 | }; | ||
494 | |||
495 | static const AVClass lavfi_class = { | ||
496 | .class_name = "lavfi indev", | ||
497 | .item_name = av_default_item_name, | ||
498 | .option = options, | ||
499 | .version = LIBAVUTIL_VERSION_INT, | ||
500 | .category = AV_CLASS_CATEGORY_DEVICE_INPUT, | ||
501 | }; | ||
502 | |||
503 | const FFInputFormat ff_lavfi_demuxer = { | ||
504 | .p.name = "lavfi", | ||
505 | .p.long_name = NULL_IF_CONFIG_SMALL("Libavfilter virtual input device"), | ||
506 | .p.flags = AVFMT_NOFILE, | ||
507 | .p.priv_class = &lavfi_class, | ||
508 | .priv_data_size = sizeof(LavfiContext), | ||
509 | .read_header = lavfi_read_header, | ||
510 | .read_packet = lavfi_read_packet, | ||
511 | .read_close = lavfi_read_close, | ||
512 | .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP, | ||
513 | }; | ||
514 |