avfilter.h 32.0 KB
Newer Older
V
Vitor Sessak 已提交
1
/*
2
 * filter layer
V
Vitor Sessak 已提交
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
V
Vitor Sessak 已提交
24

25 26
#include "libavutil/avutil.h"

27
#define LIBAVFILTER_VERSION_MAJOR  1
28
#define LIBAVFILTER_VERSION_MINOR 49
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
Víctor Paesa 已提交
38

39
#include <stddef.h>
40
#include "libavcodec/avcodec.h"
V
Vitor Sessak 已提交
41

S
Stefano Sabatini 已提交
42
/**
43
 * Return the LIBAVFILTER_VERSION_INT constant.
S
Stefano Sabatini 已提交
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


V
Vitor Sessak 已提交
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
V
Vitor Sessak 已提交
64
 * should not store pointers to this structure directly, but instead use the
65
 * AVFilterBufferRef structure below.
V
Vitor Sessak 已提交
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
V
Vitor Sessak 已提交
70

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

    /** private data to be used by a custom free function */
V
Vitor Sessak 已提交
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;
V
Vitor Sessak 已提交
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;

V
Vitor Sessak 已提交
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.
V
Vitor Sessak 已提交
121
 *
V
Vitor Sessak 已提交
122
 * TODO: add anything necessary for frame reordering
V
Vitor Sessak 已提交
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
V
Vitor Sessak 已提交
129

130 131 132 133 134 135
    /**
     * presentation timestamp. The time unit may change during
     * filtering, as it is specified in the link and the filter code
     * may need to rescale the PTS accordingly.
     */
    int64_t pts;
136
    int64_t pos;                ///< byte position in stream, -1 if unknown
V
Vitor Sessak 已提交
137

138
    int perms;                  ///< permissions, see the AV_PERM_* flags
139

140 141
    enum AVMediaType type;      ///< media type of buffer data
    AVFilterBufferRefVideoProps *video; ///< video buffer specific properties
142
    AVFilterBufferRefAudioProps *audio; ///< audio buffer specific properties
143
} AVFilterBufferRef;
V
Vitor Sessak 已提交
144

145
/**
146
 * Copy properties of src to dst, without copying the actual data
147
 */
148
static inline void avfilter_copy_buffer_ref_props(AVFilterBufferRef *dst, AVFilterBufferRef *src)
149
{
150
    // copy common properties
151 152
    dst->pts             = src->pts;
    dst->pos             = src->pos;
153 154 155

    switch (src->type) {
    case AVMEDIA_TYPE_VIDEO: *dst->video = *src->video; break;
156
    case AVMEDIA_TYPE_AUDIO: *dst->audio = *src->audio; break;
157
    }
158 159
}

V
Vitor Sessak 已提交
160
/**
161
 * Add a new reference to a buffer.
162
 *
163
 * @param ref   an existing reference to the buffer
164
 * @param pmask a bitmask containing the allowable permissions in the new
165
 *              reference
166
 * @return      a new reference to the buffer with the same properties as the
167
 *              old, excluding any permissions denied by pmask
V
Vitor Sessak 已提交
168
 */
169
AVFilterBufferRef *avfilter_ref_buffer(AVFilterBufferRef *ref, int pmask);
V
Vitor Sessak 已提交
170 171

/**
172 173
 * Remove a reference to a buffer. If this is the last reference to the
 * buffer, the buffer itself is also automatically freed.
174
 *
175
 * @param ref reference to the buffer
V
Vitor Sessak 已提交
176
 */
177
void avfilter_unref_buffer(AVFilterBufferRef *ref);
V
Vitor Sessak 已提交
178

V
Vitor Sessak 已提交
179 180 181 182 183 184 185
/**
 * 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
S
Stefano Sabatini 已提交
186
 * formats, but will always output the same format as it is given in input.
V
Vitor Sessak 已提交
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 214 215 216 217 218
 *
 * 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.
 */
219
typedef struct AVFilterFormats {
V
Vitor Sessak 已提交
220
    unsigned format_count;      ///< number of formats
221
    int *formats;               ///< list of media formats
V
Vitor Sessak 已提交
222 223

    unsigned refcount;          ///< number of references to this list
224 225
    struct AVFilterFormats ***refs; ///< references to this list
}  AVFilterFormats;;
V
Vitor Sessak 已提交
226

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

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

V
Vitor Sessak 已提交
246
/**
247
 * Return a list of all formats supported by FFmpeg for the given media type.
V
Vitor Sessak 已提交
248
 */
249
AVFilterFormats *avfilter_all_formats(enum AVMediaType type);
V
Vitor Sessak 已提交
250 251

/**
252
 * Return a format list which contains the intersection of the formats of
253 254
 * a and b. Also, all the references of a, all the references of b, and
 * a and b themselves will be deallocated.
V
Vitor Sessak 已提交
255 256 257 258 259 260
 *
 * 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);

261
/**
262
 * Add *ref as a new reference to formats.
263 264
 * That is the pointers will point like in the ascii art below:
 *   ________
265 266 267 268
 *  |formats |<--------.
 *  |  ____  |     ____|___________________
 *  | |refs| |    |  __|_
 *  | |* * | |    | |  | |  AVFilterLink
269
 *  | |* *--------->|*ref|
270 271
 *  | |____| |    | |____|
 *  |________|    |________________________
272
 */
273
void avfilter_formats_ref(AVFilterFormats *formats, AVFilterFormats **ref);
V
Vitor Sessak 已提交
274 275

/**
276
 * If *ref is non-NULL, remove *ref as a reference to the format list
277 278
 * it currently points to, deallocates that list if this was the last
 * reference, and sets *ref to NULL.
279 280 281 282 283 284 285 286 287 288
 *
 *         Before                                 After
 *   ________                               ________         NULL
 *  |formats |<--------.                   |formats |         ^
 *  |  ____  |     ____|________________   |  ____  |     ____|________________
 *  | |refs| |    |  __|_                  | |refs| |    |  __|_
 *  | |* * | |    | |  | |  AVFilterLink   | |* * | |    | |  | |  AVFilterLink
 *  | |* *--------->|*ref|                 | |*   | |    | |*ref|
 *  | |____| |    | |____|                 | |____| |    | |____|
 *  |________|    |_____________________   |________|    |_____________________
V
Vitor Sessak 已提交
289 290 291
 */
void avfilter_formats_unref(AVFilterFormats **ref);

292 293 294 295
/**
 *
 *         Before                                 After
 *   ________                         ________
296
 *  |formats |<---------.            |formats |<---------.
297 298 299 300 301 302 303 304
 *  |  ____  |       ___|___         |  ____  |       ___|___
 *  | |refs| |      |   |   |        | |refs| |      |   |   |   NULL
 *  | |* *--------->|*oldref|        | |* *--------->|*newref|     ^
 *  | |* * | |      |_______|        | |* * | |      |_______|  ___|___
 *  | |____| |                       | |____| |                |   |   |
 *  |________|                       |________|                |*oldref|
 *                                                             |_______|
 */
M
Michael Niedermayer 已提交
305 306 307
void avfilter_formats_changeref(AVFilterFormats **oldref,
                                AVFilterFormats **newref);

308
/**
309
 * A filter pad used for either input or output.
310
 */
311
struct AVFilterPad {
V
Vitor Sessak 已提交
312
    /**
313 314
     * 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
315
     * pad has no need to ever be referenced by name.
V
Vitor Sessak 已提交
316
     */
V
Vitor Sessak 已提交
317
    const char *name;
V
Vitor Sessak 已提交
318 319

    /**
320
     * AVFilterPad type. Only video supported now, hopefully someone will
V
Vitor Sessak 已提交
321 322
     * add audio in the future.
     */
323
    enum AVMediaType type;
V
Vitor Sessak 已提交
324

325
    /**
326
     * Minimum required permissions on incoming buffers. Any buffer with
327 328 329 330 331 332 333 334
     * 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;

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

V
Vitor Sessak 已提交
345
    /**
346
     * Callback called before passing the first slice of a new frame. If
V
Vitor Sessak 已提交
347 348
     * NULL, the filter layer will default to storing a reference to the
     * picture inside the link structure.
349 350
     *
     * Input video pads only.
V
Vitor Sessak 已提交
351
     */
352
    void (*start_frame)(AVFilterLink *link, AVFilterBufferRef *picref);
V
Vitor Sessak 已提交
353 354

    /**
355
     * Callback function to get a video buffer. If NULL, the filter system will
356
     * use avfilter_default_get_video_buffer().
357 358
     *
     * Input video pads only.
V
Vitor Sessak 已提交
359
     */
360
    AVFilterBufferRef *(*get_video_buffer)(AVFilterLink *link, int perms, int w, int h);
V
Vitor Sessak 已提交
361

362 363 364 365 366 367 368 369 370 371
    /**
     * 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);

V
Vitor Sessak 已提交
372
    /**
373
     * Callback called after the slices of a frame are completely sent. If
V
Vitor Sessak 已提交
374 375
     * NULL, the filter layer will default to releasing the reference stored
     * in the link structure during start_frame().
376 377
     *
     * Input video pads only.
V
Vitor Sessak 已提交
378 379 380 381
     */
    void (*end_frame)(AVFilterLink *link);

    /**
382
     * Slice drawing callback. This is where a filter receives video data
383 384 385
     * and should do its processing.
     *
     * Input video pads only.
V
Vitor Sessak 已提交
386
     */
387
    void (*draw_slice)(AVFilterLink *link, int y, int height, int slice_dir);
V
Vitor Sessak 已提交
388

389 390 391 392 393 394 395 396
    /**
     * 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);

397
    /**
398
     * Frame poll callback. This returns the number of immediately available
399
     * samples. It should return a positive value if the next request_frame()
400 401 402 403 404 405 406 407
     * 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);

V
Vitor Sessak 已提交
408
    /**
409 410
     * 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
411 412 413
     * success, and another value on error.
     *
     * Output video pads only.
V
Vitor Sessak 已提交
414
     */
415
    int (*request_frame)(AVFilterLink *link);
V
Vitor Sessak 已提交
416 417

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

434
/** default handler for start_frame() for video inputs */
435
void avfilter_default_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
436

437
/** default handler for draw_slice() for video inputs */
438
void avfilter_default_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
439

440
/** default handler for end_frame() for video inputs */
V
Vitor Sessak 已提交
441
void avfilter_default_end_frame(AVFilterLink *link);
442

443 444 445 446
/** 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 */
447
int avfilter_default_config_output_link(AVFilterLink *link);
448

449
/** default handler for config_props() for audio/video inputs */
450
int avfilter_default_config_input_link (AVFilterLink *link);
451

452
/** default handler for get_video_buffer() for video inputs */
453
AVFilterBufferRef *avfilter_default_get_video_buffer(AVFilterLink *link,
454
                                                     int perms, int w, int h);
455 456 457 458 459 460

/** 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);

V
Vitor Sessak 已提交
461 462 463 464 465 466
/**
 * 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);
467

V
Vitor Sessak 已提交
468 469
/** Default handler for query_formats() */
int avfilter_default_query_formats(AVFilterContext *ctx);
V
Vitor Sessak 已提交
470

471
/** start_frame() handler for filters which simply pass video along */
472
void avfilter_null_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
473 474 475 476 477 478 479

/** 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);

480 481 482
/** filter_samples() handler for filters which simply pass audio along */
void avfilter_null_filter_samples(AVFilterLink *link, AVFilterBufferRef *samplesref);

483
/** get_video_buffer() handler for filters which simply pass video along */
484
AVFilterBufferRef *avfilter_null_get_video_buffer(AVFilterLink *link,
485 486
                                                  int perms, int w, int h);

487 488 489 490 491
/** 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);

492
/**
493
 * Filter definition. This defines the pads a filter contains, and all the
494 495
 * callback functions used to interact with the filter.
 */
496
typedef struct AVFilter {
V
Vitor Sessak 已提交
497
    const char *name;         ///< filter name
V
Vitor Sessak 已提交
498

499
    int priv_size;      ///< size of private data to allocate for the filter
V
Vitor Sessak 已提交
500

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

    /**
510
     * Filter uninitialization function. Should deallocate any memory held
511
     * by the filter, release any buffer references, etc. This does not need
512 513
     * to deallocate the AVFilterContext->priv memory itself.
     */
V
Vitor Sessak 已提交
514 515
    void (*uninit)(AVFilterContext *ctx);

V
Vitor Sessak 已提交
516
    /**
517
     * Queries formats supported by the filter and its pads, and sets the
V
Vitor Sessak 已提交
518 519 520
     * in_formats for links connected to its output pads, and out_formats
     * for links connected to its input pads.
     *
521 522
     * @return zero on success, a negative value corresponding to an
     * AVERROR code otherwise
V
Vitor Sessak 已提交
523 524 525
     */
    int (*query_formats)(AVFilterContext *);

526 527
    const AVFilterPad *inputs;  ///< NULL terminated list of inputs. NULL if none
    const AVFilterPad *outputs; ///< NULL terminated list of outputs. NULL if none
528 529 530 531 532 533

    /**
     * A description for the filter. You should use the
     * NULL_IF_CONFIG_SMALL() macro to define it.
     */
    const char *description;
V
Vitor Sessak 已提交
534 535
} AVFilter;

536
/** An instance of a filter */
537
struct AVFilterContext {
538
    const AVClass *av_class;              ///< needed for av_log()
V
Vitor Sessak 已提交
539

540
    AVFilter *filter;               ///< the AVFilter of which this is an instance
V
Vitor Sessak 已提交
541

542
    char *name;                     ///< name of this filter instance
543

544 545 546
    unsigned input_count;           ///< number of input pads
    AVFilterPad   *input_pads;      ///< array of input pads
    AVFilterLink **inputs;          ///< array of pointers to input links
V
Vitor Sessak 已提交
547

548 549 550
    unsigned output_count;          ///< number of output pads
    AVFilterPad   *output_pads;     ///< array of output pads
    AVFilterLink **outputs;         ///< array of pointers to output links
V
Vitor Sessak 已提交
551

552
    void *priv;                     ///< private data for use by the filter
V
Vitor Sessak 已提交
553 554
};

555
/**
556
 * A link between two filters. This contains pointers to the source and
557
 * destination filters between which this link exists, and the indexes of
558
 * the pads involved. In addition, this link also contains the parameters
559
 * which have been negotiated and agreed upon between the filter, such as
560
 * image dimensions, format, etc.
561
 */
562
struct AVFilterLink {
563
    AVFilterContext *src;       ///< source filter
564
    AVFilterPad *srcpad;        ///< output pad on the source filter
V
Vitor Sessak 已提交
565

566
    AVFilterContext *dst;       ///< dest filter
567
    AVFilterPad *dstpad;        ///< input pad on the dest filter
V
Vitor Sessak 已提交
568

569 570 571 572 573 574 575
    /** 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;

576 577
    enum AVMediaType type;      ///< filter media type

578
    /* These two parameters apply only to video */
579 580
    int w;                      ///< agreed upon image width
    int h;                      ///< agreed upon image height
581 582 583 584
    /* 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

585
    int format;                 ///< agreed upon media format
V
Vitor Sessak 已提交
586

V
Vitor Sessak 已提交
587 588 589 590 591 592 593 594
    /**
     * 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;

595
    /**
596
     * The buffer reference currently being sent across the link by the source
597
     * filter. This is used internally by the filter system to allow
598
     * automatic copying of buffers which do not have sufficient permissions
599
     * for the destination. This should not be accessed directly by the
600 601
     * filters.
     */
S
S.N. Hemanth Meenakshisundaram 已提交
602
    AVFilterBufferRef *src_buf;
603

S
S.N. Hemanth Meenakshisundaram 已提交
604 605
    AVFilterBufferRef *cur_buf;
    AVFilterBufferRef *out_buf;
606 607 608 609 610 611 612 613 614

    /**
     * Define the time base used by the PTS of the frames/samples
     * which will pass through this link.
     * During the configuration stage, each filter is supposed to
     * change only the output timebase, while the timebase of the
     * input link is assumed to be an unchangeable property.
     */
    AVRational time_base;
V
Vitor Sessak 已提交
615 616
};

617
/**
618
 * Link two filters together.
619
 *
620 621 622 623 624
 * @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
625
 */
V
Vitor Sessak 已提交
626 627 628
int avfilter_link(AVFilterContext *src, unsigned srcpad,
                  AVFilterContext *dst, unsigned dstpad);

629
/**
630
 * Negotiate the media format, dimensions, etc of all inputs to a filter.
631
 *
632 633
 * @param filter the filter to negotiate the properties for its inputs
 * @return       zero on successful negotiation
634
 */
635
int avfilter_config_links(AVFilterContext *filter);
636

637
/**
638
 * Request a picture buffer with a specific set of permissions.
639
 *
640
 * @param link  the output link to the filter from which the buffer will
641
 *              be requested
642
 * @param perms the required access permissions
643 644
 * @param w     the minimum width of the buffer to allocate
 * @param h     the minimum height of the buffer to allocate
645 646
 * @return      A reference to the buffer. This must be unreferenced with
 *              avfilter_unref_buffer when you are finished with it.
647
 */
648
AVFilterBufferRef *avfilter_get_video_buffer(AVFilterLink *link, int perms,
649
                                          int w, int h);
650

651 652 653 654 655 656 657 658 659 660 661
/**
 * 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
662
 *                       avfilter_unref_buffer when you are finished with it.
663 664 665 666 667
 */
AVFilterBufferRef *avfilter_get_audio_buffer(AVFilterLink *link, int perms,
                                             enum SampleFormat sample_fmt, int size,
                                             int64_t channel_layout, int planar);

668
/**
669
 * Request an input frame from the filter at the other end of the link.
670
 *
671 672
 * @param link the input link
 * @return     zero on success
673
 */
V
Vitor Sessak 已提交
674
int avfilter_request_frame(AVFilterLink *link);
675

676
/**
677
 * Poll a frame from the filter chain.
678
 *
679
 * @param  link the input link
680 681
 * @return the number of immediately available frames, a negative
 * number in case of error
682 683 684
 */
int avfilter_poll_frame(AVFilterLink *link);

685
/**
686
 * Notifie the next filter of the start of a frame.
687
 *
688
 * @param link   the output link the frame will be sent over
689
 * @param picref A reference to the frame about to be sent. The data for this
690
 *               frame need only be valid once draw_slice() is called for that
691
 *               portion. The receiving filter will free this reference when
692 693
 *               it no longer needs it.
 */
694
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref);
695 696

/**
697
 * Notifie the next filter that the current frame has finished.
698
 *
699
 * @param link the output link the frame was sent over
700
 */
V
Vitor Sessak 已提交
701
void avfilter_end_frame(AVFilterLink *link);
702 703

/**
704
 * Send a slice to the next filter.
705 706 707 708 709
 *
 * 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.
 *
710 711 712
 * @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
713 714 715 716
 * @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.
717
 */
718
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir);
V
Vitor Sessak 已提交
719

720 721 722 723 724 725 726 727 728 729
/**
 * 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);

730
/** Initialize the filter system. Register all builtin filters. */
V
Oops  
Vitor Sessak 已提交
731
void avfilter_register_all(void);
732

733
/** Uninitialize the filter system. Unregister all filters. */
V
Vitor Sessak 已提交
734
void avfilter_uninit(void);
735 736

/**
737
 * Register a filter. This is only needed if you plan to use
738 739 740
 * 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.
741
 *
742
 * @param filter the filter to register
743 744
 * @return 0 if the registration was succesfull, a negative value
 * otherwise
745
 */
746
int avfilter_register(AVFilter *filter);
747 748

/**
749
 * Get a filter definition matching the given name.
750
 *
751 752
 * @param name the filter name to find
 * @return     the filter definition, if any matching one is registered.
753 754
 *             NULL if none found.
 */
V
Vitor Sessak 已提交
755
AVFilter *avfilter_get_by_name(const char *name);
V
Vitor Sessak 已提交
756

S
Stefano Sabatini 已提交
757 758 759 760 761 762 763 764
/**
 * 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);

765
/**
766
 * Create a filter instance.
767 768 769
 *
 * @param filter_ctx put here a pointer to the created filter context
 * on success, NULL on failure
770
 * @param filter    the filter to create an instance of
771
 * @param inst_name Name to give to the new instance. Can be NULL for none.
772
 * @return >= 0 in case of success, a negative error code otherwise
773
 */
774
int avfilter_open(AVFilterContext **filter_ctx, AVFilter *filter, const char *inst_name);
775 776

/**
777
 * Initialize a filter.
778
 *
779
 * @param filter the filter to initialize
780 781
 * @param args   A string of parameters to use when initializing the filter.
 *               The format and meaning of this string varies by filter.
782
 * @param opaque Any extra non-string data needed by the filter. The meaning
783
 *               of this parameter varies by filter.
784
 * @return       zero on success
785
 */
786
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
787 788

/**
789
 * Destroy a filter.
790
 *
791
 * @param filter the filter to destroy
792
 */
V
Vitor Sessak 已提交
793 794
void avfilter_destroy(AVFilterContext *filter);

795
/**
796
 * Insert a filter in the middle of an existing link.
797
 *
798 799
 * @param link the link into which the filter should be inserted
 * @param filt the filter to be inserted
800 801
 * @param filt_srcpad_idx the input pad on the filter to connect
 * @param filt_dstpad_idx the output pad on the filter to connect
802
 * @return     zero on success
803
 */
V
Vitor Sessak 已提交
804
int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
805
                           unsigned filt_srcpad_idx, unsigned filt_dstpad_idx);
806

807
/**
808
 * Insert a new pad.
809
 *
810
 * @param idx Insertion point. Pad is inserted at the end if this point
811 812 813 814 815 816 817 818 819 820 821 822 823
 *            is beyond the end of the list of pads.
 * @param count Pointer to the number of pads in the list
 * @param padidx_off Offset within an AVFilterLink structure to the element
 *                   to increment when inserting a new pad causes link
 *                   numbering to change
 * @param pads Pointer to the pointer to the beginning of the list of pads
 * @param links Pointer to the pointer to the beginning of the list of links
 * @param newpad The new pad to add. A copy is made when adding.
 */
void avfilter_insert_pad(unsigned idx, unsigned *count, size_t padidx_off,
                         AVFilterPad **pads, AVFilterLink ***links,
                         AVFilterPad *newpad);

824
/** Insert a new input pad for the filter. */
825 826 827 828 829 830 831
static inline void avfilter_insert_inpad(AVFilterContext *f, unsigned index,
                                         AVFilterPad *p)
{
    avfilter_insert_pad(index, &f->input_count, offsetof(AVFilterLink, dstpad),
                        &f->input_pads, &f->inputs, p);
}

832
/** Insert a new output pad for the filter. */
833 834 835 836 837 838 839
static inline void avfilter_insert_outpad(AVFilterContext *f, unsigned index,
                                          AVFilterPad *p)
{
    avfilter_insert_pad(index, &f->output_count, offsetof(AVFilterLink, srcpad),
                        &f->output_pads, &f->outputs, p);
}

840
#endif  /* AVFILTER_AVFILTER_H */