]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfilter.h
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / avfilter.h
1 /*
2  * filter layer
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #ifndef AVFILTER_AVFILTER_H
23 #define AVFILTER_AVFILTER_H
24
25 #include "libavutil/avutil.h"
26 #include "libavutil/log.h"
27 #include "libavutil/samplefmt.h"
28 #include "libavutil/pixfmt.h"
29 #include "libavutil/rational.h"
30 #include "libavcodec/avcodec.h"
31
32
33 #ifndef FF_API_OLD_VSINK_API
34 #define FF_API_OLD_VSINK_API        (LIBAVFILTER_VERSION_MAJOR < 3)
35 #endif
36 #ifndef FF_API_OLD_ALL_FORMATS_API
37 #define FF_API_OLD_ALL_FORMATS_API (LIBAVFILTER_VERSION_MAJOR < 3)
38 #endif
39
40 #include <stddef.h>
41
42 #include "libavfilter/version.h"
43
44 /**
45  * Return the LIBAVFILTER_VERSION_INT constant.
46  */
47 unsigned avfilter_version(void);
48
49 /**
50  * Return the libavfilter build-time configuration.
51  */
52 const char *avfilter_configuration(void);
53
54 /**
55  * Return the libavfilter license.
56  */
57 const char *avfilter_license(void);
58
59
60 typedef struct AVFilterContext AVFilterContext;
61 typedef struct AVFilterLink    AVFilterLink;
62 typedef struct AVFilterPad     AVFilterPad;
63 typedef struct AVFilterFormats AVFilterFormats;
64
65 /**
66  * A reference-counted buffer data type used by the filter system. Filters
67  * should not store pointers to this structure directly, but instead use the
68  * AVFilterBufferRef structure below.
69  */
70 typedef struct AVFilterBuffer {
71     uint8_t *data[8];           ///< buffer data for each plane/channel
72     int linesize[8];            ///< number of bytes per line
73
74     unsigned refcount;          ///< number of references to this buffer
75
76     /** private data to be used by a custom free function */
77     void *priv;
78     /**
79      * A pointer to the function to deallocate this buffer if the default
80      * function is not sufficient. This could, for example, add the memory
81      * back into a memory pool to be reused later without the overhead of
82      * reallocating it from scratch.
83      */
84     void (*free)(struct AVFilterBuffer *buf);
85
86     int format;                 ///< media format
87     int w, h;                   ///< width and height of the allocated buffer
88
89     /**
90      * pointers to the data planes/channels.
91      *
92      * For video, this should simply point to data[].
93      *
94      * For planar audio, each channel has a separate data pointer, and
95      * linesize[0] contains the size of each channel buffer.
96      * For packed audio, there is just one data pointer, and linesize[0]
97      * contains the total size of the buffer for all channels.
98      *
99      * Note: Both data and extended_data will always be set, but for planar
100      * audio with more channels that can fit in data, extended_data must be used
101      * in order to access all channels.
102      */
103     uint8_t **extended_data;
104 } AVFilterBuffer;
105
106 #define AV_PERM_READ     0x01   ///< can read from the buffer
107 #define AV_PERM_WRITE    0x02   ///< can write to the buffer
108 #define AV_PERM_PRESERVE 0x04   ///< nobody else can overwrite the buffer
109 #define AV_PERM_REUSE    0x08   ///< can output the buffer multiple times, with the same contents each time
110 #define AV_PERM_REUSE2   0x10   ///< can output the buffer multiple times, modified each time
111 #define AV_PERM_NEG_LINESIZES 0x20  ///< the buffer requested can have negative linesizes
112 #define AV_PERM_ALIGN    0x40   ///< the buffer must be aligned
113
114 #define AVFILTER_ALIGN 16 //not part of ABI
115
116 /**
117  * Audio specific properties in a reference to an AVFilterBuffer. Since
118  * AVFilterBufferRef is common to different media formats, audio specific
119  * per reference properties must be separated out.
120  */
121 typedef struct AVFilterBufferRefAudioProps {
122     uint64_t channel_layout;    ///< channel layout of audio buffer
123     int nb_samples;             ///< number of audio samples per channel
124     int sample_rate;            ///< audio buffer sample rate
125 #if FF_API_PACKING
126     int planar;                 ///< audio buffer - planar or packed
127 #endif
128 } AVFilterBufferRefAudioProps;
129
130 /**
131  * Video specific properties in a reference to an AVFilterBuffer. Since
132  * AVFilterBufferRef is common to different media formats, video specific
133  * per reference properties must be separated out.
134  */
135 typedef struct AVFilterBufferRefVideoProps {
136     int w;                      ///< image width
137     int h;                      ///< image height
138     AVRational sample_aspect_ratio; ///< sample aspect ratio
139     int interlaced;             ///< is frame interlaced
140     int top_field_first;        ///< field order
141     enum AVPictureType pict_type; ///< picture type of the frame
142     int key_frame;              ///< 1 -> keyframe, 0-> not
143 } AVFilterBufferRefVideoProps;
144
145 /**
146  * A reference to an AVFilterBuffer. Since filters can manipulate the origin of
147  * a buffer to, for example, crop image without any memcpy, the buffer origin
148  * and dimensions are per-reference properties. Linesize is also useful for
149  * image flipping, frame to field filters, etc, and so is also per-reference.
150  *
151  * TODO: add anything necessary for frame reordering
152  */
153 typedef struct AVFilterBufferRef {
154     AVFilterBuffer *buf;        ///< the buffer that this is a reference to
155     uint8_t *data[8];           ///< picture/audio data for each plane
156     int linesize[8];            ///< number of bytes per line
157     int format;                 ///< media format
158
159     /**
160      * presentation timestamp. The time unit may change during
161      * filtering, as it is specified in the link and the filter code
162      * may need to rescale the PTS accordingly.
163      */
164     int64_t pts;
165     int64_t pos;                ///< byte position in stream, -1 if unknown
166
167     int perms;                  ///< permissions, see the AV_PERM_* flags
168
169     enum AVMediaType type;      ///< media type of buffer data
170     AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
171     AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
172
173     /**
174      * pointers to the data planes/channels.
175      *
176      * For video, this should simply point to data[].
177      *
178      * For planar audio, each channel has a separate data pointer, and
179      * linesize[0] contains the size of each channel buffer.
180      * For packed audio, there is just one data pointer, and linesize[0]
181      * contains the total size of the buffer for all channels.
182      *
183      * Note: Both data and extended_data will always be set, but for planar
184      * audio with more channels that can fit in data, extended_data must be used
185      * in order to access all channels.
186      */
187     uint8_t **extended_data;
188 } AVFilterBufferRef;
189
190 /**
191  * Copy properties of src to dst, without copying the actual data
192  */
193 void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src);
194
195 /**
196  * Add a new reference to a buffer.
197  *
198  * @param ref   an existing reference to the buffer
199  * @param pmask a bitmask containing the allowable permissions in the new
200  *              reference
201  * @return      a new reference to the buffer with the same properties as the
202  *              old, excluding any permissions denied by pmask
203  */
204 AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
205
206 /**
207  * Remove a reference to a buffer. If this is the last reference to the
208  * buffer, the buffer itself is also automatically freed.
209  *
210  * @param ref reference to the buffer, may be NULL
211  */
212 void avfilter_unref_buffer(AVFilterBufferRef *ref);
213
214 /**
215  * Remove a reference to a buffer and set the pointer to NULL.
216  * If this is the last reference to the buffer, the buffer itself
217  * is also automatically freed.
218  *
219  * @param ref pointer to the buffer reference
220  */
221 void avfilter_unref_bufferp(AVFilterBufferRef **ref);
222
223 #if FF_API_FILTERS_PUBLIC
224 /**
225  * A list of supported formats for one end of a filter link. This is used
226  * during the format negotiation process to try to pick the best format to
227  * use to minimize the number of necessary conversions. Each filter gives a
228  * list of the formats supported by each input and output pad. The list
229  * given for each pad need not be distinct - they may be references to the
230  * same list of formats, as is often the case when a filter supports multiple
231  * formats, but will always output the same format as it is given in input.
232  *
233  * In this way, a list of possible input formats and a list of possible
234  * output formats are associated with each link. When a set of formats is
235  * negotiated over a link, the input and output lists are merged to form a
236  * new list containing only the common elements of each list. In the case
237  * that there were no common elements, a format conversion is necessary.
238  * Otherwise, the lists are merged, and all other links which reference
239  * either of the format lists involved in the merge are also affected.
240  *
241  * For example, consider the filter chain:
242  * filter (a) --> (b) filter (b) --> (c) filter
243  *
244  * where the letters in parenthesis indicate a list of formats supported on
245  * the input or output of the link. Suppose the lists are as follows:
246  * (a) = {A, B}
247  * (b) = {A, B, C}
248  * (c) = {B, C}
249  *
250  * First, the first link's lists are merged, yielding:
251  * filter (a) --> (a) filter (a) --> (c) filter
252  *
253  * Notice that format list (b) now refers to the same list as filter list (a).
254  * Next, the lists for the second link are merged, yielding:
255  * filter (a) --> (a) filter (a) --> (a) filter
256  *
257  * where (a) = {B}.
258  *
259  * Unfortunately, when the format lists at the two ends of a link are merged,
260  * we must ensure that all links which reference either pre-merge format list
261  * get updated as well. Therefore, we have the format list structure store a
262  * pointer to each of the pointers to itself.
263  * @addtogroup lavfi_deprecated
264  * @deprecated Those functions are only useful inside filters and
265  * user filters are not supported at this point.
266  * @{
267  */
268 struct AVFilterFormats {
269     unsigned format_count;      ///< number of formats
270     int *formats;               ///< list of media formats
271
272     unsigned refcount;          ///< number of references to this list
273     struct AVFilterFormats ***refs; ///< references to this list
274 };
275
276 /**
277  * Create a list of supported formats. This is intended for use in
278  * AVFilter->query_formats().
279  *
280  * @param fmts list of media formats, terminated by -1. If NULL an
281  *        empty list is created.
282  * @return the format list, with no existing references
283  */
284 attribute_deprecated
285 AVFilterFormats *avfilter_make_format_list(const int *fmts);
286
287 /**
288  * Add fmt to the list of media formats contained in *avff.
289  * If *avff is NULL the function allocates the filter formats struct
290  * and puts its pointer in *avff.
291  *
292  * @return a non negative value in case of success, or a negative
293  * value corresponding to an AVERROR code in case of error
294  * @deprecated Use ff_all_formats() instead.
295  */
296 attribute_deprecated
297 int avfilter_add_format(AVFilterFormats **avff, int64_t fmt);
298 attribute_deprecated
299 AVFilterFormats *avfilter_all_formats(enum AVMediaType type);
300
301 /**
302  * Return a list of all formats supported by FFmpeg for the given media type.
303  */
304 AVFilterFormats *avfilter_make_all_formats(enum AVMediaType type);
305
306 /**
307  * A list of all channel layouts supported by libavfilter.
308  */
309 extern const int64_t avfilter_all_channel_layouts[];
310
311 #if FF_API_PACKING
312 /**
313  * Return a list of all audio packing formats.
314  */
315 AVFilterFormats *avfilter_make_all_packing_formats(void);
316 #endif
317
318 /**
319  * Return a format list which contains the intersection of the formats of
320  * a and b. Also, all the references of a, all the references of b, and
321  * a and b themselves will be deallocated.
322  *
323  * If a and b do not share any common formats, neither is modified, and NULL
324  * is returned.
325  */
326 attribute_deprecated
327 AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b);
328
329 /**
330  * Add *ref as a new reference to formats.
331  * That is the pointers will point like in the ASCII art below:
332  *   ________
333  *  |formats |<--------.
334  *  |  ____  |     ____|___________________
335  *  | |refs| |    |  __|_
336  *  | |* * | |    | |  | |  AVFilterLink
337  *  | |* *--------->|*ref|
338  *  | |____| |    | |____|
339  *  |________|    |________________________
340  */
341 attribute_deprecated
342 void avfilter_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
343 attribute_deprecated
344 void avfilter_formats_unref(AVFilterFormats **ref);
345 attribute_deprecated
346 void avfilter_formats_changeref(AVFilterFormats **oldref,
347                                 AVFilterFormats **newref);
348 /**
349  * Helpers for query_formats() which set all links to the same list of
350  * formats/layouts. If there are no links hooked to this filter, the list
351  * of formats is freed.
352  */
353 attribute_deprecated
354 void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats);
355
356 attribute_deprecated
357 void avfilter_set_common_pixel_formats(AVFilterContext *ctx, AVFilterFormats *formats);
358 attribute_deprecated
359 void avfilter_set_common_sample_formats(AVFilterContext *ctx, AVFilterFormats *formats);
360 attribute_deprecated
361 void avfilter_set_common_channel_layouts(AVFilterContext *ctx, AVFilterFormats *formats);
362 #if FF_API_PACKING
363 attribute_deprecated
364 void avfilter_set_common_packing_formats(AVFilterContext *ctx, AVFilterFormats *formats);
365 #endif
366
367 /**
368  * @}
369  */
370 #endif
371
372 #if FF_API_AVFILTERPAD_PUBLIC
373 /**
374  * A filter pad used for either input or output.
375  *
376  * See doc/filter_design.txt for details on how to implement the methods.
377  *
378  * @warning this struct might be removed from public API.
379  * users should call avfilter_pad_get_name() and avfilter_pad_get_type()
380  * to access the name and type fields; there should be no need to access
381  * any other fields from outside of libavfilter.
382  */
383 struct AVFilterPad {
384     /**
385      * Pad name. The name is unique among inputs and among outputs, but an
386      * input may have the same name as an output. This may be NULL if this
387      * pad has no need to ever be referenced by name.
388      */
389     const char *name;
390
391     /**
392      * AVFilterPad type.
393      */
394     enum AVMediaType type;
395
396     /**
397      * Minimum required permissions on incoming buffers. Any buffer with
398      * insufficient permissions will be automatically copied by the filter
399      * system to a new buffer which provides the needed access permissions.
400      *
401      * Input pads only.
402      */
403     int min_perms;
404
405     /**
406      * Permissions which are not accepted on incoming buffers. Any buffer
407      * which has any of these permissions set will be automatically copied
408      * by the filter system to a new buffer which does not have those
409      * permissions. This can be used to easily disallow buffers with
410      * AV_PERM_REUSE.
411      *
412      * Input pads only.
413      */
414     int rej_perms;
415
416     /**
417      * Callback called before passing the first slice of a new frame. If
418      * NULL, the filter layer will default to storing a reference to the
419      * picture inside the link structure.
420      *
421      * Input video pads only.
422      */
423     void (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
424
425     /**
426      * Callback function to get a video buffer. If NULL, the filter system will
427      * use avfilter_default_get_video_buffer().
428      *
429      * Input video pads only.
430      */
431     AVFilterBufferRef *(*get_video_buffer)(AVFilterLink *link, int perms, int w, int h);
432
433     /**
434      * Callback function to get an audio buffer. If NULL, the filter system will
435      * use avfilter_default_get_audio_buffer().
436      *
437      * Input audio pads only.
438      */
439     AVFilterBufferRef *(*get_audio_buffer)(AVFilterLink *link, int perms,
440                                            int nb_samples);
441
442     /**
443      * Callback called after the slices of a frame are completely sent. If
444      * NULL, the filter layer will default to releasing the reference stored
445      * in the link structure during start_frame().
446      *
447      * Input video pads only.
448      */
449     void (*end_frame)(AVFilterLink *link);
450
451     /**
452      * Slice drawing callback. This is where a filter receives video data
453      * and should do its processing.
454      *
455      * Input video pads only.
456      */
457     void (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
458
459     /**
460      * Samples filtering callback. This is where a filter receives audio data
461      * and should do its processing.
462      *
463      * Input audio pads only.
464      */
465     void (*filter_samples)(AVFilterLink *link, AVFilterBufferRef *samplesref);
466
467     /**
468      * Frame poll callback. This returns the number of immediately available
469      * samples. It should return a positive value if the next request_frame()
470      * is guaranteed to return one frame (with no delay).
471      *
472      * Defaults to just calling the source poll_frame() method.
473      *
474      * Output pads only.
475      */
476     int (*poll_frame)(AVFilterLink *link);
477
478     /**
479      * Frame request callback. A call to this should result in at least one
480      * frame being output over the given link. This should return zero on
481      * success, and another value on error.
482      * See ff_request_frame() for the error codes with a specific
483      * meaning.
484      *
485      * Output pads only.
486      */
487     int (*request_frame)(AVFilterLink *link);
488
489     /**
490      * Link configuration callback.
491      *
492      * For output pads, this should set the following link properties:
493      * video: width, height, sample_aspect_ratio, time_base
494      * audio: sample_rate.
495      *
496      * This should NOT set properties such as format, channel_layout, etc which
497      * are negotiated between filters by the filter system using the
498      * query_formats() callback before this function is called.
499      *
500      * For input pads, this should check the properties of the link, and update
501      * the filter's internal state as necessary.
502      *
503      * For both input and output pads, this should return zero on success,
504      * and another value on error.
505      */
506     int (*config_props)(AVFilterLink *link);
507
508     /**
509      * The filter expects a fifo to be inserted on its input link,
510      * typically because it has a delay.
511      *
512      * input pads only.
513      */
514     int needs_fifo;
515 };
516 #endif
517
518 /**
519  * Get the name of an AVFilterPad.
520  *
521  * @param pads an array of AVFilterPads
522  * @param pad_idx index of the pad in the array it; is the caller's
523  *                responsibility to ensure the index is valid
524  *
525  * @return name of the pad_idx'th pad in pads
526  */
527 const char *avfilter_pad_get_name(AVFilterPad *pads, int pad_idx);
528
529 /**
530  * Get the type of an AVFilterPad.
531  *
532  * @param pads an array of AVFilterPads
533  * @param pad_idx index of the pad in the array; it is the caller's
534  *                responsibility to ensure the index is valid
535  *
536  * @return type of the pad_idx'th pad in pads
537  */
538 enum AVMediaType avfilter_pad_get_type(AVFilterPad *pads, int pad_idx);
539
540 /** default handler for end_frame() for video inputs */
541 attribute_deprecated
542 void avfilter_default_end_frame(AVFilterLink *link);
543
544 #if FF_API_FILTERS_PUBLIC
545 /** default handler for start_frame() for video inputs */
546 attribute_deprecated
547 void avfilter_default_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
548
549 /** default handler for draw_slice() for video inputs */
550 attribute_deprecated
551 void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
552
553 /** default handler for get_video_buffer() for video inputs */
554 attribute_deprecated
555 AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link,
556                                                      int perms, int w, int h);
557
558 /** Default handler for query_formats() */
559 attribute_deprecated
560 int avfilter_default_query_formats(AVFilterContext *ctx);
561 #endif
562
563 #if FF_API_FILTERS_PUBLIC
564 /** start_frame() handler for filters which simply pass video along */
565 attribute_deprecated
566 void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
567
568 /** draw_slice() handler for filters which simply pass video along */
569 attribute_deprecated
570 void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
571
572 /** end_frame() handler for filters which simply pass video along */
573 attribute_deprecated
574 void avfilter_null_end_frame(AVFilterLink *link);
575
576 /** get_video_buffer() handler for filters which simply pass video along */
577 attribute_deprecated
578 AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link,
579                                                   int perms, int w, int h);
580 #endif
581
582 /**
583  * Filter definition. This defines the pads a filter contains, and all the
584  * callback functions used to interact with the filter.
585  */
586 typedef struct AVFilter {
587     const char *name;         ///< filter name
588
589     int priv_size;      ///< size of private data to allocate for the filter
590
591     /**
592      * Filter initialization function. Args contains the user-supplied
593      * parameters. FIXME: maybe an AVOption-based system would be better?
594      * opaque is data provided by the code requesting creation of the filter,
595      * and is used to pass data to the filter.
596      */
597     int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
598
599     /**
600      * Filter uninitialization function. Should deallocate any memory held
601      * by the filter, release any buffer references, etc. This does not need
602      * to deallocate the AVFilterContext->priv memory itself.
603      */
604     void (*uninit)(AVFilterContext *ctx);
605
606     /**
607      * Queries formats/layouts supported by the filter and its pads, and sets
608      * the in_formats/in_chlayouts for links connected to its output pads,
609      * and out_formats/out_chlayouts for links connected to its input pads.
610      *
611      * @return zero on success, a negative value corresponding to an
612      * AVERROR code otherwise
613      */
614     int (*query_formats)(AVFilterContext *);
615
616     const AVFilterPad *inputs;  ///< NULL terminated list of inputs. NULL if none
617     const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
618
619     /**
620      * A description for the filter. You should use the
621      * NULL_IF_CONFIG_SMALL() macro to define it.
622      */
623     const char *description;
624
625     /**
626      * Make the filter instance process a command.
627      *
628      * @param cmd    the command to process, for handling simplicity all commands must be alphanumeric only
629      * @param arg    the argument for the command
630      * @param res    a buffer with size res_size where the filter(s) can return a response. This must not change when the command is not supported.
631      * @param flags  if AVFILTER_CMD_FLAG_FAST is set and the command would be
632      *               time consuming then a filter should treat it like an unsupported command
633      *
634      * @returns >=0 on success otherwise an error code.
635      *          AVERROR(ENOSYS) on unsupported commands
636      */
637     int (*process_command)(AVFilterContext *, const char *cmd, const char *arg, char *res, int res_len, int flags);
638 } AVFilter;
639
640 /** An instance of a filter */
641 struct AVFilterContext {
642     const AVClass *av_class;        ///< needed for av_log()
643
644     AVFilter *filter;               ///< the AVFilter of which this is an instance
645
646     char *name;                     ///< name of this filter instance
647
648 #if FF_API_FOO_COUNT
649     unsigned input_count;           ///< @deprecated use nb_inputs
650 #endif
651     AVFilterPad   *input_pads;      ///< array of input pads
652     AVFilterLink **inputs;          ///< array of pointers to input links
653
654 #if FF_API_FOO_COUNT
655     unsigned output_count;          ///< @deprecated use nb_outputs
656 #endif
657     AVFilterPad   *output_pads;     ///< array of output pads
658     AVFilterLink **outputs;         ///< array of pointers to output links
659
660     void *priv;                     ///< private data for use by the filter
661
662     unsigned nb_inputs;             ///< number of input pads
663     unsigned nb_outputs;            ///< number of output pads
664
665     struct AVFilterCommand *command_queue;
666 };
667
668 #if FF_API_PACKING
669 enum AVFilterPacking {
670     AVFILTER_PACKED = 0,
671     AVFILTER_PLANAR,
672 };
673 #endif
674
675 /**
676  * A link between two filters. This contains pointers to the source and
677  * destination filters between which this link exists, and the indexes of
678  * the pads involved. In addition, this link also contains the parameters
679  * which have been negotiated and agreed upon between the filter, such as
680  * image dimensions, format, etc.
681  */
682 struct AVFilterLink {
683     AVFilterContext *src;       ///< source filter
684     AVFilterPad *srcpad;        ///< output pad on the source filter
685
686     AVFilterContext *dst;       ///< dest filter
687     AVFilterPad *dstpad;        ///< input pad on the dest filter
688
689     /** stage of the initialization of the link properties (dimensions, etc) */
690     enum {
691         AVLINK_UNINIT = 0,      ///< not started
692         AVLINK_STARTINIT,       ///< started, but incomplete
693         AVLINK_INIT             ///< complete
694     } init_state;
695
696     enum AVMediaType type;      ///< filter media type
697
698     /* These parameters apply only to video */
699     int w;                      ///< agreed upon image width
700     int h;                      ///< agreed upon image height
701     AVRational sample_aspect_ratio; ///< agreed upon sample aspect ratio
702     /* These parameters apply only to audio */
703     uint64_t channel_layout;    ///< channel layout of current buffer (see libavutil/audioconvert.h)
704 #if FF_API_SAMPLERATE64
705     int64_t sample_rate;        ///< samples per second
706 #else
707     int sample_rate;            ///< samples per second
708 #endif
709 #if FF_API_PACKING
710     int planar;                 ///< agreed upon packing mode of audio buffers. true if planar.
711 #endif
712
713     int format;                 ///< agreed upon media format
714
715     /**
716      * Lists of formats and channel layouts supported by the input and output
717      * filters respectively. These lists are used for negotiating the format
718      * to actually be used, which will be loaded into the format and
719      * channel_layout members, above, when chosen.
720      *
721      */
722     AVFilterFormats *in_formats;
723     AVFilterFormats *out_formats;
724
725 #if FF_API_PACKING
726     AVFilterFormats *in_packing;
727     AVFilterFormats *out_packing;
728 #endif
729
730     /**
731      * The buffer reference currently being sent across the link by the source
732      * filter. This is used internally by the filter system to allow
733      * automatic copying of buffers which do not have sufficient permissions
734      * for the destination. This should not be accessed directly by the
735      * filters.
736      */
737     AVFilterBufferRef *src_buf;
738
739     AVFilterBufferRef *cur_buf;
740     AVFilterBufferRef *out_buf;
741
742     /**
743      * Define the time base used by the PTS of the frames/samples
744      * which will pass through this link.
745      * During the configuration stage, each filter is supposed to
746      * change only the output timebase, while the timebase of the
747      * input link is assumed to be an unchangeable property.
748      */
749     AVRational time_base;
750
751     /*****************************************************************
752      * All fields below this line are not part of the public API. They
753      * may not be used outside of libavfilter and can be changed and
754      * removed at will.
755      * New public fields should be added right above.
756      *****************************************************************
757      */
758     /**
759      * Lists of channel layouts and sample rates used for automatic
760      * negotiation.
761      */
762     AVFilterFormats  *in_samplerates;
763     AVFilterFormats *out_samplerates;
764     struct AVFilterChannelLayouts  *in_channel_layouts;
765     struct AVFilterChannelLayouts *out_channel_layouts;
766
767     /**
768      * Audio only, the destination filter sets this to a non-zero value to
769      * request that buffers with the given number of samples should be sent to
770      * it. AVFilterPad.needs_fifo must also be set on the corresponding input
771      * pad.
772      * Last buffer before EOF will be padded with silence.
773      */
774     int request_samples;
775
776     struct AVFilterPool *pool;
777
778     /**
779      * Graph the filter belongs to.
780      */
781     struct AVFilterGraph *graph;
782
783     /**
784      * Current timestamp of the link, as defined by the most recent
785      * frame(s), in AV_TIME_BASE units.
786      */
787     int64_t current_pts;
788
789     /**
790      * Index in the age array.
791      */
792     int age_index;
793
794     /**
795      * Frame rate of the stream on the link, or 1/0 if unknown;
796      * if left to 0/0, will be automatically be copied from the first input
797      * of the source filter if it exists.
798      *
799      * Sources should set it to the best estimation of the real frame rate.
800      * Filters should update it if necessary depending on their function.
801      * Sinks can use it to set a default output frame rate.
802      * It is similar to the r_frae_rate field in AVStream.
803      */
804     AVRational frame_rate;
805 };
806
807 /**
808  * Link two filters together.
809  *
810  * @param src    the source filter
811  * @param srcpad index of the output pad on the source filter
812  * @param dst    the destination filter
813  * @param dstpad index of the input pad on the destination filter
814  * @return       zero on success
815  */
816 int avfilter_link(AVFilterContext *src, unsigned srcpad,
817                   AVFilterContext *dst, unsigned dstpad);
818
819 /**
820  * Free the link in *link, and set its pointer to NULL.
821  */
822 void avfilter_link_free(AVFilterLink **link);
823
824 /**
825  * Negotiate the media format, dimensions, etc of all inputs to a filter.
826  *
827  * @param filter the filter to negotiate the properties for its inputs
828  * @return       zero on successful negotiation
829  */
830 int avfilter_config_links(AVFilterContext *filter);
831
832 #if FF_API_FILTERS_PUBLIC
833 attribute_deprecated
834 AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms,
835                                           int w, int h);
836 #endif
837
838 /**
839  * Create a buffer reference wrapped around an already allocated image
840  * buffer.
841  *
842  * @param data pointers to the planes of the image to reference
843  * @param linesize linesizes for the planes of the image to reference
844  * @param perms the required access permissions
845  * @param w the width of the image specified by the data and linesize arrays
846  * @param h the height of the image specified by the data and linesize arrays
847  * @param format the pixel format of the image specified by the data and linesize arrays
848  */
849 AVFilterBufferRef *
850 avfilter_get_video_buffer_ref_from_arrays(uint8_t * const data[4], const int linesize[4], int perms,
851                                           int w, int h, enum PixelFormat format);
852
853 /**
854  * Create an audio buffer reference wrapped around an already
855  * allocated samples buffer.
856  *
857  * @param data           pointers to the samples plane buffers
858  * @param linesize       linesize for the samples plane buffers
859  * @param perms          the required access permissions
860  * @param nb_samples     number of samples per channel
861  * @param sample_fmt     the format of each sample in the buffer to allocate
862  * @param channel_layout the channel layout of the buffer
863  */
864 AVFilterBufferRef *avfilter_get_audio_buffer_ref_from_arrays(uint8_t **data,
865                                                              int linesize,
866                                                              int perms,
867                                                              int nb_samples,
868                                                              enum AVSampleFormat sample_fmt,
869                                                              uint64_t channel_layout);
870
871 #if FF_API_FILTERS_PUBLIC
872 /**
873  * Request an input frame from the filter at the other end of the link.
874  *
875  * @param link the input link
876  * @return     zero on success or a negative error code; in particular:
877  *             AVERROR_EOF means that the end of frames have been reached;
878  *             AVERROR(EAGAIN) means that no frame could be immediately
879  *             produced.
880  */
881 int avfilter_request_frame(AVFilterLink *link);
882
883 attribute_deprecated
884 int avfilter_poll_frame(AVFilterLink *link);
885
886 attribute_deprecated
887 void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
888
889 /**
890  * Notify the next filter that the current frame has finished.
891  *
892  * @param link the output link the frame was sent over
893  */
894 attribute_deprecated
895 void avfilter_end_frame(AVFilterLink *link);
896 attribute_deprecated
897 void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
898 #endif
899
900 #define AVFILTER_CMD_FLAG_ONE   1 ///< Stop once a filter understood the command (for target=all for example), fast filters are favored automatically
901 #define AVFILTER_CMD_FLAG_FAST  2 ///< Only execute command when its fast (like a video out that supports contrast adjustment in hw)
902
903 /**
904  * Make the filter instance process a command.
905  * It is recommended to use avfilter_graph_send_command().
906  */
907 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags);
908
909 /** Initialize the filter system. Register all builtin filters. */
910 void avfilter_register_all(void);
911
912 /** Uninitialize the filter system. Unregister all filters. */
913 void avfilter_uninit(void);
914
915 /**
916  * Register a filter. This is only needed if you plan to use
917  * avfilter_get_by_name later to lookup the AVFilter structure by name. A
918  * filter can still by instantiated with avfilter_open even if it is not
919  * registered.
920  *
921  * @param filter the filter to register
922  * @return 0 if the registration was successful, a negative value
923  * otherwise
924  */
925 int avfilter_register(AVFilter *filter);
926
927 /**
928  * Get a filter definition matching the given name.
929  *
930  * @param name the filter name to find
931  * @return     the filter definition, if any matching one is registered.
932  *             NULL if none found.
933  */
934 AVFilter *avfilter_get_by_name(const char *name);
935
936 /**
937  * If filter is NULL, returns a pointer to the first registered filter pointer,
938  * if filter is non-NULL, returns the next pointer after filter.
939  * If the returned pointer points to NULL, the last registered filter
940  * was already reached.
941  */
942 AVFilter **av_filter_next(AVFilter **filter);
943
944 /**
945  * Create a filter instance.
946  *
947  * @param filter_ctx put here a pointer to the created filter context
948  * on success, NULL on failure
949  * @param filter    the filter to create an instance of
950  * @param inst_name Name to give to the new instance. Can be NULL for none.
951  * @return >= 0 in case of success, a negative error code otherwise
952  */
953 int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
954
955 /**
956  * Initialize a filter.
957  *
958  * @param filter the filter to initialize
959  * @param args   A string of parameters to use when initializing the filter.
960  *               The format and meaning of this string varies by filter.
961  * @param opaque Any extra non-string data needed by the filter. The meaning
962  *               of this parameter varies by filter.
963  * @return       zero on success
964  */
965 int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
966
967 /**
968  * Free a filter context.
969  *
970  * @param filter the filter to free
971  */
972 void avfilter_free(AVFilterContext *filter);
973
974 /**
975  * Insert a filter in the middle of an existing link.
976  *
977  * @param link the link into which the filter should be inserted
978  * @param filt the filter to be inserted
979  * @param filt_srcpad_idx the input pad on the filter to connect
980  * @param filt_dstpad_idx the output pad on the filter to connect
981  * @return     zero on success
982  */
983 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
984                            unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
985
986 #if FF_API_FILTERS_PUBLIC
987 attribute_deprecated
988 void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
989                          AVFilterPad **pads, AVFilterLink ***links,
990                          AVFilterPad *newpad);
991
992 attribute_deprecated
993 void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
994                            AVFilterPad *p);
995 attribute_deprecated
996 void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
997                             AVFilterPad *p);
998 #endif
999
1000 #endif /* AVFILTER_AVFILTER_H */