filter_chain: introduce dedicated filter_chain_NewVideo() for video filters
[vlc.git] / include / vlc_filter.h
1 /*****************************************************************************
2  * vlc_filter.h: filter related structures and functions
3  *****************************************************************************
4  * Copyright (C) 1999-2014 VLC authors and VideoLAN
5  *
6  * Authors: Gildas Bazin <gbazin@videolan.org>
7  *          Antoine Cellerier <dionoea at videolan dot org>
8  *          RĂ©mi Denis-Courmont
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifndef VLC_FILTER_H
26 #define VLC_FILTER_H 1
27
28 #include <vlc_es.h>
29 #include <vlc_picture.h>
30 #include <vlc_subpicture.h>
31 #include <vlc_mouse.h>
32
33 /**
34  * \file
35  * This file defines the structure and types used by video and audio filters
36  */
37
38 typedef struct filter_owner_sys_t filter_owner_sys_t;
39
40 typedef struct filter_owner_t
41 {
42     void *sys;
43
44     union
45     {
46         struct
47         {
48             picture_t * (*buffer_new)( filter_t * );
49             void        (*buffer_del)( filter_t *, picture_t * );
50         } video;
51         struct
52         {
53             subpicture_t * (*buffer_new)( filter_t * );
54             void           (*buffer_del)( filter_t *, subpicture_t * );
55         } sub;
56     };
57 } filter_owner_t;
58
59
60 /** Structure describing a filter
61  * @warning BIG FAT WARNING : the code relies on the first 4 members of
62  * filter_t and decoder_t to be the same, so if you have anything to add,
63  * do it at the end of the structure.
64  */
65 struct filter_t
66 {
67     VLC_COMMON_MEMBERS
68
69     /* Module properties */
70     module_t *          p_module;
71     filter_sys_t *      p_sys;
72
73     /* Input format */
74     es_format_t         fmt_in;
75
76     /* Output format of filter */
77     es_format_t         fmt_out;
78     bool                b_allow_fmt_out_change;
79
80     /* Filter configuration */
81     config_chain_t *    p_cfg;
82
83     union
84     {
85         struct
86         {
87             picture_t * (*pf_filter) ( filter_t *, picture_t * );
88             void        (*pf_flush)( filter_t * );
89             /* Filter mouse state.
90              *
91              * If non-NULL, you must convert from output to input formats:
92              * - If VLC_SUCCESS is returned, the mouse state is propagated.
93              * - Otherwise, the mouse change is not propagated.
94              * If NULL, the mouse state is considered unchanged and will be
95              * propagated.
96              */
97             int         (*pf_mouse)( filter_t *, vlc_mouse_t *,
98                                      const vlc_mouse_t *p_old,
99                                      const vlc_mouse_t *p_new );
100         } video;
101 #define pf_video_filter     u.video.pf_filter
102 #define pf_video_flush      u.video.pf_flush
103 #define pf_video_mouse      u.video.pf_mouse
104
105         struct
106         {
107             block_t *   (*pf_filter) ( filter_t *, block_t * );
108         } audio;
109 #define pf_audio_filter     u.audio.pf_filter
110
111         struct
112         {
113             void        (*pf_blend) ( filter_t *,  picture_t *,
114                                       const picture_t *, int, int, int );
115         } blend;
116 #define pf_video_blend     u.blend.pf_blend
117
118         struct
119         {
120             subpicture_t * (*pf_source)    ( filter_t *, mtime_t );
121             int            (*pf_mouse)     ( filter_t *,
122                                              const vlc_mouse_t *p_old,
123                                              const vlc_mouse_t *p_new,
124                                              const video_format_t * );
125         } sub;
126 #define pf_sub_source      u.sub.pf_source
127 #define pf_sub_mouse       u.sub.pf_mouse
128
129         struct
130         {
131             subpicture_t * (*pf_filter) ( filter_t *, subpicture_t * );
132         } subf;
133 #define pf_sub_filter      u.subf.pf_filter
134
135         struct
136         {
137             int         (*pf_text) ( filter_t *, subpicture_region_t *,
138                                      subpicture_region_t *,
139                                      const vlc_fourcc_t * );
140             int         (*pf_html) ( filter_t *, subpicture_region_t *,
141                                      subpicture_region_t *,
142                                      const vlc_fourcc_t * );
143         } render;
144 #define pf_render_text     u.render.pf_text
145 #define pf_render_html     u.render.pf_html
146
147     } u;
148
149     /* Input attachments
150      * XXX use filter_GetInputAttachments */
151     int (*pf_get_attachments)( filter_t *, input_attachment_t ***, int * );
152
153     /* Private structure for the owner of the decoder */
154     filter_owner_t      owner;
155 };
156
157 /**
158  * This function will return a new picture usable by p_filter as an output
159  * buffer. You have to release it using filter_DeletePicture or by returning
160  * it to the caller as a pf_video_filter return value.
161  * Provided for convenience.
162  *
163  * \param p_filter filter_t object
164  * \return new picture on success or NULL on failure
165  */
166 static inline picture_t *filter_NewPicture( filter_t *p_filter )
167 {
168     picture_t *pic = p_filter->owner.video.buffer_new( p_filter );
169     if( pic == NULL )
170         msg_Warn( p_filter, "can't get output picture" );
171     return pic;
172 }
173
174 /**
175  * This function will release a picture create by filter_NewPicture.
176  * Provided for convenience.
177  *
178  * \param p_filter filter_t object
179  * \param p_picture picture to be deleted
180  */
181 static inline void filter_DeletePicture( filter_t *p_filter, picture_t *pic )
182 {
183     p_filter->owner.video.buffer_del( p_filter, pic );
184 }
185
186 /**
187  * This function will flush the state of a video filter.
188  */
189 static inline void filter_FlushPictures( filter_t *p_filter )
190 {
191     if( p_filter->pf_video_flush )
192         p_filter->pf_video_flush( p_filter );
193 }
194
195 /**
196  * This function will return a new subpicture usable by p_filter as an output
197  * buffer. You have to release it using filter_DeleteSubpicture or by returning
198  * it to the caller as a pf_sub_source return value.
199  * Provided for convenience.
200  *
201  * \param p_filter filter_t object
202  * \return new subpicture
203  */
204 static inline subpicture_t *filter_NewSubpicture( filter_t *p_filter )
205 {
206     subpicture_t *subpic = p_filter->owner.sub.buffer_new( p_filter );
207     if( subpic == NULL )
208         msg_Warn( p_filter, "can't get output subpicture" );
209     return subpic;
210 }
211
212 /**
213  * This function will release a subpicture create by filter_NewSubicture.
214  * Provided for convenience.
215  *
216  * \param p_filter filter_t object
217  * \param p_subpicture to be released
218  */
219 static inline void filter_DeleteSubpicture( filter_t *p_filter,
220                                             subpicture_t *subpic )
221 {
222     p_filter->owner.sub.buffer_del( p_filter, subpic );
223 }
224
225 /**
226  * This function gives all input attachments at once.
227  *
228  * You MUST release the returned values
229  */
230 static inline int filter_GetInputAttachments( filter_t *p_filter,
231                                               input_attachment_t ***ppp_attachment,
232                                               int *pi_attachment )
233 {
234     if( !p_filter->pf_get_attachments )
235         return VLC_EGENERIC;
236     return p_filter->pf_get_attachments( p_filter,
237                                          ppp_attachment, pi_attachment );
238 }
239
240 /**
241  * It creates a blend filter.
242  *
243  * Only the chroma properties of the dest format is used (chroma
244  * type, rgb masks and shifts)
245  */
246 VLC_API filter_t * filter_NewBlend( vlc_object_t *, const video_format_t *p_dst_chroma ) VLC_USED;
247
248 /**
249  * It configures blend filter parameters that are allowed to changed
250  * after the creation.
251  */
252 VLC_API int filter_ConfigureBlend( filter_t *, int i_dst_width, int i_dst_height, const video_format_t *p_src );
253
254 /**
255  * It blends a picture into another one.
256  *
257  * The input picture is not modified and not released.
258  */
259 VLC_API int filter_Blend( filter_t *, picture_t *p_dst, int i_dst_x, int i_dst_y, const picture_t *p_src, int i_alpha );
260
261 /**
262  * It destroys a blend filter created by filter_NewBlend.
263  */
264 VLC_API void filter_DeleteBlend( filter_t * );
265
266 /**
267  * Create a picture_t *(*)( filter_t *, picture_t * ) compatible wrapper
268  * using a void (*)( filter_t *, picture_t *, picture_t * ) function
269  *
270  * Currently used by the chroma video filters
271  */
272 #define VIDEO_FILTER_WRAPPER( name )                                    \
273     static picture_t *name ## _Filter ( filter_t *p_filter,             \
274                                         picture_t *p_pic )              \
275     {                                                                   \
276         picture_t *p_outpic = filter_NewPicture( p_filter );            \
277         if( p_outpic )                                                  \
278         {                                                               \
279             name( p_filter, p_pic, p_outpic );                          \
280             picture_CopyProperties( p_outpic, p_pic );                  \
281         }                                                               \
282         picture_Release( p_pic );                                       \
283         return p_outpic;                                                \
284     }
285
286 /**
287  * Filter chain management API
288  * The filter chain management API is used to dynamically construct filters
289  * and add them in a chain.
290  */
291
292 typedef struct filter_chain_t filter_chain_t;
293
294 /**
295  * Create new filter chain
296  *
297  * \param p_object pointer to a vlc object
298  * \param psz_capability vlc capability of filters in filter chain
299  * \param b_allow_format_fmt_change allow changing of fmt
300  * \return pointer to a filter chain
301  */
302 VLC_API filter_chain_t * filter_chain_New( vlc_object_t *, const char *, bool )
303 VLC_USED;
304 #define filter_chain_New( a, b, c ) filter_chain_New( VLC_OBJECT( a ), b, c )
305
306 /**
307  * Creates a new video filter chain.
308  *
309  * \param obj pointer to parent VLC object
310  * \param change whether to allow changing the output format
311  * \param owner owner video buffer callbacks
312  * \return new filter chain, or NULL on error
313  */
314 VLC_API filter_chain_t * filter_chain_NewVideo( vlc_object_t *obj, bool change,
315                                                 const filter_owner_t *owner )
316 VLC_USED;
317 #define filter_chain_NewVideo( a, b, c ) \
318         filter_chain_NewVideo( VLC_OBJECT( a ), b, c )
319
320 /**
321  * Delete filter chain will delete all filters in the chain and free all
322  * allocated data. The pointer to the filter chain is then no longer valid.
323  *
324  * \param p_chain pointer to filter chain
325  */
326 VLC_API void filter_chain_Delete( filter_chain_t * );
327
328 /**
329  * Reset filter chain will delete all filters in the chain and
330  * reset p_fmt_in and p_fmt_out to the new values.
331  *
332  * \param p_chain pointer to filter chain
333  * \param p_fmt_in new fmt_in params
334  * \param p_fmt_out new fmt_out params
335  */
336 VLC_API void filter_chain_Reset( filter_chain_t *, const es_format_t *, const es_format_t * );
337
338 /**
339  * Append filter to the end of the chain.
340  *
341  * \param p_chain pointer to filter chain
342  * \param psz_name name of filter
343  * \param p_cfg
344  * \param p_fmt_in input es_format_t
345  * \param p_fmt_out output es_format_t
346  * \return pointer to filter chain
347  */
348 VLC_API filter_t * filter_chain_AppendFilter( filter_chain_t *, const char *, config_chain_t *, const es_format_t *, const es_format_t * );
349
350 /**
351  * Append new filter to filter chain from string.
352  *
353  * \param p_chain pointer to filter chain
354  * \param psz_string string of filters
355  * \return 0 for success
356  */
357 VLC_API int filter_chain_AppendFromString( filter_chain_t *, const char * );
358
359 /**
360  * Delete filter from filter chain. This function also releases the filter
361  * object and unloads the filter modules. The pointer to p_filter is no
362  * longer valid after this function successfully returns.
363  *
364  * \param p_chain pointer to filter chain
365  * \param p_filter pointer to filter object
366  * \return VLC_SUCCESS on succes, else VLC_EGENERIC
367  */
368 VLC_API int filter_chain_DeleteFilter( filter_chain_t *, filter_t * );
369
370 /**
371  * Get the number of filters in the filter chain.
372  *
373  * \param p_chain pointer to filter chain
374  * \return number of filters in this filter chain
375  */
376 VLC_API int filter_chain_GetLength( filter_chain_t * );
377
378 /**
379  * Get last p_fmt_out in the chain.
380  *
381  * \param p_chain pointer to filter chain
382  * \return last p_fmt (es_format_t) of this filter chain
383  */
384 VLC_API const es_format_t * filter_chain_GetFmtOut( filter_chain_t * );
385
386 /**
387  * Apply the filter chain to a video picture.
388  *
389  * \param p_chain pointer to filter chain
390  * \param p_picture picture to apply filters on
391  * \return modified picture after applying all video filters
392  */
393 VLC_API picture_t * filter_chain_VideoFilter( filter_chain_t *, picture_t * );
394
395 /**
396  * Flush a video filter chain.
397  */
398 VLC_API void filter_chain_VideoFlush( filter_chain_t * );
399
400 /**
401  * Apply the filter chain to a audio block.
402  *
403  * \param p_chain pointer to filter chain
404  * \param p_block audio frame to apply filters on
405  * \return modified audio frame after applying all audio filters
406  */
407 VLC_API block_t * filter_chain_AudioFilter( filter_chain_t *, block_t * );
408
409 /**
410  * Apply filter chain to subpictures.
411  *
412  * \param p_chain pointer to filter chain
413  * \param display_date of subpictures
414  */
415 void filter_chain_SubSource( filter_chain_t *, spu_t *, mtime_t );
416
417 /**
418  * Apply filter chain to subpictures.
419  *
420  * \param p_chain pointer to filter chain
421  * \param p_subpicture subpicture to apply filters on
422  * \return modified subpicture after applying all subpicture filters
423  */
424 VLC_API subpicture_t * filter_chain_SubFilter( filter_chain_t *, subpicture_t * );
425
426 /**
427  * Apply the filter chain to a mouse state.
428  *
429  * It will be applied from the output to the input. It makes sense only
430  * for a video filter chain.
431  *
432  * The vlc_mouse_t* pointers may be the same.
433  */
434 VLC_API int filter_chain_MouseFilter( filter_chain_t *, vlc_mouse_t *, const vlc_mouse_t * );
435
436 /**
437  * Inform the filter chain of mouse state.
438  *
439  * It makes sense only for a sub source chain.
440  */
441 VLC_API int filter_chain_MouseEvent( filter_chain_t *, const vlc_mouse_t *, const video_format_t * );
442
443 int filter_chain_ForEach( filter_chain_t *chain,
444                           int (*cb)( filter_t *, void * ), void *opaque );
445
446 #endif /* _VLC_FILTER_H */
447