summaryrefslogtreecommitdiff
path: root/libavfilter/avfilter.h
blob: 013e63deceefe0fe7b846f034457d4081a00de8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Filter layer
 * 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
 */

#ifndef FFMPEG_AVFILTER_H
#define FFMPEG_AVFILTER_H

#include "avcodec.h"

typedef struct AVFilterContext AVFilterContext;
typedef struct AVFilterLink    AVFilterLink;
typedef struct AVFilterPad     AVFilterPad;

/* TODO: look for other flags which may be useful in this structure (interlace
 * flags, etc)
 */
/**
 * A reference-counted picture data type used by the filter system.  Filters
 * should not store pointers to this structure directly, but instead use the
 * AVFilterPicRef structure below
 */
typedef struct AVFilterPic
{
    uint8_t *data[4];
    int linesize[4];    ///< number of bytes per line
    enum PixelFormat format;

    unsigned refcount;
    void *priv;
    void (*free)(struct AVFilterPic *pic);
} AVFilterPic;

/**
 * A reference to an AVFilterPic.  Since filters can manipulate the origin of
 * a picture to, for example, crop image without any memcpy, the picture origin
 * and dimensions are per-reference properties.  Linesize is also useful for
 * image flipping, frame to field filters, etc, and so is also per-reference.
 *
 * TODO: add anything necessary for frame reordering
 */
typedef struct AVFilterPicRef
{
    AVFilterPic *pic;
    uint8_t *data[4];
    int linesize[4];
    int w, h;

    int64_t pts;                ///< presentation timestamp in milliseconds

    int perms;                  ///< permissions
#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
} AVFilterPicRef;

/**
 * Add a new reference to a picture.
 * @param ref An existing reference to the picture
 * @param pmask A bitmask containing the allowable permissions in the new reference
 * @return A new reference to the picture with the same properties as the old
 */
AVFilterPicRef *avfilter_ref_pic(AVFilterPicRef *ref, int pmask);

/**
 * Remove a reference to a picture.  If this is the last reference to the
 * picture, the picture itself is also automatically freed.
 * @param ref Reference to the picture.
 */
void avfilter_unref_pic(AVFilterPicRef *ref);

struct AVFilterPad
{
    /**
     * Pad name.  The name is unique among inputs and among oututs, but an
     * input may have the same name as an output.
     */
    char *name;

    /**
     * AVFilterPad type.  Only video supported now, hopefully someone will
     * add audio in the future.
     */
    int type;
#define AV_PAD_VIDEO 0

    /**
     * Callback to get a list of supported formats.  The returned list should
     * be terminated by -1.  This is used for both input and output pads and
     * is required for both.
     */
    int *(*query_formats)(AVFilterLink *link);

    /**
     * Callback called before passing the first slice of a new frame.  If
     * NULL, the filter layer will default to storing a reference to the
     * picture inside the link structure.
     */
    void (*start_frame)(AVFilterLink *link, AVFilterPicRef *picref);

    /**
     * Callback function to get a buffer.  If NULL, the filter system will
     * handle buffer requests.  Only required for input video pads.
     */
    AVFilterPicRef *(*get_video_buffer)(AVFilterLink *link, int perms);

    /**
     * Callback called after the slices of a frame are completely sent.  If
     * NULL, the filter layer will default to releasing the reference stored
     * in the link structure during start_frame().
     */
    void (*end_frame)(AVFilterLink *link);

    /**
     * Slice drawing callback.  This is where a filter receives video data
     * and should do its processing.  Only required for input video pads.
     */
    void (*draw_slice)(AVFilterLink *link, uint8_t *data[4], int y, int height);

    /**
     * Frame request callback.  A call to this should result in at least one
     * frame being output over the given link.  Video output pads only.
     */
    void (*request_frame)(AVFilterLink *link);

    /**
     * Link configuration callback.  For output pads, this should set the link
     * properties such as width/height.  NOTE: this should not set the format
     * property - that is negotiated between filters by the filter system using
     * the query_formats() callback.
     *
     * For input pads, this should check the properties of the link, and update
     * the filter's internal state as necessary.
     */
    int (*config_props)(AVFilterLink *link);
};

/* the default implementations of filter entry points */
void avfilter_default_start_frame(AVFilterLink *link, AVFilterPicRef *picref);
void avfilter_default_end_frame(AVFilterLink *link);
int avfilter_default_config_output_link(AVFilterLink *link);
int *avfilter_default_query_output_formats(AVFilterLink *link);
AVFilterPicRef *avfilter_default_get_video_buffer(AVFilterLink *link,
                                                  int perms);

typedef struct
{
    char *name;
    char *author;

    int priv_size;

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

    const AVFilterPad *inputs;  /// NULL terminated list of inputs. NULL if none
    const AVFilterPad *outputs; /// NULL terminated list of outputs. NULL if none
} AVFilter;

struct AVFilterContext
{
    AVClass *av_class;

    AVFilter *filter;

    char *name;

    unsigned input_count;
    AVFilterPad   *input_pads;
    AVFilterLink **inputs;

    unsigned output_count;
    AVFilterPad   *output_pads;
    AVFilterLink **outputs;

    void *priv;
};

struct AVFilterLink
{
    AVFilterContext *src;
    unsigned int srcpad;

    AVFilterContext *dst;
    unsigned int dstpad;

    int w, h;
    enum PixelFormat format;

    AVFilterPicRef *cur_pic;
    AVFilterPicRef *outpic;
};

/** Link two filters together */
int avfilter_link(AVFilterContext *src, unsigned srcpad,
                  AVFilterContext *dst, unsigned dstpad);

AVFilterPicRef *avfilter_get_video_buffer(AVFilterLink *link, int perms);
void avfilter_request_frame(AVFilterLink *link);
void avfilter_start_frame(AVFilterLink *link, AVFilterPicRef *picref);
void avfilter_end_frame(AVFilterLink *link);
void avfilter_draw_slice(AVFilterLink *link, uint8_t *data[4], int y, int h);

void avfilter_init(void);
void avfilter_uninit(void);
void avfilter_register(AVFilter *filter);
AVFilter *avfilter_get_by_name(char *name);

AVFilterContext *avfilter_create(AVFilter *filter, char *inst_name);
AVFilterContext *avfilter_create_by_name(char *name, char *inst_name);
int avfilter_init_filter(AVFilterContext *filter, const char *args, void *opaque);
void avfilter_destroy(AVFilterContext *filter);

int *avfilter_make_format_list(int len, ...);

#endif  /* FFMPEG_AVFILTER_H */