VLC  4.0.0-dev
vlc_filter.h
Go to the documentation of this file.
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_codec.h>
31 
33 
34 /**
35  * \defgroup filter Filters
36  * \ingroup output
37  * Audio, video, text filters
38  * @{
39  * \file
40  * Filter modules interface
41  */
42 
44 {
45  picture_t *(*buffer_new)(filter_t *);
46  vlc_decoder_device * (*hold_device)(vlc_object_t *, void *sys);
47 };
48 
50 {
51  subpicture_t *(*buffer_new)(filter_t *);
52 };
53 
54 typedef struct filter_owner_t
55 {
56  union
57  {
58  const struct filter_video_callbacks *video;
59  const struct filter_subpicture_callbacks *sub;
60  };
61  void *sys;
63 
64 struct vlc_mouse_t;
65 
66 /** Structure describing a filter
67  * @warning BIG FAT WARNING : the code relies on the first 4 members of
68  * filter_t and decoder_t to be the same, so if you have anything to add,
69  * do it at the end of the structure.
70  */
71 struct filter_t
72 {
73  struct vlc_object_t obj;
74 
75  /* Module properties */
76  module_t * p_module;
77  void *p_sys;
78 
79  /* Input format */
80  es_format_t fmt_in;
81  vlc_video_context *vctx_in; // video filter, set by owner
82 
83  /* Output format of filter */
84  es_format_t fmt_out;
85  vlc_video_context *vctx_out; // video filter, handled by the filter
86  bool b_allow_fmt_out_change;
87 
88  /* Name of the "video filter" shortcut that is requested, can be NULL */
89  const char * psz_name;
90  /* Filter configuration */
91  config_chain_t * p_cfg;
92 
93  union
94  {
95  /** Filter a picture (video filter) */
96  picture_t * (*pf_video_filter)( filter_t *, picture_t * );
97 
98  /** Filter an audio block (audio filter) */
99  block_t * (*pf_audio_filter)( filter_t *, block_t * );
101  /** Blend a subpicture onto a picture (blend) */
102  void (*pf_video_blend)( filter_t *, picture_t *, const picture_t *,
103  int, int, int );
104 
105  /** Generate a subpicture (sub source) */
106  subpicture_t *(*pf_sub_source)( filter_t *, vlc_tick_t );
108  /** Filter a subpicture (sub filter) */
109  subpicture_t *(*pf_sub_filter)( filter_t *, subpicture_t * );
111  /** Render text (text render) */
112  int (*pf_render)( filter_t *, subpicture_region_t *,
113  subpicture_region_t *, const vlc_fourcc_t * );
114  };
115 
116  union
117  {
118  /* TODO: video filter drain */
119  /** Drain (audio filter) */
120  block_t *(*pf_audio_drain) ( filter_t * );
121  };
122 
123  /** Flush
124  *
125  * Flush (i.e. discard) any internal buffer in a video or audio filter.
126  */
127  void (*pf_flush)( filter_t * );
129  /** Change viewpoint
130  *
131  * Pass a new viewpoint to audio filters. Filters like the spatialaudio one
132  * used for Ambisonics rendering will change its output according to this
133  * viewpoint.
134  */
135  void (*pf_change_viewpoint)( filter_t *, const vlc_viewpoint_t * );
137  union
138  {
139  /** Filter mouse state (video filter).
140  *
141  * If non-NULL, you must convert from output to input formats:
142  * - If VLC_SUCCESS is returned, the mouse state is propagated.
143  * - Otherwise, the mouse change is not propagated.
144  * If NULL, the mouse state is considered unchanged and will be
145  * propagated. */
146  int (*pf_video_mouse)( filter_t *, struct vlc_mouse_t *,
147  const struct vlc_mouse_t *p_old,
148  const struct vlc_mouse_t *p_new );
149  };
150 
151  /* Input attachments
152  * XXX use filter_GetInputAttachments */
153  int (*pf_get_attachments)( filter_t *, input_attachment_t ***, int * );
155  /** Private structure for the owner of the filter */
156  filter_owner_t owner;
157 };
158 
159 /**
160  * This function will return a new picture usable by p_filter as an output
161  * buffer. You have to release it using picture_Release or by returning
162  * it to the caller as a pf_video_filter return value.
163  * Provided for convenience.
164  *
165  * \param p_filter filter_t object
166  * \return new picture on success or NULL on failure
167  */
168 static inline picture_t *filter_NewPicture( filter_t *p_filter )
169 {
170  picture_t *pic = NULL;
171  if ( p_filter->owner.video != NULL && p_filter->owner.video->buffer_new != NULL)
172  pic = p_filter->owner.video->buffer_new( p_filter );
173  if ( pic == NULL )
174  {
175  // legacy filter owners not setting a default filter_allocator
176  pic = picture_NewFromFormat( &p_filter->fmt_out.video );
177  }
178  if( pic == NULL )
179  msg_Warn( p_filter, "can't get output picture" );
180  return pic;
181 }
182 
183 /**
184  * Flush a filter
185  *
186  * This function will flush the state of a filter (audio or video).
187  */
188 static inline void filter_Flush( filter_t *p_filter )
189 {
190  if( p_filter->pf_flush != NULL )
191  p_filter->pf_flush( p_filter );
192 }
193 
194 static inline void filter_ChangeViewpoint( filter_t *p_filter,
195  const vlc_viewpoint_t *vp)
196 {
197  if( p_filter->pf_change_viewpoint != NULL )
198  p_filter->pf_change_viewpoint( p_filter, vp );
199 }
200 
201 static inline vlc_decoder_device * filter_HoldDecoderDevice( filter_t *p_filter )
202 {
203  if ( !p_filter->owner.video || !p_filter->owner.video->hold_device )
204  return NULL;
205 
206  return p_filter->owner.video->hold_device( VLC_OBJECT(p_filter), p_filter->owner.sys );
207 }
208 
209 static inline vlc_decoder_device * filter_HoldDecoderDeviceType( filter_t *p_filter,
211 {
212  if ( !p_filter->owner.video || !p_filter->owner.video->hold_device )
213  return NULL;
214 
215  vlc_decoder_device *dec_dev = p_filter->owner.video->hold_device( VLC_OBJECT(p_filter),
216  p_filter->owner.sys );
217  if ( dec_dev != NULL )
218  {
219  if ( dec_dev->type == type )
220  return dec_dev;
222  }
223  return NULL;
224 }
225 
226 /**
227  * This function will drain, then flush an audio filter.
228  */
229 static inline block_t *filter_DrainAudio( filter_t *p_filter )
230 {
231  if( p_filter->pf_audio_drain )
232  return p_filter->pf_audio_drain( p_filter );
233  else
234  return NULL;
235 }
236 
237 /**
238  * This function will return a new subpicture usable by p_filter as an output
239  * buffer. You have to release it using subpicture_Delete or by returning it to
240  * the caller as a pf_sub_source return value.
241  * Provided for convenience.
242  *
243  * \param p_filter filter_t object
244  * \return new subpicture
245  */
246 static inline subpicture_t *filter_NewSubpicture( filter_t *p_filter )
247 {
248  subpicture_t *subpic = p_filter->owner.sub->buffer_new( p_filter );
249  if( subpic == NULL )
250  msg_Warn( p_filter, "can't get output subpicture" );
251  return subpic;
252 }
253 
254 /**
255  * This function gives all input attachments at once.
256  *
257  * You MUST release the returned values
258  */
259 static inline int filter_GetInputAttachments( filter_t *p_filter,
260  input_attachment_t ***ppp_attachment,
261  int *pi_attachment )
262 {
263  if( !p_filter->pf_get_attachments )
264  return VLC_EGENERIC;
265  return p_filter->pf_get_attachments( p_filter,
266  ppp_attachment, pi_attachment );
267 }
268 
269 /**
270  * This function duplicates every variables from the filter, and adds a proxy
271  * callback to trigger filter events from obj.
272  *
273  * \param restart_cb a vlc_callback_t to call if the event means restarting the
274  * filter (i.e. an event on a non-command variable)
275  */
277  vlc_callback_t restart_cb );
278 # define filter_AddProxyCallbacks(a, b, c) \
279  filter_AddProxyCallbacks(VLC_OBJECT(a), b, c)
280 
281 /**
282  * This function removes the callbacks previously added to every duplicated
283  * variables, and removes them afterward.
284  *
285  * \param restart_cb the same vlc_callback_t passed to filter_AddProxyCallbacks
286  */
288  vlc_callback_t restart_cb);
289 # define filter_DelProxyCallbacks(a, b, c) \
290  filter_DelProxyCallbacks(VLC_OBJECT(a), b, c)
291 
292 typedef filter_t vlc_blender_t;
294 /**
295  * It creates a blend filter.
296  *
297  * Only the chroma properties of the dest format is used (chroma
298  * type, rgb masks and shifts)
299  */
301 
302 /**
303  * It configures blend filter parameters that are allowed to changed
304  * after the creation.
305  */
306 VLC_API int filter_ConfigureBlend( vlc_blender_t *, int i_dst_width, int i_dst_height, const video_format_t *p_src );
307 
308 /**
309  * It blends a picture into another one.
310  *
311  * The input picture is not modified and not released.
312  */
313 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 );
314 
315 /**
316  * It destroys a blend filter created by filter_NewBlend.
317  */
319 
320 /**
321  * Create a picture_t *(*)( filter_t *, picture_t * ) compatible wrapper
322  * using a void (*)( filter_t *, picture_t *, picture_t * ) function
323  *
324  * Currently used by the chroma video filters
325  */
326 #define VIDEO_FILTER_WRAPPER( name ) \
327  static picture_t *name ## _Filter ( filter_t *p_filter, \
328  picture_t *p_pic ) \
329  { \
330  picture_t *p_outpic = filter_NewPicture( p_filter ); \
331  if( p_outpic ) \
332  { \
333  name( p_filter, p_pic, p_outpic ); \
334  picture_CopyProperties( p_outpic, p_pic ); \
335  } \
336  picture_Release( p_pic ); \
337  return p_outpic; \
338  }
339 
340 /**
341  * Filter chain management API
342  * The filter chain management API is used to dynamically construct filters
343  * and add them in a chain.
344  */
345 
346 typedef struct filter_chain_t filter_chain_t;
348 /**
349  * Create new filter chain
350  *
351  * \param obj pointer to a vlc object
352  * \param psz_capability vlc capability of filters in filter chain
353  * \return pointer to a filter chain
354  */
355 filter_chain_t * filter_chain_NewSPU( vlc_object_t *obj, const char *psz_capability )
356 VLC_USED;
357 #define filter_chain_NewSPU( a, b ) filter_chain_NewSPU( VLC_OBJECT( a ), b )
359 /**
360  * Creates a new video filter chain.
361  *
362  * \param obj pointer to parent VLC object
363  * \param change whether to allow changing the output format
364  * \param owner owner video buffer callbacks
365  * \return new filter chain, or NULL on error
366  */
368  const filter_owner_t *owner )
369 VLC_USED;
370 #define filter_chain_NewVideo( a, b, c ) \
371  filter_chain_NewVideo( VLC_OBJECT( a ), b, c )
372 
373 /**
374  * Delete filter chain will delete all filters in the chain and free all
375  * allocated data. The pointer to the filter chain is then no longer valid.
376  *
377  * \param p_chain pointer to filter chain
378  */
380 
381 /**
382  * Reset filter chain will delete all filters in the chain and
383  * reset p_fmt_in and p_fmt_out to the new values.
384  *
385  * \param p_chain pointer to filter chain
386  * \param p_fmt_in new fmt_in params
387  * \paramt vctx_in new input video context
388  * \param p_fmt_out new fmt_out params
389  */
391  const es_format_t *p_fmt_in,
393  const es_format_t *p_fmt_out );
394 
395 /**
396  * Remove all existing filters
397  *
398  * \param p_chain pointer to filter chain
399  */
401 
402 /**
403  * Append a filter to the chain.
404  *
405  * \param chain filter chain to append a filter to
406  * \param name filter name
407  * \param fmt_out filter output format
408  * \return a pointer to the filter or NULL on error
409  */
411  const char *name, config_chain_t *cfg,
412  const es_format_t *fmt_out);
413 
414 /**
415  * Append a conversion to the chain.
416  *
417  * \param chain filter chain to append a filter to
418  * \param fmt_out filter output format
419  * \retval 0 on success
420  * \retval -1 on failure
421  */
423  const es_format_t *fmt_out);
424 
425 /**
426  * Append new filter to filter chain from string.
427  *
428  * \param chain filter chain to append a filter to
429  * \param str filters chain nul-terminated string
430  */
432  const char *str);
433 
434 /**
435  * Delete filter from filter chain. This function also releases the filter
436  * object and unloads the filter modules. The pointer to p_filter is no
437  * longer valid after this function successfully returns.
438  *
439  * \param chain filter chain to remove the filter from
440  * \param filter filter to remove from the chain and delete
441  */
443  filter_t *filter);
444 
445 /**
446  * Checks if the filter chain is empty.
447  *
448  * \param chain pointer to filter chain
449  * \return true if and only if there are no filters in this filter chain
450  */
451 VLC_API bool filter_chain_IsEmpty(const filter_chain_t *chain);
452 
453 /**
454  * Get last output format of the last element in the filter chain.
455  *
456  * \param chain filter chain
457  */
459 
460 /**
461  * Get last output video context of the last element in the filter chain.
462  * \note doesn't create change the reference count
463  *
464  * \param chain filter chain
465  */
467 
468 /**
469  * Apply the filter chain to a video picture.
470  *
471  * \param chain pointer to filter chain
472  * \param pic picture to apply filters to
473  * \return modified picture after applying all video filters
474  */
476  picture_t *pic);
477 
478 /**
479  * Flush a video filter chain.
480  */
482 
483 /**
484  * Generate subpictures from a chain of subpicture source "filters".
485  *
486  * \param chain filter chain
487  * \param display_date of subpictures
488  */
490  vlc_tick_t display_date);
491 
492 /**
493  * Apply filter chain to subpictures.
494  *
495  * \param chain filter chain
496  * \param subpic subpicture to apply filters on
497  * \return modified subpicture after applying all subpicture filters
498  */
500  subpicture_t *subpic);
501 
502 /**
503  * Apply the filter chain to a mouse state.
504  *
505  * It will be applied from the output to the input. It makes sense only
506  * for a video filter chain.
507  *
508  * The vlc_mouse_t* pointers may be the same.
509  */
511  const struct vlc_mouse_t * );
512 
514  int (*cb)( filter_t *, void * ), void *opaque );
515 
516 /** @} */
517 #endif /* _VLC_FILTER_H */
Definition: vlc_input.h:160
This file defines picture structures and functions in vlc.
const es_format_t * filter_chain_GetFmtOut(const filter_chain_t *chain)
Get last output format of the last element in the filter chain.
Definition: filter_chain.c:406
static int filter_GetInputAttachments(filter_t *p_filter, input_attachment_t ***ppp_attachment, int *pi_attachment)
This function gives all input attachments at once.
Definition: vlc_filter.h:260
void filter_chain_VideoFlush(filter_chain_t *)
Flush a video filter chain.
Definition: filter_chain.c:467
int filter_chain_ForEach(filter_chain_t *chain, int(*cb)(filter_t *, void *), void *opaque)
Definition: filter_chain.c:389
Video picture.
Definition: vlc_picture.h:127
filter_owner_t owner
Private structure for the owner of the filter.
Definition: vlc_filter.h:157
es_format_t fmt_out
Definition: vlc_filter.h:85
#define filter_chain_NewSPU(a, b)
Definition: vlc_filter.h:358
Video subtitle region.
Definition: vlc_subpicture.h:59
int(* pf_get_attachments)(filter_t *, input_attachment_t ***, int *)
Definition: vlc_filter.h:154
void(* pf_change_viewpoint)(filter_t *, const vlc_viewpoint_t *)
Change viewpoint.
Definition: vlc_filter.h:136
block_t *(* pf_audio_drain)(filter_t *)
Drain (audio filter)
Definition: vlc_filter.h:121
int filter_chain_AppendFromString(filter_chain_t *chain, const char *str)
Append new filter to filter chain from string.
Definition: filter_chain.c:345
vlc_decoder_device *(* hold_device)(vlc_object_t *, void *sys)
Definition: vlc_filter.h:47
Video subtitle.
Definition: vlc_subpicture.h:166
void filter_DeleteBlend(vlc_blender_t *)
It destroys a blend filter created by filter_NewBlend.
Definition: filter.c:171
Internal module descriptor.
Definition: modules.h:75
subpicture_t * filter_chain_SubFilter(filter_chain_t *chain, subpicture_t *subpic)
Apply filter chain to subpictures.
Definition: filter_chain.c:492
subpicture_t *(* buffer_new)(filter_t *)
Definition: vlc_filter.h:52
#define msg_Warn(p_this,...)
Definition: vlc_messages.h:104
static block_t * filter_DrainAudio(filter_t *p_filter)
This function will drain, then flush an audio filter.
Definition: vlc_filter.h:230
picture_t * filter_chain_VideoFilter(filter_chain_t *chain, picture_t *pic)
Apply the filter chain to a video picture.
Definition: filter_chain.c:444
const char * psz_name
Definition: text_style.c:33
Definition: decoder_helpers.c:232
static picture_t * filter_NewPicture(filter_t *p_filter)
This function will return a new picture usable by p_filter as an output buffer.
Definition: vlc_filter.h:169
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45
static vlc_decoder_device * filter_HoldDecoderDeviceType(filter_t *p_filter, enum vlc_decoder_device_type type)
Definition: vlc_filter.h:210
Definition: vlc_configuration.h:331
uint32_t vlc_fourcc_t
Definition: fourcc_gen.c:33
vlc_video_context * vctx_in
Chain input video context (set on Reset)
Definition: filter_chain.c:54
void * sys
Definition: vlc_filter.h:62
Viewpoints.
Definition: vlc_viewpoint.h:41
Definition: vlc_es.h:617
bool filter_chain_IsEmpty(const filter_chain_t *chain)
Checks if the filter chain is empty.
Definition: filter_chain.c:401
void filter_chain_DeleteFilter(filter_chain_t *chain, filter_t *filter)
Delete filter from filter chain.
Definition: filter_chain.c:310
void filter_chain_Reset(filter_chain_t *p_chain, const es_format_t *p_fmt_in, vlc_video_context *vctx_in, const es_format_t *p_fmt_out)
Reset filter chain will delete all filters in the chain and reset p_fmt_in and p_fmt_out to the new v...
Definition: filter_chain.c:186
video format description
Definition: vlc_es.h:349
#define filter_chain_NewVideo(a, b, c)
Definition: vlc_filter.h:371
picture_t *(* buffer_new)(filter_t *)
Definition: vlc_filter.h:46
picture_t * picture_NewFromFormat(const video_format_t *restrict fmt)
Definition: picture.c:259
int filter_chain_AppendConverter(filter_chain_t *chain, const es_format_t *fmt_out)
Append a conversion to the chain.
Definition: filter_chain.c:303
Decoder context struct.
Definition: vlc_codec.h:577
const char name[16]
Definition: httpd.c:1269
Mouse state.
Definition: vlc_mouse.h:45
vlc_video_context * filter_chain_GetVideoCtxOut(const filter_chain_t *chain)
Get last output video context of the last element in the filter chain.
Definition: filter_chain.c:415
void filter_chain_Clear(filter_chain_t *)
Remove all existing filters.
Definition: filter_chain.c:163
struct filter_owner_t filter_owner_t
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)
It blends a picture into another one.
Definition: filter.c:160
#define filter_DelProxyCallbacks(a, b, c)
Definition: vlc_filter.h:290
Definition: vlc_filter.h:44
#define VLC_API
Definition: fourcc_gen.c:31
#define VLC_OBJECT(x)
Type-safe vlc_object_t cast.
Definition: vlc_objects.h:70
int filter_ConfigureBlend(vlc_blender_t *, int i_dst_width, int i_dst_height, const video_format_t *p_src)
It configures blend filter parameters that are allowed to changed after the creation.
Definition: filter.c:128
filter_t vlc_blender_t
Definition: vlc_filter.h:293
Subpicture unit descriptor.
Definition: vlc_spu.h:47
const struct filter_subpicture_callbacks * sub
Definition: vlc_filter.h:60
#define VLC_EGENERIC
Unspecified error.
Definition: vlc_common.h:472
video_format_t video
description of video format
Definition: vlc_es.h:646
Structure describing a filter.
Definition: vlc_filter.h:72
static vlc_decoder_device * filter_HoldDecoderDevice(filter_t *p_filter)
Definition: vlc_filter.h:202
int(* vlc_callback_t)(vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void *)
Definition: vlc_common.h:491
enum vlc_decoder_device_type type
Must be set from the "decoder device" module open entry point.
Definition: vlc_codec.h:588
const struct filter_video_callbacks * video
Definition: vlc_filter.h:59
Definition: vlc_block.h:117
Definition: vlc_filter.h:50
Definition: filter_chain.c:46
static void filter_ChangeViewpoint(filter_t *p_filter, const vlc_viewpoint_t *vp)
Definition: vlc_filter.h:195
vlc_blender_t * filter_NewBlend(vlc_object_t *, const video_format_t *p_dst_chroma)
It creates a blend filter.
Definition: filter.c:104
Decoder and encoder modules interface.
static subpicture_t * filter_NewSubpicture(filter_t *p_filter)
This function will return a new subpicture usable by p_filter as an output buffer.
Definition: vlc_filter.h:247
void vlc_decoder_device_Release(vlc_decoder_device *device)
Release a decoder device.
Definition: decoder_helpers.c:217
filter_t * filter_chain_AppendFilter(filter_chain_t *chain, const char *name, config_chain_t *cfg, const es_format_t *fmt_out)
Append a filter to the chain.
Definition: filter_chain.c:295
static void filter_Flush(filter_t *p_filter)
Flush a filter.
Definition: vlc_filter.h:189
This file defines the elementary streams format types.
es_format_t fmt_out
Chain output format (constant)
Definition: filter_chain.c:55
VLC object common members.
Definition: vlc_objects.h:43
void(* pf_flush)(filter_t *)
Flush.
Definition: vlc_filter.h:128
int filter_chain_MouseFilter(filter_chain_t *, struct vlc_mouse_t *, const struct vlc_mouse_t *)
Apply the filter chain to a mouse state.
vlc_decoder_device_type
Decoder device type.
Definition: vlc_codec.h:557
Definition: vlc_filter.h:55
#define VLC_USED
Definition: fourcc_gen.c:32
#define filter_AddProxyCallbacks(a, b, c)
Definition: vlc_filter.h:279
void filter_chain_SubSource(filter_chain_t *chain, spu_t *, vlc_tick_t display_date)
Generate subpictures from a chain of subpicture source "filters".
Definition: filter_chain.c:480
void filter_chain_Delete(filter_chain_t *)
Delete filter chain will delete all filters in the chain and free all allocated data.
Definition: filter_chain.c:172