audiotrack: avoid cast, use proper type
[vlc.git] / include / vlc_filter.h
blob9d5e3541a4427ca1d761be367d17eb808be00a25
1 /*****************************************************************************
2 * vlc_filter.h: filter related structures and functions
3 *****************************************************************************
4 * Copyright (C) 1999-2014 VLC authors and VideoLAN
6 * Authors: Gildas Bazin <gbazin@videolan.org>
7 * Antoine Cellerier <dionoea at videolan dot org>
8 * RĂ©mi Denis-Courmont
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.
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.
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 *****************************************************************************/
25 #ifndef VLC_FILTER_H
26 #define VLC_FILTER_H 1
28 #include <vlc_es.h>
29 #include <vlc_picture.h>
30 #include <vlc_codec.h>
32 typedef struct vlc_video_context vlc_video_context;
33 struct vlc_audio_loudness;
35 /**
36 * \defgroup filter Filters
37 * \ingroup output
38 * Audio, video, text filters
39 * @{
40 * \file
41 * Filter modules interface
44 struct filter_video_callbacks
46 picture_t *(*buffer_new)(filter_t *);
47 vlc_decoder_device * (*hold_device)(vlc_object_t *, void *sys);
50 struct filter_audio_callbacks
52 struct
54 void (*on_changed)(filter_t *,
55 const struct vlc_audio_loudness *loudness);
56 } meter_loudness;
59 struct filter_subpicture_callbacks
61 subpicture_t *(*buffer_new)(filter_t *);
64 typedef struct filter_owner_t
66 union
68 const struct filter_video_callbacks *video;
69 const struct filter_audio_callbacks *audio;
70 const struct filter_subpicture_callbacks *sub;
73 /* Input attachments
74 * XXX use filter_GetInputAttachments */
75 int (*pf_get_attachments)( filter_t *, input_attachment_t ***, int * );
77 void *sys;
78 } filter_owner_t;
80 struct vlc_mouse_t;
82 struct vlc_filter_operations
84 /* Operation depending on the type of filter. */
85 union
87 /** Filter a picture (video filter) */
88 picture_t * (*filter_video)(filter_t *, picture_t *);
90 /** Filter an audio block (audio filter) */
91 block_t * (*filter_audio)(filter_t *, block_t *);
93 /** Blend a subpicture onto a picture (video blending) */
94 void (*blend_video)(filter_t *, picture_t *, const picture_t *,
95 int, int, int);
97 /** Generate a subpicture (sub source) */
98 subpicture_t *(*source_sub)(filter_t *, vlc_tick_t);
100 /** Filter a subpicture (sub filter) */
101 subpicture_t *(*filter_sub)(filter_t *, subpicture_t *);
103 /** Render text (text renderer) */
104 int (*render)(filter_t *, subpicture_region_t *,
105 subpicture_region_t *, const vlc_fourcc_t *);
108 union
110 /* TODO: video filter drain */
111 /** Drain (audio filter) */
112 block_t *(*drain_audio)(filter_t *);
115 /** Flush
117 * Flush (i.e. discard) any internal buffer in a video or audio filter.
119 void (*flush)(filter_t *);
121 /** Change viewpoint
123 * Pass a new viewpoint to audio filters. Filters like the spatialaudio one
124 * used for Ambisonics rendering will change its output according to this
125 * viewpoint.
127 void (*change_viewpoint)(filter_t *, const vlc_viewpoint_t *);
129 /** Filter mouse state (video filter).
131 * If non-NULL, you must convert from output to input formats:
132 * - If VLC_SUCCESS is returned, the mouse state is propagated.
133 * - Otherwise, the mouse change is not propagated.
134 * If NULL, the mouse state is considered unchanged and will be
135 * propagated. */
136 int (*video_mouse)(filter_t *, struct vlc_mouse_t *,
137 const struct vlc_mouse_t *p_old);
139 /** Close the filter and release its resources. */
140 void (*close)(filter_t *);
143 typedef int (*vlc_open_deinterlace)(filter_t *);
144 typedef int (*vlc_video_converter_open)(filter_t *);
145 typedef int (*vlc_video_filter_open)(filter_t *);
146 typedef int (*vlc_video_text_renderer_open)(filter_t *);
147 typedef int (*vlc_video_sub_filter_open)(filter_t *);
148 typedef int (*vlc_video_sub_source_open)(filter_t *);
149 typedef int (*vlc_video_blending_open)(filter_t *);
152 #define set_deinterlace_callback( activate ) \
154 vlc_open_deinterlace open__ = activate; \
155 (void) open__; \
156 set_callback(activate) \
158 set_capability( "video filter", 0 ) \
159 add_shortcut( "deinterlace" )
161 #define set_callback_video_filter( activate ) \
163 vlc_video_filter_open open__ = activate; \
164 (void) open__; \
165 set_callback(activate) \
167 set_capability( "video filter", 0 )
169 #define set_callback_video_converter( activate, priority ) \
171 vlc_video_converter_open open__ = activate; \
172 (void) open__; \
173 set_callback(activate) \
175 set_capability( "video converter", priority )
177 #define set_callback_text_renderer( activate, priority ) \
179 vlc_video_text_renderer_open open__ = activate; \
180 (void) open__; \
181 set_callback(activate) \
183 set_capability( "text renderer", priority )
185 #define set_callback_sub_filter( activate ) \
187 vlc_video_sub_filter_open open__ = activate; \
188 (void) open__; \
189 set_callback(activate) \
191 set_capability( "sub filter", 0 )
193 #define set_callback_sub_source( activate, priority ) \
195 vlc_video_sub_source_open open__ = activate; \
196 (void) open__; \
197 set_callback(activate) \
199 set_capability( "sub source", priority )
201 #define set_callback_video_blending( activate, priority ) \
203 vlc_video_blending_open open__ = activate; \
204 (void) open__; \
205 set_callback(activate) \
207 set_capability( "video blending", priority )
209 /** Structure describing a filter
210 * @warning BIG FAT WARNING : the code relies on the first 4 members of
211 * filter_t and decoder_t to be the same, so if you have anything to add,
212 * do it at the end of the structure.
214 struct filter_t
216 struct vlc_object_t obj;
218 /* Module properties */
219 module_t * p_module;
220 void *p_sys;
222 /* Input format */
223 es_format_t fmt_in;
224 vlc_video_context *vctx_in; // video filter, set by owner
226 /* Output format of filter */
227 es_format_t fmt_out;
228 vlc_video_context *vctx_out; // video filter, handled by the filter
229 bool b_allow_fmt_out_change;
231 /* Name of the "video filter" shortcut that is requested, can be NULL */
232 const char * psz_name;
233 /* Filter configuration */
234 config_chain_t * p_cfg;
236 /* Implementation of filter API */
237 const struct vlc_filter_operations *ops;
239 /** Private structure for the owner of the filter */
240 filter_owner_t owner;
243 static inline void filter_Close( filter_t *p_filter )
245 if ( p_filter->ops->close )
246 p_filter->ops->close( p_filter );
250 * This function will return a new picture usable by p_filter as an output
251 * buffer. You have to release it using picture_Release or by returning
252 * it to the caller as a ops->filter_video return value.
253 * Provided for convenience.
255 * \param p_filter filter_t object
256 * \return new picture on success or NULL on failure
258 static inline picture_t *filter_NewPicture( filter_t *p_filter )
260 picture_t *pic = NULL;
261 if ( p_filter->owner.video != NULL && p_filter->owner.video->buffer_new != NULL)
262 pic = p_filter->owner.video->buffer_new( p_filter );
263 if ( pic == NULL )
265 // legacy filter owners not setting a default filter_allocator
266 pic = picture_NewFromFormat( &p_filter->fmt_out.video );
268 if( pic == NULL )
269 msg_Warn( p_filter, "can't get output picture" );
270 return pic;
274 * Flush a filter
276 * This function will flush the state of a filter (audio or video).
278 static inline void filter_Flush( filter_t *p_filter )
280 if( p_filter->ops->flush != NULL )
281 p_filter->ops->flush( p_filter );
284 static inline void filter_ChangeViewpoint( filter_t *p_filter,
285 const vlc_viewpoint_t *vp)
287 if( p_filter->ops->change_viewpoint != NULL )
288 p_filter->ops->change_viewpoint( p_filter, vp );
291 static inline vlc_decoder_device * filter_HoldDecoderDevice( filter_t *p_filter )
293 if ( !p_filter->owner.video || !p_filter->owner.video->hold_device )
294 return NULL;
296 return p_filter->owner.video->hold_device( VLC_OBJECT(p_filter), p_filter->owner.sys );
299 static inline vlc_decoder_device * filter_HoldDecoderDeviceType( filter_t *p_filter,
300 enum vlc_decoder_device_type type )
302 if ( !p_filter->owner.video || !p_filter->owner.video->hold_device )
303 return NULL;
305 vlc_decoder_device *dec_dev = p_filter->owner.video->hold_device( VLC_OBJECT(p_filter),
306 p_filter->owner.sys );
307 if ( dec_dev != NULL )
309 if ( dec_dev->type == type )
310 return dec_dev;
311 vlc_decoder_device_Release(dec_dev);
313 return NULL;
317 * This function will drain, then flush an audio filter.
319 static inline block_t *filter_DrainAudio( filter_t *p_filter )
321 if( p_filter->ops->drain_audio )
322 return p_filter->ops->drain_audio( p_filter );
323 else
324 return NULL;
327 static inline void filter_SendAudioLoudness(filter_t *filter,
328 const struct vlc_audio_loudness *loudness)
330 assert(filter->owner.audio->meter_loudness.on_changed);
331 filter->owner.audio->meter_loudness.on_changed(filter, loudness);
335 * This function will return a new subpicture usable by p_filter as an output
336 * buffer. You have to release it using subpicture_Delete or by returning it to
337 * the caller as a ops->sub_source return value.
338 * Provided for convenience.
340 * \param p_filter filter_t object
341 * \return new subpicture
343 static inline subpicture_t *filter_NewSubpicture( filter_t *p_filter )
345 subpicture_t *subpic = p_filter->owner.sub->buffer_new( p_filter );
346 if( subpic == NULL )
347 msg_Warn( p_filter, "can't get output subpicture" );
348 return subpic;
352 * This function gives all input attachments at once.
354 * You MUST release the returned values
356 static inline int filter_GetInputAttachments( filter_t *p_filter,
357 input_attachment_t ***ppp_attachment,
358 int *pi_attachment )
360 if( !p_filter->owner.pf_get_attachments )
361 return VLC_EGENERIC;
362 return p_filter->owner.pf_get_attachments( p_filter,
363 ppp_attachment, pi_attachment );
367 * This function duplicates every variables from the filter, and adds a proxy
368 * callback to trigger filter events from obj.
370 * \param restart_cb a vlc_callback_t to call if the event means restarting the
371 * filter (i.e. an event on a non-command variable)
373 VLC_API void filter_AddProxyCallbacks( vlc_object_t *obj, filter_t *filter,
374 vlc_callback_t restart_cb );
375 # define filter_AddProxyCallbacks(a, b, c) \
376 filter_AddProxyCallbacks(VLC_OBJECT(a), b, c)
379 * This function removes the callbacks previously added to every duplicated
380 * variables, and removes them afterward.
382 * \param restart_cb the same vlc_callback_t passed to filter_AddProxyCallbacks
384 VLC_API void filter_DelProxyCallbacks( vlc_object_t *obj, filter_t *filter,
385 vlc_callback_t restart_cb);
386 # define filter_DelProxyCallbacks(a, b, c) \
387 filter_DelProxyCallbacks(VLC_OBJECT(a), b, c)
389 typedef filter_t vlc_blender_t;
392 * It creates a blend filter.
394 * Only the chroma properties of the dest format is used (chroma
395 * type, rgb masks and shifts)
397 VLC_API vlc_blender_t * filter_NewBlend( vlc_object_t *, const video_format_t *p_dst_chroma ) VLC_USED;
400 * It configures blend filter parameters that are allowed to changed
401 * after the creation.
403 VLC_API int filter_ConfigureBlend( vlc_blender_t *, int i_dst_width, int i_dst_height, const video_format_t *p_src );
406 * It blends a picture into another one.
408 * The input picture is not modified and not released.
410 VLC_API int filter_Blend( vlc_blender_t *, picture_t *p_dst, int i_dst_x, int i_dst_y, const picture_t *p_src, int i_alpha );
413 * It destroys a blend filter created by filter_NewBlend.
415 VLC_API void filter_DeleteBlend( vlc_blender_t * );
418 * Create a picture_t *(*)( filter_t *, picture_t * ) compatible wrapper
419 * using a void (*)( filter_t *, picture_t *, picture_t * ) function
421 * Currently used by the chroma video filters
423 #define VIDEO_FILTER_WRAPPER_CLOSE_FILT( name, close_cb ) \
424 static picture_t *name ## _Filter ( filter_t *p_filter, \
425 picture_t *p_pic ) \
427 picture_t *p_outpic = filter_NewPicture( p_filter ); \
428 if( p_outpic ) \
430 name( p_filter, p_pic, p_outpic ); \
431 picture_CopyProperties( p_outpic, p_pic ); \
433 picture_Release( p_pic ); \
434 return p_outpic; \
436 static const struct vlc_filter_operations name ## _ops = { \
437 .filter_video = name ## _Filter, .close = close_cb, \
440 #define VIDEO_FILTER_WRAPPER_CLOSE( name, close_cb ) \
441 static void name (filter_t *, picture_t *, picture_t *); \
442 static void close_cb (filter_t *); \
443 VIDEO_FILTER_WRAPPER_CLOSE_FILT( name, close_cb )
445 #define VIDEO_FILTER_WRAPPER( name ) \
446 static void name (filter_t *, picture_t *, picture_t *); \
447 VIDEO_FILTER_WRAPPER_CLOSE_FILT( name, NULL )
450 * Wrappers to use when the filter function is not a static function
452 #define VIDEO_FILTER_WRAPPER_EXT( name ) \
453 void name (filter_t *, picture_t *, picture_t *); \
454 VIDEO_FILTER_WRAPPER_CLOSE_FILT( name, NULL )
456 #define VIDEO_FILTER_WRAPPER_CLOSE_EXT( name, close_cb ) \
457 void name (filter_t *, picture_t *, picture_t *); \
458 static void close_cb (filter_t *); \
459 VIDEO_FILTER_WRAPPER_CLOSE_FILT( name, close_cb )
462 * Filter chain management API
463 * The filter chain management API is used to dynamically construct filters
464 * and add them in a chain.
467 typedef struct filter_chain_t filter_chain_t;
470 * Create new filter chain
472 * \param obj pointer to a vlc object
473 * \param psz_capability vlc capability of filters in filter chain
474 * \return pointer to a filter chain
476 filter_chain_t * filter_chain_NewSPU( vlc_object_t *obj, const char *psz_capability )
477 VLC_USED;
478 #define filter_chain_NewSPU( a, b ) filter_chain_NewSPU( VLC_OBJECT( a ), b )
481 * Creates a new video filter chain.
483 * \param obj pointer to parent VLC object
484 * \param change whether to allow changing the output format
485 * \param owner owner video buffer callbacks
486 * \return new filter chain, or NULL on error
488 VLC_API filter_chain_t * filter_chain_NewVideo( vlc_object_t *obj, bool change,
489 const filter_owner_t *owner )
490 VLC_USED;
491 #define filter_chain_NewVideo( a, b, c ) \
492 filter_chain_NewVideo( VLC_OBJECT( a ), b, c )
495 * Delete filter chain will delete all filters in the chain and free all
496 * allocated data. The pointer to the filter chain is then no longer valid.
498 * \param p_chain pointer to filter chain
500 VLC_API void filter_chain_Delete( filter_chain_t * );
503 * Reset filter chain will delete all filters in the chain and
504 * reset p_fmt_in and p_fmt_out to the new values.
506 * \param p_chain pointer to filter chain
507 * \param p_fmt_in new fmt_in params
508 * \paramt vctx_in new input video context
509 * \param p_fmt_out new fmt_out params
511 VLC_API void filter_chain_Reset( filter_chain_t *p_chain,
512 const es_format_t *p_fmt_in,
513 vlc_video_context *vctx_in,
514 const es_format_t *p_fmt_out );
517 * Remove all existing filters
519 * \param p_chain pointer to filter chain
521 VLC_API void filter_chain_Clear(filter_chain_t *);
524 * Append a filter to the chain.
526 * \param chain filter chain to append a filter to
527 * \param name filter name
528 * \param fmt_out filter output format
529 * \return a pointer to the filter or NULL on error
531 VLC_API filter_t *filter_chain_AppendFilter(filter_chain_t *chain,
532 const char *name, config_chain_t *cfg,
533 const es_format_t *fmt_out);
536 * Append a conversion to the chain.
538 * \param chain filter chain to append a filter to
539 * \param fmt_out filter output format
540 * \retval 0 on success
541 * \retval -1 on failure
543 VLC_API int filter_chain_AppendConverter(filter_chain_t *chain,
544 const es_format_t *fmt_out);
547 * Append new filter to filter chain from string.
549 * \param chain filter chain to append a filter to
550 * \param str filters chain nul-terminated string
552 VLC_API int filter_chain_AppendFromString(filter_chain_t *chain,
553 const char *str);
556 * Delete filter from filter chain. This function also releases the filter
557 * object and unloads the filter modules. The pointer to p_filter is no
558 * longer valid after this function successfully returns.
560 * \param chain filter chain to remove the filter from
561 * \param filter filter to remove from the chain and delete
563 VLC_API void filter_chain_DeleteFilter(filter_chain_t *chain,
564 filter_t *filter);
567 * Checks if the filter chain is empty.
569 * \param chain pointer to filter chain
570 * \return true if and only if there are no filters in this filter chain
572 VLC_API bool filter_chain_IsEmpty(const filter_chain_t *chain);
575 * Get last output format of the last element in the filter chain.
577 * \param chain filter chain
579 VLC_API const es_format_t *filter_chain_GetFmtOut(const filter_chain_t *chain);
582 * Get last output video context of the last element in the filter chain.
583 * \note doesn't create change the reference count
585 * \param chain filter chain
587 VLC_API vlc_video_context *filter_chain_GetVideoCtxOut(const filter_chain_t *chain);
590 * Apply the filter chain to a video picture.
592 * \param chain pointer to filter chain
593 * \param pic picture to apply filters to
594 * \return modified picture after applying all video filters
596 VLC_API picture_t *filter_chain_VideoFilter(filter_chain_t *chain,
597 picture_t *pic);
600 * Flush a video filter chain.
602 VLC_API void filter_chain_VideoFlush( filter_chain_t * );
605 * Generate subpictures from a chain of subpicture source "filters".
607 * \param chain filter chain
608 * \param display_date of subpictures
610 void filter_chain_SubSource(filter_chain_t *chain, spu_t *,
611 vlc_tick_t display_date);
614 * Apply filter chain to subpictures.
616 * \param chain filter chain
617 * \param subpic subpicture to apply filters on
618 * \return modified subpicture after applying all subpicture filters
620 VLC_API subpicture_t *filter_chain_SubFilter(filter_chain_t *chain,
621 subpicture_t *subpic);
624 * Apply the filter chain to a mouse state.
626 * It will be applied from the output to the input. It makes sense only
627 * for a video filter chain.
629 * The vlc_mouse_t* pointers may be the same.
631 VLC_API int filter_chain_MouseFilter( filter_chain_t *, struct vlc_mouse_t *,
632 const struct vlc_mouse_t * );
634 VLC_API int filter_chain_ForEach( filter_chain_t *chain,
635 int (*cb)( filter_t *, void * ), void *opaque );
637 /** @} */
638 #endif /* _VLC_FILTER_H */