36 #define FF_INTERNAL_FIELDS 1
45 #define OFFSET(x) offsetof(AVFilterGraph, x)
46 #define F AV_OPT_FLAG_FILTERING_PARAM
47 #define V AV_OPT_FLAG_VIDEO_PARAM
48 #define A AV_OPT_FLAG_AUDIO_PARAM
53 {
"threads",
"Maximum number of threads",
OFFSET(nb_threads),
55 {
"scale_sws_opts" ,
"default scale filter options" ,
OFFSET(scale_sws_opts) ,
57 {
"aresample_swr_opts" ,
"default aresample filter options" ,
OFFSET(aresample_swr_opts) ,
111 for (j = 0; j<
filter->nb_outputs; j++)
125 while ((*graph)->nb_filters)
132 av_freep(&(*graph)->scale_sws_opts);
133 av_freep(&(*graph)->aresample_swr_opts);
135 av_freep(&(*graph)->resample_lavr_opts);
143 const char *
name,
const char *args,
void *opaque,
223 for (j = 0; j <
filt->nb_inputs; j++) {
224 if (!
filt->inputs[j] || !
filt->inputs[j]->src) {
225 pad = &
filt->input_pads[j];
227 "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
233 for (j = 0; j <
filt->nb_outputs; j++) {
234 if (!
filt->outputs[j] || !
filt->outputs[j]->dst) {
235 pad = &
filt->output_pads[j];
237 "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
260 if (!
filt->nb_outputs) {
278 for (j = 0; j <
f->nb_outputs; j++) {
320 switch (link->
type) {
350 for (
i = 0;
i <
ctx->nb_inputs;
i++) {
355 for (
i = 0;
i <
ctx->nb_outputs;
i++) {
370 ctx->outputs &&
ctx->outputs[0] ?
ctx->outputs[0]->type :
373 if ((ret =
ctx->filter->query_formats(
ctx)) < 0) {
383 for (
i = 0;
i <
ctx->nb_inputs;
i++)
385 for (
i = 0;
i <
ctx->nb_outputs;
i++)
406 for (
i = 0;
i <
f->nb_inputs;
i++) {
407 if (!
f->inputs[
i]->outcfg.formats)
410 !(
f->inputs[
i]->outcfg.samplerates &&
411 f->inputs[
i]->outcfg.channel_layouts))
414 for (
i = 0;
i <
f->nb_outputs;
i++) {
415 if (!
f->outputs[
i]->incfg.formats)
418 !(
f->outputs[
i]->incfg.samplerates &&
419 f->outputs[
i]->incfg.channel_layouts))
438 int scaler_count = 0, resampler_count = 0;
439 int count_queried = 0;
440 int count_merged = 0;
441 int count_already_merged = 0;
442 int count_delayed = 0;
448 if (
f->filter->query_formats)
452 if (ret < 0 && ret !=
AVERROR(EAGAIN))
455 count_queried += ret >= 0;
462 for (j = 0; j <
filter->nb_inputs; j++) {
464 int convert_needed = 0;
482 #define CHECKED_MERGE(field, ...) ((ret = ff_merge_ ## field(__VA_ARGS__)) <= 0)
483 #define MERGE_DISPATCH(field, ...) \
484 if (!(link->incfg.field && link->outcfg.field)) { \
486 } else if (link->incfg.field == link->outcfg.field) { \
487 count_already_merged++; \
488 } else if (!convert_needed) { \
490 if (CHECKED_MERGE(field, __VA_ARGS__)) { \
493 convert_needed = 1; \
505 #undef MERGE_DISPATCH
507 if (convert_needed) {
515 "The filters '%s' and '%s' do not have a common format "
516 "and automatic conversion is disabled.\n",
522 switch (link->
type) {
526 "not present, cannot convert pixel formats.\n");
530 snprintf(inst_name,
sizeof(inst_name),
"auto_scaler_%d",
541 "not present, cannot convert audio formats.\n");
545 snprintf(inst_name,
sizeof(inst_name),
"auto_resampler_%d",
569 av_assert0( inlink-> incfg.samplerates->refcount > 0);
571 av_assert0(outlink-> incfg.samplerates->refcount > 0);
573 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
575 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
595 "Impossible to convert between the formats supported by the filter "
604 "%d queried, %d merged, %d already done, %d delayed\n",
605 count_queried, count_merged, count_already_merged, count_delayed);
614 if (count_queried || count_merged)
622 "The following filters could not choose their formats: %s\n"
623 "Consider inserting the (a)format filter near their input or "
624 "output.\n", bp.str);
661 return score1 < score2 ? dst_fmt1 : dst_fmt2;
705 " the link between filters %s and %s.\n", link->
src->
name,
714 " the link between filters %s and %s.\n", link->
src->
name,
718 "supported, try specifying a channel layout using "
719 "'aformat=channel_layouts=something'.\n");
740 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
742 for (i = 0; i < filter->nb_inputs; i++) { \
743 AVFilterLink *link = filter->inputs[i]; \
746 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
748 fmt = link->outcfg.list->var[0]; \
750 for (j = 0; j < filter->nb_outputs; j++) { \
751 AVFilterLink *out_link = filter->outputs[j]; \
754 if (link->type != out_link->type || \
755 out_link->incfg.list->nb == 1) \
757 fmts = out_link->incfg.list; \
759 if (!out_link->incfg.list->nb) { \
760 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
766 for (k = 0; k < out_link->incfg.list->nb; k++) \
767 if (fmts->var[k] == fmt) { \
768 fmts->var[0] = fmt; \
779 int i, j, k, ret = 0;
787 for (
i = 0;
i <
filter->nb_inputs;
i++) {
796 for (j = 0; j <
filter->nb_outputs; j++) {
850 for (
i = 0;
i <
filter->nb_inputs;
i++) {
862 for (
i = 0;
i <
filter->nb_outputs;
i++) {
864 int best_idx, best_diff = INT_MAX;
875 if (
diff < best_diff) {
893 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
894 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
895 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
896 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
897 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
898 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
899 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
932 for (
i = 0;
i <
filter->nb_inputs;
i++) {
942 for (
i = 0;
i <
filter->nb_outputs;
i++) {
944 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
955 int count_diff = out_channels - in_channels;
956 int matched_channels, extra_channels;
968 score -= 10000 +
FFABS(out_channels - in_channels) +
969 (in_channels > out_channels ? 10000 : 0);
970 in_chlayout = out_chlayout = 0;
979 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
980 (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
981 in_chlayout &= ~cmp0;
982 out_chlayout &= ~cmp1;
1000 score += 10 * matched_channels - 5 * extra_channels;
1002 if (score > best_score ||
1003 (count_diff < best_count_diff && score == best_score)) {
1006 best_count_diff = count_diff;
1030 for (
i = 0;
i <
filter->nb_inputs;
i++) {
1043 for (
i = 0;
i <
filter->nb_outputs;
i++) {
1045 int best_idx = -1, best_score = INT_MIN;
1063 if (
bps == 4 && out_bps == 8) {
1069 score = -
abs(out_bps -
bps);
1073 if (score > best_score) {
1103 for (j = 0; j <
filter->nb_inputs; j++){
1104 if (
filter->inputs[j]->incfg.formats &&
filter->inputs[j]->incfg.formats->nb_formats == 1) {
1112 for (j = 0; j <
filter->nb_outputs; j++){
1113 if (
filter->outputs[j]->incfg.formats &&
filter->outputs[j]->incfg.formats->nb_formats == 1) {
1121 for (j = 0; j <
filter->nb_outputs; j++) {
1122 if (
filter->outputs[j]->format<0) {
1135 for (j = 0; j <
filter->nb_inputs; j++)
1138 for (j = 0; j <
filter->nb_outputs; j++)
1180 int sink_links_count = 0, n = 0;
1186 for (j = 0; j <
f->nb_inputs; j++) {
1187 f->inputs[j]->graph = graph;
1188 f->inputs[j]->age_index = -1;
1190 for (j = 0; j <
f->nb_outputs; j++) {
1191 f->outputs[j]->graph = graph;
1192 f->outputs[j]->age_index= -1;
1194 if (!
f->nb_outputs) {
1195 if (
f->nb_inputs > INT_MAX - sink_links_count)
1197 sink_links_count +=
f->nb_inputs;
1200 sinks =
av_calloc(sink_links_count,
sizeof(*sinks));
1205 if (!
f->nb_outputs) {
1206 for (j = 0; j <
f->nb_inputs; j++) {
1207 sinks[n] =
f->inputs[j];
1208 f->inputs[j]->age_index = n++;
1254 if (!strcmp(target,
"all") || (
filter->name && !strcmp(target,
filter->name)) || !strcmp(target,
filter->filter->name)) {
1275 if(
filter && (!strcmp(target,
"all") || !strcmp(target,
filter->name) || !strcmp(target,
filter->filter->name))){
1277 while (*queue && (*queue)->
time <= ts)
1278 queue = &(*queue)->
next;
1286 (*queue)->time = ts;
1287 (*queue)->flags =
flags;
1288 (*queue)->next = next;
1305 int parent = (
index - 1) >> 1;
1308 links[
index] = links[parent];
1312 links[
index] = link;
1324 int child = 2 *
index + 1;
1332 links[
index] = links[child];
1336 links[
index] = link;
1349 int64_t frame_count;
static const char *const format[]
static const int8_t filt[NUMTAPS *2]
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVFilterContext * ff_filter_alloc(const AVFilter *filter, const char *inst_name)
Allocate a new filter context and return it.
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
int ff_filter_activate(AVFilterContext *filter)
Main libavfilter public API header.
#define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format)
void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
Remove a filter from a graph;.
static int reduce_formats_on_filter(AVFilterContext *filter)
int ff_filter_graph_run_once(AVFilterGraph *graph)
Run one round of processing on a filter graph.
static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
Perform one round of query_formats() and merging formats lists on the filter graph.
static const uint64_t ch_subst[][2]
static void swap_channel_layouts_on_filter(AVFilterContext *filter)
static void swap_sample_fmts(AVFilterGraph *graph)
static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
Configure all the links of graphctx.
static void heap_bubble_down(AVFilterGraph *graph, AVFilterLink *link, int index)
static int formats_declared(AVFilterContext *f)
static void heap_bubble_up(AVFilterGraph *graph, AVFilterLink *link, int index)
static void swap_samplerates_on_filter(AVFilterContext *filter)
static int graph_config_pointers(AVFilterGraph *graph, AVClass *log_ctx)
static void swap_channel_layouts(AVFilterGraph *graph)
static int filter_check_formats(AVFilterContext *ctx)
Check the validity of the formats / etc.
static int graph_check_links(AVFilterGraph *graph, AVClass *log_ctx)
#define MERGE_DISPATCH(field,...)
static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2, enum AVSampleFormat src_fmt)
static int reduce_formats(AVFilterGraph *graph)
static const AVClass filtergraph_class
void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
Update the position of a link in the age heap.
void ff_graph_thread_free(AVFilterGraph *graph)
static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
static int pick_formats(AVFilterGraph *graph)
#define CHECKED_MERGE(field,...)
static int filter_query_formats(AVFilterContext *ctx)
int ff_graph_thread_init(AVFilterGraph *graph)
static void swap_sample_fmts_on_filter(AVFilterContext *filter)
static int pick_format(AVFilterLink *link, AVFilterLink *ref)
static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
Configure the formats of all the links in the graph.
static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
Check for the validity of graph.
static const AVOption filtergraph_options[]
static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
static void swap_samplerates(AVFilterGraph *graph)
void av_bprintf(AVBPrint *buf, const char *fmt,...)
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
#define AV_BPRINT_SIZE_AUTOMATIC
memory buffer sink API for audio and video
#define flags(name, subs,...)
static av_always_inline void filter(int16_t *output, ptrdiff_t out_stride, const int16_t *low, ptrdiff_t low_stride, const int16_t *high, ptrdiff_t high_stride, int len, int clip)
audio channel layout utility functions
#define FFSWAP(type, a, b)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static const uint16_t channel_layouts[7]
static void convert(float y, float u, float v, float *b, float *g, float *r)
void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
Init a global structure.
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define AV_CH_BACK_CENTER
#define AV_CH_FRONT_CENTER
#define AV_CH_LOW_FREQUENCY
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.
#define AV_BUFFERSINK_FLAG_PEEK
Tell av_buffersink_get_buffer_ref() to read video/samples buffer reference, but not remove it from th...
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 avfilter_free(AVFilterContext *filter)
Free a filter context.
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
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.
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
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.
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
#define AVFILTER_THREAD_SLICE
Process multiple parts of the frame concurrently.
AVFilterContext * avfilter_graph_alloc_filter(AVFilterGraph *graph, const AVFilter *filter, const char *name)
Create a new filter instance in a filter graph.
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 avfilter_config_links(AVFilterContext *filter)
Negotiate the media format, dimensions, etc of all inputs to a filter.
int avfilter_graph_request_oldest(AVFilterGraph *graph)
Request a frame on the oldest sink link.
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.
#define AVFILTER_CMD_FLAG_ONE
Stop once a filter understood the command (for target=all for example), fast filters are favored auto...
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
#define AVFILTER_CMD_FLAG_FAST
Only execute command when its fast (like a video out that supports contrast adjustment in hw)
void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
Enable or disable automatic format conversion inside the graph.
AVFilterContext * avfilter_graph_get_filter(AVFilterGraph *graph, const char *name)
Get a filter instance identified by instance name from graph.
#define AVERROR_EOF
End of file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
char * av_strdup(const char *s)
Duplicate a string.
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
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.
int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of a plane of an image with...
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
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.
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_S32
signed 32 bits
#define LIBAVUTIL_VERSION_INT
common internal API header
@ AV_CLASS_CATEGORY_FILTER
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.
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.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
AVPixelFormat
Pixel format.
static const struct PPFilter filters[]
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
A list of supported channel layouts.
char all_layouts
accept any known channel layout
char all_counts
accept any channel layout or count
int nb_channel_layouts
number of channel layouts
uint64_t * channel_layouts
list of channel layouts
unsigned refcount
number of references to this list
struct AVFilterCommand * next
double time
time expressed in seconds
char * name
name of this filter instance
const AVFilter * filter
the AVFilter of which this is an instance
unsigned ready
Ready status of the filter.
avfilter_execute_func * thread_execute
FFFrameQueueGlobal frame_queues
AVFilterLink ** sink_links
Private fields.
char * scale_sws_opts
sws options to use for the auto-inserted scale filters
AVFilterContext ** filters
char * aresample_swr_opts
swr options to use for the auto-inserted aresample filters, Access ONLY through AVOptions
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
unsigned disable_auto_convert
int thread_type
Type of multithreading allowed for filters in this graph.
avfilter_execute_func * execute
This callback may be set by the caller immediately after allocating the graph and before adding any f...
int nb_threads
Maximum number of threads used by filters in this graph.
A link between two filters.
AVFilterFormatsConfig incfg
Lists of supported formats / etc.
int w
agreed upon image width
int h
agreed upon image height
int channels
Number of channels.
AVFilterFormatsConfig outcfg
Lists of supported formats / etc.
enum AVMediaType type
filter media type
AVFilterContext * src
source filter
int sample_rate
samples per second
int frame_wanted_out
True if a frame is currently wanted on the output of this filter.
int age_index
Index in the age array.
int64_t current_pts_us
Current timestamp of the link, as defined by the most recent frame(s), in AV_TIME_BASE units.
uint64_t channel_layout
channel layout of current buffer (see libavutil/channel_layout.h)
AVFilterPad * dstpad
input pad on the dest filter
AVFilterContext * dst
dest filter
int format
agreed upon media format
A filter pad used for either input or output.
enum AVMediaType type
AVFilterPad type.
const char * name
Pad name.
int(* activate)(AVFilterContext *ctx)
Filter activation function.
uint8_t nb_components
The number of components each pixel has, (1-4)
static int ref[MAX_W *MAX_W]
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
static av_always_inline int diff(const uint32_t a, const uint32_t b)