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
46
48 AVClass *
class;
///< class for private options
62
64 {
65 int i, j, *fmts,
count = 0;
66
67 for (i = 0; i <
n; i++) {
70 count++;
71 }
72
73 if (!(fmts =
av_malloc((count+1) *
sizeof(
int))))
75 for (j = 0, i = 0; i <
n; i++) {
78 fmts[j++] = i;
79 }
80 fmts[j] = -1;
81 return fmts;
82 }
83
85 {
87
95
96 return 0;
97 }
98
100 {
103 int stream_idx, sink_idx;
104
105 for (stream_idx = 0; stream_idx < lavfi->
nb_sinks; stream_idx++) {
113 } else {
115 }
116 }
117 return 0;
118 }
119
121 {
128
129 #define FAIL(ERR) { ret = ERR; goto end; }
130
131 if (!pix_fmts)
133
135
138
141 "Only one of the graph or graph_file options must be specified\n");
143 }
144
146 AVBPrint graph_file_pb;
153 if (ret < 0)
161 if (ret) {
164 }
167 }
168
171
172 /* parse the graph, create a stream for each open output */
175
177 &input_links, &output_links, avctx)) < 0)
179
180 if (input_links) {
182 "Open inputs in the filtergraph are not acceptable\n");
184 }
185
186 /* count the outputs */
187 for (n = 0, inout = output_links; inout; n++, inout = inout->next);
189
198
199 for (i = 0; i <
n; i++)
201
202 /* parse the output link names - they need to be of the form out0, out1, ...
203 * create a mapping between them and the streams */
204 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
205 int stream_idx = 0,
suffix = 0, use_subcc = 0;
206 sscanf(inout->name,
"out%n%d%n", &
suffix, &stream_idx, &
suffix);
209 "Invalid outpad name '%s'\n", inout->name);
211 }
212 if (inout->name[
suffix]) {
213 if (!strcmp(inout->name +
suffix,
"+subcc")) {
214 use_subcc = 1;
215 } else {
217 "Invalid outpad suffix '%s'\n", inout->name);
219 }
220 }
221
222 if ((unsigned)stream_idx >= n) {
224 "Invalid index was specified in output '%s', "
225 "must be a non-negative value < %d\n",
226 inout->name, n);
228 }
229
232 "An output with stream index %d was already specified\n",
233 stream_idx);
235 }
239 }
240
241 /* for each open output create a corresponding stream */
242 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
247 }
248
249 /* create a sink for each output and connect them to the graph */
253
254 for (i = 0, inout = output_links; inout; i++, inout = inout->next) {
256
258
263 }
264
269 if (ret >= 0)
271 if (ret < 0)
279
283 if (ret >= 0)
285 if (ret < 0)
289 if (ret < 0)
291 } else {
293 "Output '%s' is not a video or audio output, not yet supported\n", inout->name);
295 }
296
297 lavfi->
sinks[i] = sink;
298 if ((ret =
avfilter_link(inout->filter_ctx, inout->pad_idx, sink, 0)) < 0)
300 }
301
302 /* configure the graph */
305
308 fputs(dump, stderr);
309 fflush(stderr);
311 }
312
313 /* fill each stream with the information in the corresponding sink */
314 for (i = 0; i < lavfi->
nb_sinks; i++) {
330 30);
340 "Could not find PCM codec for sample format %s.\n",
342 }
343 }
344
347
350
355 if (ret < 0)
357 return ret;
358 }
359
361 int sink_idx)
362 {
365 int stream_idx, i, ret;
366
368 return 0;
371 break;
373 return 0;
376 return ret;
381 return 0;
382 }
383
385 {
387 double min_pts = DBL_MAX;
388 int stream_idx, min_pts_sink_idx = 0;
391 int ret, i;
393
400 }
401
402 /* iterate through all the graph sinks. Select the sink with the
403 * minimum PTS */
404 for (i = 0; i < lavfi->
nb_sinks; i++) {
406 double d;
407 int ret;
408
410 continue;
411
415 ff_dlog(avctx,
"EOF sink_idx:%d\n", i);
417 continue;
418 } else if (ret < 0)
419 return ret;
421 ff_dlog(avctx,
"sink_idx:%d time:%f\n", i, d);
423
424 if (d < min_pts) {
425 min_pts = d;
426 min_pts_sink_idx = i;
427 }
428 }
429 if (min_pts == DBL_MAX)
431
432 ff_dlog(avctx,
"min_pts_sink_idx:%i\n", min_pts_sink_idx);
433
436
437 if (frame->
width /* FIXME best way of testing a video */) {
440 return ret;
441
448 return ret;
449 memcpy(pkt->
data, frame->
data[0], size);
450 }
451
453 if (frame_metadata) {
456 AVBPrint meta_buf;
457
464 }
467 meta_buf.len))) {
470 }
471 memcpy(metadata, meta_buf.str, meta_buf.len);
473 }
474
478 return ret;
479 }
480
487 }
488
489 #define OFFSET(x) offsetof(LavfiContext, x)
490
491 #define DEC AV_OPT_FLAG_DECODING_PARAM
492
498 };
499
506 };
507
516 .priv_class = &lavfi_class,
517 };
int64_t probesize
Maximum size of the data read from input for determining the input container format.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
int av_image_copy_to_buffer(uint8_t *dst, int dst_size, const uint8_t *const src_data[4], const int src_linesize[4], enum AVPixelFormat pix_fmt, int width, int height, int align)
Copy image data from an image into a buffer.
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
#define LIBAVUTIL_VERSION_INT
Main libavfilter public API header.
memory handling functions
int64_t pos
byte position in stream, -1 if unknown
static av_cold int lavfi_read_close(AVFormatContext *avctx)
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
int h
agreed upon image height
#define av_opt_set_int_list(obj, name, val, term, flags)
Set a binary option to an integer list.
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size)
Read contents of h into print buffer, up to max_size bytes, or up to EOF.
enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
Get the type of an AVFilterPad.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
#define AVIO_FLAG_READ
read-only
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
memory buffer sink API for audio and video
AVInputFormat ff_lavfi_demuxer
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
AVFilterLink ** inputs
array of pointers to input links
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
enum AVSampleFormat sample_fmt
audio sample format
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
static av_cold int end(AVCodecContext *avctx)
int id
Format-specific stream ID.
static const AVClass lavfi_class
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
A list of all streams in the file.
void avfilter_register_all(void)
Initialize the filter system.
int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt, const char *name, const char *args, void *opaque, AVFilterGraph *graph_ctx)
Create and add a filter instance into an existing graph.
Structure to hold side data for an AVFrame.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
char * protocol_whitelist
',' separated list of allowed protocols.
#define AVERROR_EOF
End of file.
static av_cold int read_close(AVFormatContext *ctx)
AVFrameSideData ** side_data
Main libavdevice API header.
A link between two filters.
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define AV_BPRINT_SIZE_UNLIMITED
static int create_subcc_streams(AVFormatContext *avctx)
int sample_rate
samples per second
int avfilter_link_get_channels(AVFilterLink *link)
Get the number of channels of a link.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
int av_get_padded_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel for the pixel format described by pixdesc, including any padding ...
ATSC A53 Part 4 Closed Captions.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
Round to nearest and halfway cases away from zero.
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags)
Get a frame with filtered data from sink and put it in frame.
uint64_t channel_layout
Audio channel layout.
AVCodecContext * codec
Codec context associated with this stream.
int w
agreed upon image width
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
common internal API header
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
static av_cold int lavfi_read_header(AVFormatContext *avctx)
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
audio channel layout utility functions
char filename[1024]
input or output filename
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
int width
picture width / height.
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
int format
agreed upon media format
static int read_header(FFV1Context *f)
A linked-list of the inputs/outputs of the filter chain.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
enum AVMediaType codec_type
A list of zero terminated key/value strings.
AVSampleFormat
Audio sample formats.
enum AVCodecID av_get_pcm_codec(enum AVSampleFormat fmt, int be)
Return the PCM codec associated with a sample format.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
char * av_strdup(const char *s)
Duplicate the string s.
int sample_rate
samples per second
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
AVDictionary * av_frame_get_metadata(const AVFrame *frame)
Describe the class of an AVClass context structure.
int av_frame_get_channels(const AVFrame *frame)
rational number numerator/denominator
static int lavfi_read_packet(AVFormatContext *avctx, AVPacket *pkt)
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
const char * name
Filter name.
AVRational sample_aspect_ratio
agreed upon sample aspect ratio
static enum AVPixelFormat pix_fmts[]
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
enum AVFrameSideDataType type
enum AVMediaType type
filter media type
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AVERROR_FILTER_NOT_FOUND
Filter not found.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
static int create_subcc_packet(AVFormatContext *avctx, AVFrame *frame, int sink_idx)
char * avfilter_graph_dump(AVFilterGraph *graph, const char *options)
Dump a graph into a human-readable string representation.
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
static const AVOption options[]
Flag to pass INT64_MIN/MAX through instead of rescaling, this avoids special cases for AV_NOPTS_VALUE...
#define AV_BUFFERSINK_FLAG_PEEK
Tell av_buffersink_get_buffer_ref() to read video/samples buffer reference, but not remove it from th...
int channels
number of audio channels
int64_t av_frame_get_pkt_pos(const AVFrame *frame)
void * priv_data
Format private data.
int avfilter_graph_parse_ptr(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs, void *log_ctx)
Add a graph described by a string to a graph.
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
static enum AVSampleFormat sample_fmts[]
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
#define av_malloc_array(a, b)
static int * create_all_formats(int n)
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
int * sink_stream_subcc_map
This structure stores compressed data.
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
int nb_samples
number of audio samples (per channel) described by this frame
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.