35 #define FF_INTERNAL_FIELDS 1
44 #define OFFSET(x) offsetof(AVFilterGraph, x)
45 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
50 {
"threads",
"Maximum number of threads",
OFFSET(nb_threads),
52 {
"scale_sws_opts" ,
"default scale filter options" ,
OFFSET(scale_sws_opts) ,
54 {
"aresample_swr_opts" ,
"default aresample filter options" ,
OFFSET(aresample_swr_opts) ,
103 if (graph->
filters[i] == filter) {
122 while ((*graph)->nb_filters)
129 av_freep(&(*graph)->scale_sws_opts);
130 av_freep(&(*graph)->aresample_swr_opts);
132 av_freep(&(*graph)->resample_lavr_opts);
139 #if FF_API_AVFILTER_OPEN
150 filter->
graph = graph;
157 const char *
name,
const char *
args,
void *opaque,
241 "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
251 "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
404 if (is_sample_rate) {
436 int scaler_count = 0, resampler_count = 0;
437 int count_queried = 0;
438 int count_merged = 0;
439 int count_already_merged = 0;
440 int count_delayed = 0;
450 if (ret < 0 && ret !=
AVERROR(EAGAIN))
453 count_queried += ret >= 0;
460 for (j = 0; j < filter->
nb_inputs; j++) {
462 int convert_needed = 0;
481 #define MERGE_DISPATCH(field, statement) \
482 if (!(link->in_ ## field && link->out_ ## field)) { \
484 } else if (link->in_ ## field == link->out_ ## field) { \
485 count_already_merged++; \
486 } else if (!convert_needed) { \
508 #undef MERGE_DISPATCH
510 if (convert_needed) {
514 char scale_args[256];
519 "The filters '%s' and '%s' do not have a common format "
520 "and automatic conversion is disabled.\n",
526 switch (link->
type) {
530 "not present, cannot convert pixel formats.\n");
534 snprintf(inst_name,
sizeof(inst_name),
"auto_scaler_%d",
545 "not present, cannot convert audio formats.\n");
549 snprintf(inst_name,
sizeof(inst_name),
"auto_resampler_%d",
551 scale_args[0] =
'\0';
553 snprintf(scale_args,
sizeof(scale_args),
"%s",
570 inlink = convert->
inputs[0];
572 av_assert0( inlink-> in_formats->refcount > 0);
574 av_assert0(outlink-> in_formats->refcount > 0);
577 av_assert0( inlink-> in_samplerates->refcount > 0);
579 av_assert0(outlink-> in_samplerates->refcount > 0);
581 av_assert0( inlink-> in_channel_layouts->refcount > 0);
583 av_assert0(outlink-> in_channel_layouts->refcount > 0);
604 "Impossible to convert between the formats supported by the filter "
613 "%d queried, %d merged, %d already done, %d delayed\n",
614 count_queried, count_merged, count_already_merged, count_delayed);
623 if (count_queried || count_merged)
631 "The following filters could not choose their formats: %s\n"
632 "Consider inserting the (a)format filter near their input or "
633 "output.\n", bp.str);
670 return score1 < score2 ? dst_fmt1 : dst_fmt2;
713 " the link between filters %s and %s.\n", link->
src->
name,
722 " the link between filters %s and %s.\n", link->
src->
name,
726 "supported, try specifying a channel layout using "
727 "'aformat=channel_layouts=something'.\n");
748 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format, unref_format) \
750 for (i = 0; i < filter->nb_inputs; i++) { \
751 AVFilterLink *link = filter->inputs[i]; \
754 if (!link->out_ ## list || link->out_ ## list->nb != 1) \
756 fmt = link->out_ ## list->var[0]; \
758 for (j = 0; j < filter->nb_outputs; j++) { \
759 AVFilterLink *out_link = filter->outputs[j]; \
762 if (link->type != out_link->type || \
763 out_link->in_ ## list->nb == 1) \
765 fmts = out_link->in_ ## list; \
767 if (!out_link->in_ ## list->nb) { \
768 if ((ret = add_format(&out_link->in_ ##list, fmt)) < 0)\
774 for (k = 0; k < out_link->in_ ## list->nb; k++) \
775 if (fmts->var[k] == fmt) { \
776 fmts->var[0] = fmt; \
787 int i, j, k, ret = 0;
795 for (i = 0; i < filter->
nb_inputs; i++) {
858 for (i = 0; i < filter->
nb_inputs; i++) {
872 int best_idx, best_diff = INT_MAX;
883 if (diff < best_diff) {
901 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
902 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
903 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
904 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
905 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
906 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
907 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
940 for (i = 0; i < filter->
nb_inputs; i++) {
952 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
963 int count_diff = out_channels - in_channels;
964 int matched_channels, extra_channels;
976 score -= 10000 +
FFABS(out_channels - in_channels) +
977 (in_channels > out_channels ? 10000 : 0);
978 in_chlayout = out_chlayout = 0;
985 uint64_t cmp0 = ch_subst[k][0];
986 uint64_t cmp1 = ch_subst[k][1];
987 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
988 (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
989 in_chlayout &= ~cmp0;
990 out_chlayout &= ~cmp1;
999 (out_chlayout & AV_CH_LOW_FREQUENCY))
1001 in_chlayout &= ~AV_CH_LOW_FREQUENCY;
1002 out_chlayout &= ~AV_CH_LOW_FREQUENCY;
1008 score += 10 * matched_channels - 5 * extra_channels;
1010 if (score > best_score ||
1011 (count_diff < best_count_diff && score == best_score)) {
1014 best_count_diff = count_diff;
1038 for (i = 0; i < filter->
nb_inputs; i++) {
1039 link = filter->
inputs[i];
1053 int best_idx = -1, best_score = INT_MIN;
1071 if (bps == 4 && out_bps == 8) {
1077 score = -abs(out_bps - bps);
1081 if (score > best_score) {
1111 for (j = 0; j < filter->
nb_inputs; j++){
1188 int sink_links_count = 0,
n = 0;
1203 if (f->
nb_inputs > INT_MAX - sink_links_count)
1208 sinks =
av_calloc(sink_links_count,
sizeof(*sinks));
1248 snprintf(name,
sizeof(name),
"auto_fifo_%d", fifo_count++);
1300 if (!strcmp(target,
"all") || (filter->
name && !strcmp(target, filter->
name)) || !strcmp(target, filter->
filter->
name)) {
1303 if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1321 if(filter && (!strcmp(target,
"all") || !strcmp(target, filter->
name) || !strcmp(target, filter->
filter->
name))){
1323 while (*queue && (*queue)->
time <= ts)
1324 queue = &(*queue)->
next;
1332 (*queue)->time = ts;
1333 (*queue)->flags =
flags;
1334 (*queue)->next =
next;
1351 int parent = (index - 1) >> 1;
1354 links[
index] = links[parent];
1358 links[
index] = link;
1370 int child = 2 * index + 1;
1382 links[
index] = link;
1395 int64_t frame_count;
AVFilterContext ** filters
void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
Enable or disable automatic format conversion inside the graph.
int frame_wanted_out
True if a frame is currently wanted on the output of this filter.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
AVFilterContext * ff_filter_alloc(const AVFilter *filter, const char *inst_name)
Allocate a new filter context and return it.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
Queue a command for one or more filter instances.
int thread_type
Type of multithreading allowed for filters in this graph.
static void swap_samplerates(AVFilterGraph *graph)
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
static void swap_sample_fmts(AVFilterGraph *graph)
void avfilter_free(AVFilterContext *filter)
Free a filter context.
static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
Main libavfilter public API header.
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
static int reduce_formats(AVFilterGraph *graph)
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
static const uint64_t ch_subst[][2]
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
enum AVMediaType type
AVFilterPad type.
static int reduce_formats_on_filter(AVFilterContext *filter)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int(* query_formats)(AVFilterContext *)
Query formats supported by the filter on its inputs and outputs.
AVFilterPad * dstpad
input pad on the dest filter
struct AVFilterChannelLayouts * in_channel_layouts
static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
char * scale_sws_opts
sws options to use for the auto-inserted scale filters
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
#define AVFILTER_THREAD_SLICE
Process multiple parts of the frame concurrently.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
struct AVFilterGraph * graph
filtergraph this filter belongs to
memory buffer sink API for audio and video
const char * name
Pad name.
static int pick_format(AVFilterLink *link, AVFilterLink *ref)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int ff_filter_graph_run_once(AVFilterGraph *graph)
Run one round of processing on a filter graph.
AVFilterLink ** inputs
array of pointers to input links
char * name
name of this filter instance
#define av_assert0(cond)
assert() equivalent, that is always enabled.
avfilter_execute_func * execute
This callback may be set by the caller immediately after allocating the graph and before adding any f...
AVFilterPad * output_pads
array of output pads
unsigned ready
Ready status of the filter.
AVS_FilterInfo AVS_Value child
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
#define AV_CH_LOW_FREQUENCY
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.
static int pick_formats(AVFilterGraph *graph)
int nb_threads
Maximum number of threads used by filters in this graph.
int avfilter_config_links(AVFilterContext *filter)
Negotiate the media format, dimensions, etc of all inputs to a filter.
#define AVERROR_EOF
End of file.
AVFilterContext * avfilter_graph_get_filter(AVFilterGraph *graph, const char *name)
Get a filter instance identified by instance name from graph.
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
A filter pad used for either input or output.
A link between two filters.
AVFilterPad * input_pads
array of input pads
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int sample_rate
samples per second
unsigned nb_outputs
number of output pads
int64_t current_pts_us
Current timestamp of the link, as defined by the most recent frame(s), in AV_TIME_BASE units...
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
uint64_t * channel_layouts
list of channel layouts
simple assert() macros that are a bit more flexible than ISO C assert().
struct AVFilterChannelLayouts * out_channel_layouts
AVFilterFormats * in_formats
Lists of formats and channel layouts supported by the input and output filters respectively.
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.
static void heap_bubble_down(AVFilterGraph *graph, AVFilterLink *link, int index)
#define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format, unref_format)
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
AVFilterLink ** sink_links
Private fields.
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.
char all_counts
accept any channel layout or count
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
common internal API header
static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
Configure the formats of all the links in the graph.
uint8_t nb_components
The number of components each pixel has, (1-4)
audio channel layout utility functions
static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
unsigned nb_inputs
number of input pads
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
static void swap_sample_fmts_on_filter(AVFilterContext *filter)
struct AVFilterCommand * next
static const AVOption filtergraph_options[]
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
Check for the validity of graph.
static int convert(uint8_t x)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
AVFilterContext * src
source filter
#define AVFILTER_CMD_FLAG_ONE
Stop once a filter understood the command (for target=all for example), fast filters are favored auto...
#define AV_CH_FRONT_CENTER
static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2, enum AVSampleFormat src_fmt)
int age_index
Index in the age array.
AVFilterFormats * out_samplerates
static AVFilterFormats * clone_filter_formats(AVFilterFormats *arg)
static int can_merge_formats(AVFilterFormats *a_arg, AVFilterFormats *b_arg, enum AVMediaType type, int is_sample_rate)
int format
agreed upon media format
#define FF_ARRAY_ELEMS(a)
A list of supported channel layouts.
int avfilter_init_str(AVFilterContext *filter, const char *args)
Initialize a filter with the supplied parameters.
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt, unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
Insert a filter in the middle of an existing link.
void ff_graph_thread_free(AVFilterGraph *graph)
AVFilterFormats * in_samplerates
Lists of channel layouts and sample rates used for automatic negotiation.
#define AV_BPRINT_SIZE_AUTOMATIC
char * av_strdup(const char *s)
Duplicate a string.
AVSampleFormat
Audio sample formats.
static void heap_bubble_up(AVFilterGraph *graph, AVFilterLink *link, int index)
static int formats_declared(AVFilterContext *f)
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
char all_layouts
accept any known channel layout
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
Update the position of a link in the age heap.
int avfilter_graph_request_oldest(AVFilterGraph *graph)
Request a frame on the oldest sink link.
int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
Make the filter instance process a command.
static const char * format
static const uint16_t channel_layouts[7]
Describe the class of an AVClass context structure.
#define AVFILTER_CMD_FLAG_FAST
Only execute command when its fast (like a video out that supports contrast adjustment in hw) ...
const char * name
Filter name.
AVFilterLink ** outputs
array of pointers to output links
static void swap_channel_layouts_on_filter(AVFilterContext *filter)
static const int8_t filt[NUMTAPS]
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
enum AVMediaType type
filter media type
#define AV_CH_BACK_CENTER
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
Perform one round of query_formats() and merging formats lists on the filter graph.
struct AVFilterGraph * graph
Graph the filter belongs to.
int ff_graph_thread_init(AVFilterGraph *graph)
static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
Configure all the links of graphctx.
static void swap_channel_layouts(AVFilterGraph *graph)
#define MERGE_DISPATCH(field, statement)
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
static int ref[MAX_W *MAX_W]
int nb_channel_layouts
number of channel layouts
struct AVFilterCommand * command_queue
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
Send a command to one or more filter instances.
int channels
Number of channels.
enum AVPixelFormat av_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2, enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
Compute what kind of losses will occur when converting from one specific pixel format to another...
static void swap_samplerates_on_filter(AVFilterContext *filter)
char * aresample_swr_opts
swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions ...
void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
Remove a filter from a graph;.
#define AV_BUFFERSINK_FLAG_PEEK
Tell av_buffersink_get_buffer_ref() to read video/samples buffer reference, but not remove it from th...
static av_always_inline int diff(const uint32_t a, const uint32_t b)
avfilter_execute_func * thread_execute
AVFilterContext * avfilter_graph_alloc_filter(AVFilterGraph *graph, const AVFilter *filter, const char *name)
Create a new filter instance in a filter graph.
AVFilterContext * dst
dest filter
static const struct PPFilter filters[]
unsigned refcount
number of references to this list
void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
Init a global structure.
static const AVClass filtergraph_class
FFFrameQueueGlobal frame_queues
double time
time expressed in seconds
int needs_fifo
The filter expects a fifo to be inserted on its input link, typically because it has a delay...
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
#define FFSWAP(type, a, b)
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
static int filter_query_formats(AVFilterContext *ctx)
int ff_filter_activate(AVFilterContext *filter)
int(* activate)(AVFilterContext *ctx)
Filter activation function.
AVPixelFormat
Pixel format.
const AVFilter * filter
the AVFilter of which this is an instance
AVFilterFormats * out_formats
unsigned disable_auto_convert
static int graph_config_pointers(AVFilterGraph *graph, AVClass *log_ctx)