avfilter.h 31.6 KB
Newer Older
Vitor Sessak's avatar
Vitor Sessak committed
1
/*
2
 * filter layer
Vitor Sessak's avatar
Vitor Sessak committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * copyright (c) 2007 Bobby Bingham
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22 23
#ifndef AVFILTER_AVFILTER_H
#define AVFILTER_AVFILTER_H
Vitor Sessak's avatar
Vitor Sessak committed
24

25 26
#include "libavutil/avutil.h"

27
#define LIBAVFILTER_VERSION_MAJOR  1
28
#define LIBAVFILTER_VERSION_MINOR 45
29
#define LIBAVFILTER_VERSION_MICRO  0
30 31 32 33 34 35 36 37

#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
                                               LIBAVFILTER_VERSION_MINOR, \
                                               LIBAVFILTER_VERSION_MICRO)
#define LIBAVFILTER_VERSION     AV_VERSION(LIBAVFILTER_VERSION_MAJOR,   \
                                           LIBAVFILTER_VERSION_MINOR,   \
                                           LIBAVFILTER_VERSION_MICRO)
#define LIBAVFILTER_BUILD       LIBAVFILTER_VERSION_INT
Víctor Paesa's avatar
Víctor Paesa committed
38

39
#include <stddef.h>
40
#include "libavcodec/avcodec.h"
Vitor Sessak's avatar
Vitor Sessak committed
41

42
/**
43
 * Return the LIBAVFILTER_VERSION_INT constant.
44 45 46
 */
unsigned avfilter_version(void);

47
/**
48
 * Return the libavfilter build-time configuration.
49
 */
50
const char *avfilter_configuration(void);
51 52

/**
53
 * Return the libavfilter license.
54
 */
55
const char *avfilter_license(void);
56 57


Vitor Sessak's avatar
Vitor Sessak committed
58 59 60 61 62
typedef struct AVFilterContext AVFilterContext;
typedef struct AVFilterLink    AVFilterLink;
typedef struct AVFilterPad     AVFilterPad;

/**
63
 * A reference-counted buffer data type used by the filter system. Filters
Vitor Sessak's avatar
Vitor Sessak committed
64
 * should not store pointers to this structure directly, but instead use the
65
 * AVFilterBufferRef structure below.
Vitor Sessak's avatar
Vitor Sessak committed
66
 */
67
typedef struct AVFilterBuffer {
68 69
    uint8_t *data[8];           ///< buffer data for each plane/channel
    int linesize[8];            ///< number of bytes per line
Vitor Sessak's avatar
Vitor Sessak committed
70

71
    unsigned refcount;          ///< number of references to this buffer
72 73

    /** private data to be used by a custom free function */
Vitor Sessak's avatar
Vitor Sessak committed
74
    void *priv;
75
    /**
76
     * A pointer to the function to deallocate this buffer if the default
77
     * function is not sufficient. This could, for example, add the memory
78 79 80
     * back into a memory pool to be reused later without the overhead of
     * reallocating it from scratch.
     */
81
    void (*free)(struct AVFilterBuffer *buf);
82
} AVFilterBuffer;
Vitor Sessak's avatar
Vitor Sessak committed
83

84 85 86 87 88 89
#define AV_PERM_READ     0x01   ///< can read from the buffer
#define AV_PERM_WRITE    0x02   ///< can write to the buffer
#define AV_PERM_PRESERVE 0x04   ///< nobody else can overwrite the buffer
#define AV_PERM_REUSE    0x08   ///< can output the buffer multiple times, with the same contents each time
#define AV_PERM_REUSE2   0x10   ///< can output the buffer multiple times, modified each time

90 91 92 93 94 95 96 97 98 99 100 101 102
/**
 * Audio specific properties in a reference to an AVFilterBuffer. Since
 * AVFilterBufferRef is common to different media formats, audio specific
 * per reference properties must be separated out.
 */
typedef struct AVFilterBufferRefAudioProps {
    int64_t channel_layout;     ///< channel layout of audio buffer
    int samples_nb;             ///< number of audio samples
    int size;                   ///< audio buffer size
    uint32_t sample_rate;       ///< audio buffer sample rate
    int planar;                 ///< audio buffer - planar or packed
} AVFilterBufferRefAudioProps;

103 104 105 106 107
/**
 * Video specific properties in a reference to an AVFilterBuffer. Since
 * AVFilterBufferRef is common to different media formats, video specific
 * per reference properties must be separated out.
 */
108
typedef struct AVFilterBufferRefVideoProps {
109 110 111 112 113 114 115
    int w;                      ///< image width
    int h;                      ///< image height
    AVRational pixel_aspect;    ///< pixel aspect ratio
    int interlaced;             ///< is frame interlaced
    int top_field_first;        ///< field order
} AVFilterBufferRefVideoProps;

Vitor Sessak's avatar
Vitor Sessak committed
116
/**
117
 * A reference to an AVFilterBuffer. Since filters can manipulate the origin of
118
 * a buffer to, for example, crop image without any memcpy, the buffer origin
119
 * and dimensions are per-reference properties. Linesize is also useful for
120
 * image flipping, frame to field filters, etc, and so is also per-reference.
Vitor Sessak's avatar
Vitor Sessak committed
121
 *
Vitor Sessak's avatar
Vitor Sessak committed
122
 * TODO: add anything necessary for frame reordering
Vitor Sessak's avatar
Vitor Sessak committed
123
 */
124
typedef struct AVFilterBufferRef {
125
    AVFilterBuffer *buf;        ///< the buffer that this is a reference to
126
    uint8_t *data[8];           ///< picture/audio data for each plane
127
    int linesize[8];            ///< number of bytes per line
128
    int format;                 ///< media format
Vitor Sessak's avatar
Vitor Sessak committed
129

130
    int64_t pts;                ///< presentation timestamp in units of 1/AV_TIME_BASE
131
    int64_t pos;                ///< byte position in stream, -1 if unknown
Vitor Sessak's avatar
Vitor Sessak committed
132

133
    int perms;                  ///< permissions, see the AV_PERM_* flags
134

135 136
    enum AVMediaType type;      ///< media type of buffer data
    AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
137
    AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
138
} AVFilterBufferRef;
Vitor Sessak's avatar
Vitor Sessak committed
139

140
/**
141
 * Copy properties of src to dst, without copying the actual data
142
 */
143
static inline void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
144
{
145
    // copy common properties
146 147
    dst->pts             = src->pts;
    dst->pos             = src->pos;
148 149 150

    switch (src->type) {
    case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
151
    case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
152
    }
153 154
}

Vitor Sessak's avatar
Vitor Sessak committed
155
/**
156
 * Add a new reference to a buffer.
157
 *
158
 * @param ref   an existing reference to the buffer
159
 * @param pmask a bitmask containing the allowable permissions in the new
160
 *              reference
161
 * @return      a new reference to the buffer with the same properties as the
162
 *              old, excluding any permissions denied by pmask
Vitor Sessak's avatar
Vitor Sessak committed
163
 */
164
AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
Vitor Sessak's avatar
Vitor Sessak committed
165 166

/**
167 168
 * Remove a reference to a buffer. If this is the last reference to the
 * buffer, the buffer itself is also automatically freed.
169
 *
170
 * @param ref reference to the buffer
Vitor Sessak's avatar
Vitor Sessak committed
171
 */
172
void avfilter_unref_buffer(AVFilterBufferRef *ref);
Vitor Sessak's avatar
Vitor Sessak committed
173

Vitor Sessak's avatar
Vitor Sessak committed
174 175 176 177 178 179 180
/**
 * A list of supported formats for one end of a filter link. This is used
 * during the format negotiation process to try to pick the best format to
 * use to minimize the number of necessary conversions. Each filter gives a
 * list of the formats supported by each input and output pad. The list
 * given for each pad need not be distinct - they may be references to the
 * same list of formats, as is often the case when a filter supports multiple
Stefano Sabatini's avatar
Stefano Sabatini committed
181
 * formats, but will always output the same format as it is given in input.
Vitor Sessak's avatar
Vitor Sessak committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
 *
 * In this way, a list of possible input formats and a list of possible
 * output formats are associated with each link. When a set of formats is
 * negotiated over a link, the input and output lists are merged to form a
 * new list containing only the common elements of each list. In the case
 * that there were no common elements, a format conversion is necessary.
 * Otherwise, the lists are merged, and all other links which reference
 * either of the format lists involved in the merge are also affected.
 *
 * For example, consider the filter chain:
 * filter (a) --> (b) filter (b) --> (c) filter
 *
 * where the letters in parenthesis indicate a list of formats supported on
 * the input or output of the link. Suppose the lists are as follows:
 * (a) = {A, B}
 * (b) = {A, B, C}
 * (c) = {B, C}
 *
 * First, the first link's lists are merged, yielding:
 * filter (a) --> (a) filter (a) --> (c) filter
 *
 * Notice that format list (b) now refers to the same list as filter list (a).
 * Next, the lists for the second link are merged, yielding:
 * filter (a) --> (a) filter (a) --> (a) filter
 *
 * where (a) = {B}.
 *
 * Unfortunately, when the format lists at the two ends of a link are merged,
 * we must ensure that all links which reference either pre-merge format list
 * get updated as well. Therefore, we have the format list structure store a
 * pointer to each of the pointers to itself.
 */
214
typedef struct AVFilterFormats {
Vitor Sessak's avatar
Vitor Sessak committed
215
    unsigned format_count;      ///< number of formats
216
    int *formats;               ///< list of media formats
Vitor Sessak's avatar
Vitor Sessak committed
217 218

    unsigned refcount;          ///< number of references to this list
219 220
    struct AVFilterFormats ***refs; ///< references to this list
}  AVFilterFormats;;
Vitor Sessak's avatar
Vitor Sessak committed
221

222
/**
223
 * Create a list of supported formats. This is intended for use in
224
 * AVFilter->query_formats().
225
 *
226
 * @param fmts list of media formats, terminated by -1
227 228
 * @return the format list, with no existing references
 */
229
AVFilterFormats *avfilter_make_format_list(const int *fmts);
230

231
/**
232
 * Add fmt to the list of media formats contained in *avff.
233 234
 * If *avff is NULL the function allocates the filter formats struct
 * and puts its pointer in *avff.
235 236 237 238
 *
 * @return a non negative value in case of success, or a negative
 * value corresponding to an AVERROR code in case of error
 */
239
int avfilter_add_format(AVFilterFormats **avff, int fmt);
240

Vitor Sessak's avatar
Vitor Sessak committed
241
/**
242
 * Return a list of all formats supported by FFmpeg for the given media type.
Vitor Sessak's avatar
Vitor Sessak committed
243
 */
244
AVFilterFormats *avfilter_all_formats(enum AVMediaType type);
Vitor Sessak's avatar
Vitor Sessak committed
245 246

/**
247
 * Return a format list which contains the intersection of the formats of
248 249
 * a and b. Also, all the references of a, all the references of b, and
 * a and b themselves will be deallocated.
Vitor Sessak's avatar
Vitor Sessak committed
250 251 252 253 254 255
 *
 * If a and b do not share any common formats, neither is modified, and NULL
 * is returned.
 */
AVFilterFormats *avfilter_merge_formats(AVFilterFormats *a, AVFilterFormats *b);

256
/**
257
 * Add *ref as a new reference to formats.
258 259
 * That is the pointers will point like in the ascii art below:
 *   ________
260 261 262 263
 *  |formats |<--------.
 *  |  ____  |     ____|___________________
 *  | |refs| |    |  __|_
 *  | |* * | |    | |  | |  AVFilterLink
264
 *  | |* *--------->|*ref|
265 266
 *  | |____| |    | |____|
 *  |________|    |________________________
267
 */
268
void avfilter_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
Vitor Sessak's avatar
Vitor Sessak committed
269 270

/**
271
 * If *ref is non-NULL, remove *ref as a reference to the format list
272 273
 * it currently points to, deallocates that list if this was the last
 * reference, and sets *ref to NULL.
274 275 276 277 278 279 280 281 282 283
 *
 *         Before                                 After
 *   ________                               ________         NULL
 *  |formats |<--------.                   |formats |         ^
 *  |  ____  |     ____|________________   |  ____  |     ____|________________
 *  | |refs| |    |  __|_                  | |refs| |    |  __|_
 *  | |* * | |    | |  | |  AVFilterLink   | |* * | |    | |  | |  AVFilterLink
 *  | |* *--------->|*ref|                 | |*   | |    | |*ref|
 *  | |____| |    | |____|                 | |____| |    | |____|
 *  |________|    |_____________________   |________|    |_____________________
Vitor Sessak's avatar
Vitor Sessak committed
284 285 286
 */
void avfilter_formats_unref(AVFilterFormats **ref);

287 288 289 290
/**
 *
 *         Before                                 After
 *   ________                         ________
291
 *  |formats |<---------.            |formats |<---------.
292 293 294 295 296 297 298 299
 *  |  ____  |       ___|___         |  ____  |       ___|___
 *  | |refs| |      |   |   |        | |refs| |      |   |   |   NULL
 *  | |* *--------->|*oldref|        | |* *--------->|*newref|     ^
 *  | |* * | |      |_______|        | |* * | |      |_______|  ___|___
 *  | |____| |                       | |____| |                |   |   |
 *  |________|                       |________|                |*oldref|
 *                                                             |_______|
 */
Michael Niedermayer's avatar
Michael Niedermayer committed
300 301 302
void avfilter_formats_changeref(AVFilterFormats **oldref,
                                AVFilterFormats **newref);

303
/**
304
 * A filter pad used for either input or output.
305
 */
306
struct AVFilterPad {
Vitor Sessak's avatar
Vitor Sessak committed
307
    /**
308 309
     * Pad name. The name is unique among inputs and among outputs, but an
     * input may have the same name as an output. This may be NULL if this
310
     * pad has no need to ever be referenced by name.
Vitor Sessak's avatar
Vitor Sessak committed
311
     */
Vitor Sessak's avatar
Vitor Sessak committed
312
    const char *name;
Vitor Sessak's avatar
Vitor Sessak committed
313 314

    /**
315
     * AVFilterPad type. Only video supported now, hopefully someone will
Vitor Sessak's avatar
Vitor Sessak committed
316 317
     * add audio in the future.
     */
318
    enum AVMediaType type;
Vitor Sessak's avatar
Vitor Sessak committed
319

320
    /**
321
     * Minimum required permissions on incoming buffers. Any buffer with
322 323 324 325 326 327 328 329
     * insufficient permissions will be automatically copied by the filter
     * system to a new buffer which provides the needed access permissions.
     *
     * Input pads only.
     */
    int min_perms;

    /**
330
     * Permissions which are not accepted on incoming buffers. Any buffer
331 332
     * which has any of these permissions set will be automatically copied
     * by the filter system to a new buffer which does not have those
333
     * permissions. This can be used to easily disallow buffers with
334
     * AV_PERM_REUSE.
335 336 337 338 339
     *
     * Input pads only.
     */
    int rej_perms;

Vitor Sessak's avatar
Vitor Sessak committed
340
    /**
341
     * Callback called before passing the first slice of a new frame. If
Vitor Sessak's avatar
Vitor Sessak committed
342 343
     * NULL, the filter layer will default to storing a reference to the
     * picture inside the link structure.
344 345
     *
     * Input video pads only.
Vitor Sessak's avatar
Vitor Sessak committed
346
     */
347
    void (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
Vitor Sessak's avatar
Vitor Sessak committed
348 349

    /**
350
     * Callback function to get a video buffer. If NULL, the filter system will
351
     * use avfilter_default_get_video_buffer().
352 353
     *
     * Input video pads only.
Vitor Sessak's avatar
Vitor Sessak committed
354
     */
355
    AVFilterBufferRef *(*get_video_buffer)(AVFilterLink *link, int perms, int w, int h);
Vitor Sessak's avatar
Vitor Sessak committed
356

357 358 359 360 361 362 363 364 365 366
    /**
     * Callback function to get an audio buffer. If NULL, the filter system will
     * use avfilter_default_get_audio_buffer().
     *
     * Input audio pads only.
     */
    AVFilterBufferRef *(*get_audio_buffer)(AVFilterLink *link, int perms,
                                           enum SampleFormat sample_fmt, int size,
                                           int64_t channel_layout, int planar);

Vitor Sessak's avatar
Vitor Sessak committed
367
    /**
368
     * Callback called after the slices of a frame are completely sent. If
Vitor Sessak's avatar
Vitor Sessak committed
369 370
     * NULL, the filter layer will default to releasing the reference stored
     * in the link structure during start_frame().
371 372
     *
     * Input video pads only.
Vitor Sessak's avatar
Vitor Sessak committed
373 374 375 376
     */
    void (*end_frame)(AVFilterLink *link);

    /**
377
     * Slice drawing callback. This is where a filter receives video data
378 379 380
     * and should do its processing.
     *
     * Input video pads only.
Vitor Sessak's avatar
Vitor Sessak committed
381
     */
382
    void (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
Vitor Sessak's avatar
Vitor Sessak committed
383

384 385 386 387 388 389 390 391
    /**
     * Samples filtering callback. This is where a filter receives audio data
     * and should do its processing.
     *
     * Input audio pads only.
     */
    void (*filter_samples)(AVFilterLink *link, AVFilterBufferRef *samplesref);

392
    /**
393
     * Frame poll callback. This returns the number of immediately available
394
     * samples. It should return a positive value if the next request_frame()
395 396 397 398 399 400 401 402
     * is guaranteed to return one frame (with no delay).
     *
     * Defaults to just calling the source poll_frame() method.
     *
     * Output video pads only.
     */
    int (*poll_frame)(AVFilterLink *link);

Vitor Sessak's avatar
Vitor Sessak committed
403
    /**
404 405
     * Frame request callback. A call to this should result in at least one
     * frame being output over the given link. This should return zero on
406 407 408
     * success, and another value on error.
     *
     * Output video pads only.
Vitor Sessak's avatar
Vitor Sessak committed
409
     */
410
    int (*request_frame)(AVFilterLink *link);
Vitor Sessak's avatar
Vitor Sessak committed
411 412

    /**
413 414 415
     * Link configuration callback.
     *
     * For output pads, this should set the link properties such as
416
     * width/height. This should NOT set the format property - that is
417 418
     * negotiated between filters by the filter system using the
     * query_formats() callback before this function is called.
419 420 421
     *
     * For input pads, this should check the properties of the link, and update
     * the filter's internal state as necessary.
422 423 424
     *
     * For both input and output filters, this should return zero on success,
     * and another value on error.
Vitor Sessak's avatar
Vitor Sessak committed
425
     */
426
    int (*config_props)(AVFilterLink *link);
Vitor Sessak's avatar
Vitor Sessak committed
427 428
};

429
/** default handler for start_frame() for video inputs */
430
void avfilter_default_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
431

432
/** default handler for draw_slice() for video inputs */
433
void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
434

435
/** default handler for end_frame() for video inputs */
Vitor Sessak's avatar
Vitor Sessak committed
436
void avfilter_default_end_frame(AVFilterLink *link);
437

438 439 440 441
/** default handler for filter_samples() for audio inputs */
void avfilter_default_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref);

/** default handler for config_props() for audio/video outputs */
442
int avfilter_default_config_output_link(AVFilterLink *link);
443

444
/** default handler for config_props() for audio/video inputs */
445
int avfilter_default_config_input_link (AVFilterLink *link);
446

447
/** default handler for get_video_buffer() for video inputs */
448
AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link,
449
                                                     int perms, int w, int h);
450 451 452 453 454 455

/** default handler for get_audio_buffer() for audio inputs */
AVFilterBufferRef *avfilter_default_get_audio_buffer(AVFilterLink *link, int perms,
                                                     enum SampleFormat sample_fmt, int size,
                                                     int64_t channel_layout, int planar);

Vitor Sessak's avatar
Vitor Sessak committed
456 457 458 459 460 461
/**
 * A helper for query_formats() which sets all links to the same list of
 * formats. If there are no links hooked to this filter, the list of formats is
 * freed.
 */
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats);
462

Vitor Sessak's avatar
Vitor Sessak committed
463 464
/** Default handler for query_formats() */
int avfilter_default_query_formats(AVFilterContext *ctx);
Vitor Sessak's avatar
Vitor Sessak committed
465

466
/** start_frame() handler for filters which simply pass video along */
467
void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
468 469 470 471 472 473 474

/** draw_slice() handler for filters which simply pass video along */
void avfilter_null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);

/** end_frame() handler for filters which simply pass video along */
void avfilter_null_end_frame(AVFilterLink *link);

475 476 477
/** filter_samples() handler for filters which simply pass audio along */
void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref);

478
/** get_video_buffer() handler for filters which simply pass video along */
479
AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link,
480 481
                                                  int perms, int w, int h);

482 483 484 485 486
/** get_audio_buffer() handler for filters which simply pass audio along */
AVFilterBufferRef *avfilter_null_get_audio_buffer(AVFilterLink *link, int perms,
                                                  enum SampleFormat sample_fmt, int size,
                                                  int64_t channel_layout, int planar);

487
/**
488
 * Filter definition. This defines the pads a filter contains, and all the
489 490
 * callback functions used to interact with the filter.
 */
491
typedef struct AVFilter {
Vitor Sessak's avatar
Vitor Sessak committed
492
    const char *name;         ///< filter name
Vitor Sessak's avatar
Vitor Sessak committed
493

494
    int priv_size;      ///< size of private data to allocate for the filter
Vitor Sessak's avatar
Vitor Sessak committed
495

496
    /**
497 498
     * Filter initialization function. Args contains the user-supplied
     * parameters. FIXME: maybe an AVOption-based system would be better?
499 500
     * opaque is data provided by the code requesting creation of the filter,
     * and is used to pass data to the filter.
501
     */
502
    int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
503 504

    /**
505
     * Filter uninitialization function. Should deallocate any memory held
506
     * by the filter, release any buffer references, etc. This does not need
507 508
     * to deallocate the AVFilterContext->priv memory itself.
     */
Vitor Sessak's avatar
Vitor Sessak committed
509 510
    void (*uninit)(AVFilterContext *ctx);

Vitor Sessak's avatar
Vitor Sessak committed
511
    /**
512
     * Queries formats supported by the filter and its pads, and sets the
Vitor Sessak's avatar
Vitor Sessak committed
513 514 515
     * in_formats for links connected to its output pads, and out_formats
     * for links connected to its input pads.
     *
516 517
     * @return zero on success, a negative value corresponding to an
     * AVERROR code otherwise
Vitor Sessak's avatar
Vitor Sessak committed
518 519 520
     */
    int (*query_formats)(AVFilterContext *);

521 522
    const AVFilterPad *inputs;  ///< NULL terminated list of inputs. NULL if none
    const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
523 524 525 526 527 528

    /**
     * A description for the filter. You should use the
     * NULL_IF_CONFIG_SMALL() macro to define it.
     */
    const char *description;
Vitor Sessak's avatar
Vitor Sessak committed
529 530
} AVFilter;

531
/** An instance of a filter */
532
struct AVFilterContext {
533
    const AVClass *av_class;              ///< needed for av_log()
Vitor Sessak's avatar
Vitor Sessak committed
534

535
    AVFilter *filter;               ///< the AVFilter of which this is an instance
Vitor Sessak's avatar
Vitor Sessak committed
536

537
    char *name;                     ///< name of this filter instance
538

539 540 541
    unsigned input_count;           ///< number of input pads
    AVFilterPad   *input_pads;      ///< array of input pads
    AVFilterLink **inputs;          ///< array of pointers to input links
Vitor Sessak's avatar
Vitor Sessak committed
542

543 544 545
    unsigned output_count;          ///< number of output pads
    AVFilterPad   *output_pads;     ///< array of output pads
    AVFilterLink **outputs;         ///< array of pointers to output links
Vitor Sessak's avatar
Vitor Sessak committed
546

547
    void *priv;                     ///< private data for use by the filter
Vitor Sessak's avatar
Vitor Sessak committed
548 549
};

550
/**
551
 * A link between two filters. This contains pointers to the source and
552
 * destination filters between which this link exists, and the indexes of
553
 * the pads involved. In addition, this link also contains the parameters
554
 * which have been negotiated and agreed upon between the filter, such as
555
 * image dimensions, format, etc.
556
 */
557
struct AVFilterLink {
558 559
    AVFilterContext *src;       ///< source filter
    unsigned int srcpad;        ///< index of the output pad on the source filter
Vitor Sessak's avatar
Vitor Sessak committed
560

561 562
    AVFilterContext *dst;       ///< dest filter
    unsigned int dstpad;        ///< index of the input pad on the dest filter
Vitor Sessak's avatar
Vitor Sessak committed
563

564 565 566 567 568 569 570
    /** stage of the initialization of the link properties (dimensions, etc) */
    enum {
        AVLINK_UNINIT = 0,      ///< not started
        AVLINK_STARTINIT,       ///< started, but incomplete
        AVLINK_INIT             ///< complete
    } init_state;

571 572
    enum AVMediaType type;      ///< filter media type

573
    /* These two parameters apply only to video */
574 575
    int w;                      ///< agreed upon image width
    int h;                      ///< agreed upon image height
576 577 578 579
    /* These two parameters apply only to audio */
    int64_t channel_layout;     ///< channel layout of current buffer (see avcodec.h)
    int64_t sample_rate;        ///< samples per second

580
    int format;                 ///< agreed upon media format
Vitor Sessak's avatar
Vitor Sessak committed
581

Vitor Sessak's avatar
Vitor Sessak committed
582 583 584 585 586 587 588 589
    /**
     * Lists of formats supported by the input and output filters respectively.
     * These lists are used for negotiating the format to actually be used,
     * which will be loaded into the format member, above, when chosen.
     */
    AVFilterFormats *in_formats;
    AVFilterFormats *out_formats;

590
    /**
591
     * The buffer reference currently being sent across the link by the source
592
     * filter. This is used internally by the filter system to allow
593
     * automatic copying of buffers which do not have sufficient permissions
594
     * for the destination. This should not be accessed directly by the
595 596
     * filters.
     */
597
    AVFilterBufferRef *src_buf;
598

599 600
    AVFilterBufferRef *cur_buf;
    AVFilterBufferRef *out_buf;
Vitor Sessak's avatar
Vitor Sessak committed
601 602
};

603
/**
604
 * Link two filters together.
605
 *
606 607 608 609 610
 * @param src    the source filter
 * @param srcpad index of the output pad on the source filter
 * @param dst    the destination filter
 * @param dstpad index of the input pad on the destination filter
 * @return       zero on success
611
 */
Vitor Sessak's avatar
Vitor Sessak committed
612 613 614
int avfilter_link(AVFilterContext *src, unsigned srcpad,
                  AVFilterContext *dst, unsigned dstpad);

615
/**
616
 * Negotiate the media format, dimensions, etc of all inputs to a filter.
617
 *
618 619
 * @param filter the filter to negotiate the properties for its inputs
 * @return       zero on successful negotiation
620
 */
621
int avfilter_config_links(AVFilterContext *filter);
622

623
/**
624
 * Request a picture buffer with a specific set of permissions.
625
 *
626
 * @param link  the output link to the filter from which the buffer will
627
 *              be requested
628
 * @param perms the required access permissions
629 630
 * @param w     the minimum width of the buffer to allocate
 * @param h     the minimum height of the buffer to allocate
631 632
 * @return      A reference to the buffer. This must be unreferenced with
 *              avfilter_unref_buffer when you are finished with it.
633
 */
634
AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms,
635
                                          int w, int h);
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
/**
 * Request an audio samples buffer with a specific set of permissions.
 *
 * @param link           the output link to the filter from which the buffer will
 *                       be requested
 * @param perms          the required access permissions
 * @param sample_fmt     the format of each sample in the buffer to allocate
 * @param size           the buffer size in bytes
 * @param channel_layout the number and type of channels per sample in the buffer to allocate
 * @param planar         audio data layout - planar or packed
 * @return               A reference to the samples. This must be unreferenced with
 *                       avfilter_unref_samples when you are finished with it.
 */
AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
                                             enum SampleFormat sample_fmt, int size,
                                             int64_t channel_layout, int planar);

654
/**
655
 * Request an input frame from the filter at the other end of the link.
656
 *
657 658
 * @param link the input link
 * @return     zero on success
659
 */
Vitor Sessak's avatar
Vitor Sessak committed
660
int avfilter_request_frame(AVFilterLink *link);
661

662
/**
663
 * Poll a frame from the filter chain.
664
 *
665
 * @param  link the input link
666 667
 * @return the number of immediately available frames, a negative
 * number in case of error
668 669 670
 */
int avfilter_poll_frame(AVFilterLink *link);

671
/**
672
 * Notifie the next filter of the start of a frame.
673
 *
674
 * @param link   the output link the frame will be sent over
675
 * @param picref A reference to the frame about to be sent. The data for this
676
 *               frame need only be valid once draw_slice() is called for that
677
 *               portion. The receiving filter will free this reference when
678 679
 *               it no longer needs it.
 */
680
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
681 682

/**
683
 * Notifie the next filter that the current frame has finished.
684
 *
685
 * @param link the output link the frame was sent over
686
 */
Vitor Sessak's avatar
Vitor Sessak committed
687
void avfilter_end_frame(AVFilterLink *link);
688 689

/**
690
 * Send a slice to the next filter.
691 692 693 694 695
 *
 * Slices have to be provided in sequential order, either in
 * top-bottom or bottom-top order. If slices are provided in
 * non-sequential order the behavior of the function is undefined.
 *
696 697 698
 * @param link the output link over which the frame is being sent
 * @param y    offset in pixels from the top of the image for this slice
 * @param h    height of this slice in pixels
699 700 701 702
 * @param slice_dir the assumed direction for sending slices,
 *             from the top slice to the bottom slice if the value is 1,
 *             from the bottom slice to the top slice if the value is -1,
 *             for other values the behavior of the function is undefined.
703
 */
704
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
Vitor Sessak's avatar
Vitor Sessak committed
705

706 707 708 709 710 711 712 713 714 715
/**
 * Send a buffer of audio samples to the next filter.
 *
 * @param link       the output link over which the audio samples are being sent
 * @param samplesref a reference to the buffer of audio samples being sent. The
 *                   receiving filter will free this reference when it no longer
 *                   needs it or pass it on to the next filter.
 */
void avfilter_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref);

716
/** Initialize the filter system. Register all builtin filters. */
Vitor Sessak's avatar
Oops  
Vitor Sessak committed
717
void avfilter_register_all(void);
718

719
/** Uninitialize the filter system. Unregister all filters. */
Vitor Sessak's avatar
Vitor Sessak committed
720
void avfilter_uninit(void);
721 722

/**
723
 * Register a filter. This is only needed if you plan to use
724 725 726
 * avfilter_get_by_name later to lookup the AVFilter structure by name. A
 * filter can still by instantiated with avfilter_open even if it is not
 * registered.
727
 *
728
 * @param filter the filter to register
729 730
 * @return 0 if the registration was succesfull, a negative value
 * otherwise
731
 */
732
int avfilter_register(AVFilter *filter);
733 734

/**
735
 * Get a filter definition matching the given name.
736
 *
737 738
 * @param name the filter name to find
 * @return     the filter definition, if any matching one is registered.
739 740
 *             NULL if none found.
 */
Vitor Sessak's avatar
Vitor Sessak committed
741
AVFilter *avfilter_get_by_name(const char *name);
Vitor Sessak's avatar
Vitor Sessak committed
742

Stefano Sabatini's avatar
Stefano Sabatini committed
743 744 745 746 747 748 749 750
/**
 * If filter is NULL, returns a pointer to the first registered filter pointer,
 * if filter is non-NULL, returns the next pointer after filter.
 * If the returned pointer points to NULL, the last registered filter
 * was already reached.
 */
AVFilter **av_filter_next(AVFilter **filter);

751
/**
752
 * Create a filter instance.
753 754 755
 *
 * @param filter_ctx put here a pointer to the created filter context
 * on success, NULL on failure
756
 * @param filter    the filter to create an instance of
757
 * @param inst_name Name to give to the new instance. Can be NULL for none.
758
 * @return >= 0 in case of success, a negative error code otherwise
759
 */
760
int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
761 762

/**
763
 * Initialize a filter.
764
 *
765
 * @param filter the filter to initialize
766 767
 * @param args   A string of parameters to use when initializing the filter.
 *               The format and meaning of this string varies by filter.
768
 * @param opaque Any extra non-string data needed by the filter. The meaning
769
 *               of this parameter varies by filter.
770
 * @return       zero on success
771
 */
772
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
773 774

/**
775
 * Destroy a filter.
776
 *
777
 * @param filter the filter to destroy
778
 */
Vitor Sessak's avatar
Vitor Sessak committed
779 780
void avfilter_destroy(AVFilterContext *filter);

781
/**
782
 * Insert a filter in the middle of an existing link.