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
56
58 {
59 int i, j, *fmts,
count = 0;
60
61 for (i = 0; i <
n; i++) {
64 count++;
65 }
66
67 if (!(fmts =
av_malloc((count+1) *
sizeof(
int))))
68 return NULL;
69 for (j = 0, i = 0; i <
n; i++) {
72 fmts[j++] = i;
73 }
74 fmts[j] = -1;
75 return fmts;
76 }
77
79 {
81
88
89 return 0;
90 }
91
93 {
95 AVFilterInOut *input_links = NULL, *output_links = NULL, *inout;
100
101 #define FAIL(ERR) { ret = ERR; goto end; }
102
103 if (!pix_fmts)
105
107
110
113 "Only one of the graph or graph_file options must be specified\n");
115 }
116
119 size_t file_bufsize;
121 &file_buf, &file_bufsize, 0, avctx);
122 if (ret < 0)
124
125 /* create a 0-terminated string based on the read file */
127 if (!graph_buf) {
130 }
131 memcpy(graph_buf, file_buf, file_bufsize);
132 graph_buf[file_bufsize] = 0;
135 }
136
139
140 /* parse the graph, create a stream for each open output */
143
145 &input_links, &output_links, avctx)) < 0)
147
148 if (input_links) {
150 "Open inputs in the filtergraph are not acceptable\n");
152 }
153
154 /* count the outputs */
155 for (n = 0, inout = output_links; inout; n++, inout = inout->next);
156
163
164 for (i = 0; i <
n; i++)
166
167 /* parse the output link names - they need to be of the form out0, out1, ...
168 * create a mapping between them and the streams */
169 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
170 int stream_idx;
171 if (!strcmp(inout->name, "out"))
172 stream_idx = 0;
173 else if (sscanf(inout->name, "out%d\n", &stream_idx) != 1) {
175 "Invalid outpad name '%s'\n", inout->name);
177 }
178
179 if ((unsigned)stream_idx >= n) {
181 "Invalid index was specified in output '%s', "
182 "must be a non-negative value < %d\n",
183 inout->name, n);
185 }
186
187 /* is an audio or video output? */
188 type = inout->filter_ctx->output_pads[inout->pad_idx].type;
191 "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
193 }
194
197 "An output with stream index %d was already specified\n",
198 stream_idx);
200 }
203 }
204
205 /* for each open output create a corresponding stream */
206 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
211 }
212
213 /* create a sink for each output and connect them to the graph */
217
218 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
220
222
227 }
228
233 if (ret >= 0)
235 if (ret < 0)
243
247 if (ret >= 0)
249 if (ret < 0)
253 if (ret < 0)
255 }
256
257 lavfi->
sinks[i] = sink;
258 if ((ret =
avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
260 }
261
262 /* configure the graph */
265
268 fputs(dump, stderr);
269 fflush(stderr);
271 }
272
273 /* fill each stream with the information in the corresponding sink */
290 30);
300 "Could not find PCM codec for sample format %s.\n",
302 }
303 }
304
307
312 if (ret < 0)
315 }
316
318 {
320 double min_pts = DBL_MAX;
321 int stream_idx, min_pts_sink_idx = 0;
327
328 /* iterate through all the graph sinks. Select the sink with the
329 * minimum PTS */
332 double d;
334
336 continue;
337
341 av_dlog(avctx,
"EOF sink_idx:%d\n", i);
343 continue;
344 } else if (ret < 0)
347 av_dlog(avctx,
"sink_idx:%d time:%f\n", i, d);
349
350 if (d < min_pts) {
351 min_pts = d;
352 min_pts_sink_idx = i;
353 }
354 }
355 if (min_pts == DBL_MAX)
357
358 av_dlog(avctx,
"min_pts_sink_idx:%i\n", min_pts_sink_idx);
359
362
363 if (frame->
width /* FIXME best way of testing a video */) {
367
368 memcpy(pict.
data, frame->
data, 4*
sizeof(frame->
data[0]));
370
378 memcpy(pkt->
data, frame->
data[0], size);
379 }
380
382 if (frame_metadata) {
386
393 }
396 meta_buf.len))) {
399 }
400 memcpy(metadata, meta_buf.str, meta_buf.len);
402 }
403
410 }
411
412 #define OFFSET(x) offsetof(LavfiContext, x)
413
414 #define DEC AV_OPT_FLAG_DECODING_PARAM
415
420 { NULL },
421 };
422
428 };
429
438 .priv_class = &lavfi_class,
439 };