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
43
45 AVClass *
class;
///< class for private options
59
61 {
62 int i, j, *fmts,
count = 0;
63
64 for (i = 0; i <
n; i++) {
67 count++;
68 }
69
70 if (!(fmts =
av_malloc((count+1) *
sizeof(
int))))
72 for (j = 0, i = 0; i <
n; i++) {
75 fmts[j++] = i;
76 }
77 fmts[j] = -1;
78 return fmts;
79 }
80
82 {
84
92
93 return 0;
94 }
95
97 {
100 int stream_idx, sink_idx;
101
102 for (stream_idx = 0; stream_idx < lavfi->
nb_sinks; stream_idx++) {
110 } else {
112 }
113 }
114 return 0;
115 }
116
118 {
125
126 #define FAIL(ERR) { ret = ERR; goto end; }
127
128 if (!pix_fmts)
130
132
135
138 "Only one of the graph or graph_file options must be specified\n");
140 }
141
143 AVBPrint graph_file_pb;
146 if (ret < 0)
154 if (ret) {
157 }
160 }
161
164
165 /* parse the graph, create a stream for each open output */
168
170 &input_links, &output_links, avctx)) < 0)
172
173 if (input_links) {
175 "Open inputs in the filtergraph are not acceptable\n");
177 }
178
179 /* count the outputs */
180 for (n = 0, inout = output_links; inout; n++, inout = inout->next);
182
191
192 for (i = 0; i <
n; i++)
194
195 /* parse the output link names - they need to be of the form out0, out1, ...
196 * create a mapping between them and the streams */
197 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
198 int stream_idx = 0, suffix = 0, use_subcc = 0;
199 sscanf(inout->name, "out%n%d%n", &suffix, &stream_idx, &suffix);
200 if (!suffix) {
202 "Invalid outpad name '%s'\n", inout->name);
204 }
205 if (inout->name[suffix]) {
206 if (!strcmp(inout->name + suffix, "+subcc")) {
207 use_subcc = 1;
208 } else {
210 "Invalid outpad suffix '%s'\n", inout->name);
212 }
213 }
214
215 if ((unsigned)stream_idx >= n) {
217 "Invalid index was specified in output '%s', "
218 "must be a non-negative value < %d\n",
219 inout->name, n);
221 }
222
225 "An output with stream index %d was already specified\n",
226 stream_idx);
228 }
232 }
233
234 /* for each open output create a corresponding stream */
235 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
240 }
241
242 /* create a sink for each output and connect them to the graph */
246
247 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
249
251
256 }
257
262 if (ret >= 0)
264 if (ret < 0)
272
276 if (ret >= 0)
278 if (ret < 0)
282 if (ret < 0)
284 } else {
286 "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
288 }
289
290 lavfi->
sinks[i] = sink;
291 if ((ret =
avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
293 }
294
295 /* configure the graph */
298
301 fputs(dump, stderr);
302 fflush(stderr);
304 }
305
306 /* fill each stream with the information in the corresponding sink */
307 for (i = 0; i < lavfi->
nb_sinks; i++) {
323 30);
333 "Could not find PCM codec for sample format %s.\n",
335 }
336 }
337
340
343
348 if (ret < 0)
351 }
352
354 int sink_idx)
355 {
358 int stream_idx, i,
ret;
359
361 return 0;
364 break;
366 return 0;
374 return 0;
375 }
376
378 {
380 double min_pts = DBL_MAX;
381 int stream_idx, min_pts_sink_idx = 0;
387
394 }
395
396 /* iterate through all the graph sinks. Select the sink with the
397 * minimum PTS */
398 for (i = 0; i < lavfi->
nb_sinks; i++) {
400 double d;
402
404 continue;
405
409 av_dlog(avctx,
"EOF sink_idx:%d\n", i);
411 continue;
412 } else if (ret < 0)
415 av_dlog(avctx,
"sink_idx:%d time:%f\n", i, d);
417
418 if (d < min_pts) {
419 min_pts = d;
420 min_pts_sink_idx = i;
421 }
422 }
423 if (min_pts == DBL_MAX)
425
426 av_dlog(avctx,
"min_pts_sink_idx:%i\n", min_pts_sink_idx);
427
430
431 if (frame->
width /* FIXME best way of testing a video */) {
435
436 memcpy(pict.
data, frame->
data, 4*
sizeof(frame->
data[0]));
438
446 memcpy(pkt->
data, frame->
data[0], size);
447 }
448
450 if (frame_metadata) {
453 AVBPrint meta_buf;
454
461 }
464 meta_buf.len))) {
467 }
468 memcpy(metadata, meta_buf.str, meta_buf.len);
470 }
471
476 }
477
484 }
485
486 #define OFFSET(x) offsetof(LavfiContext, x)
487
488 #define DEC AV_OPT_FLAG_DECODING_PARAM
489
495 };
496
503 };
504
513 .priv_class = &lavfi_class,
514 };