]> git.sesse.net Git - vlc/blob - modules/video_filter/chain.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / video_filter / chain.c
1 /*****************************************************************************
2  * chain.c : chain multiple video filter modules as a last resort solution
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int       Activate   ( vlc_object_t * );
40 static void      Destroy    ( vlc_object_t * );
41
42 vlc_module_begin ()
43     set_description( N_("Video filtering using a chain of video filter modules") )
44     set_capability( "video filter2", 1 )
45     set_callbacks( Activate, Destroy )
46 vlc_module_end ()
47
48 /*****************************************************************************
49  * Local prototypes.
50  *****************************************************************************/
51 static picture_t *Chain         ( filter_t *, picture_t * );
52 static int BufferAllocationInit ( filter_t *, void * );
53
54 static int BuildChromaResize( filter_t * );
55 static int BuildChromaChain( filter_t *p_filter );
56
57 static int CreateChain( filter_chain_t *p_chain, es_format_t *p_fmt_mid );
58 static void EsFormatMergeSize( es_format_t *p_dst,
59                                const es_format_t *p_base,
60                                const es_format_t *p_size );
61
62 static const vlc_fourcc_t pi_allowed_chromas[] = {
63     VLC_FOURCC('I','4','2','0'),
64     VLC_FOURCC('I','4','2','2'),
65     VLC_FOURCC('R','V','3','2'),
66     VLC_FOURCC('R','V','2','4'),
67     0
68 };
69
70 struct filter_sys_t
71 {
72     filter_chain_t *p_chain;
73 };
74
75 #define CHAIN_LEVEL_MAX 4
76
77 /*****************************************************************************
78  * Activate: allocate a chroma function
79  *****************************************************************************
80  * This function allocates and initializes a chroma function
81  *****************************************************************************/
82 static int Activate( vlc_object_t *p_this )
83 {
84     filter_t *p_filter = (filter_t *)p_this;
85     filter_sys_t *p_sys;
86     int i_ret;
87
88     const bool b_chroma = p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma;
89     const bool b_resize = p_filter->fmt_in.video.i_width  != p_filter->fmt_out.video.i_width ||
90                           p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height;
91
92     /* XXX Remove check on b_resize to build chroma chain (untested) */
93     if( !b_chroma || !b_resize )
94         return VLC_EGENERIC;
95
96     p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
97     if( !p_sys )
98         return VLC_ENOMEM;
99
100     memset( p_sys, 0, sizeof( *p_sys ) );
101
102     p_sys->p_chain = filter_chain_New( p_filter, "video filter2", false, BufferAllocationInit, NULL, p_filter );
103     if( !p_sys->p_chain )
104     {
105         free( p_sys );
106         return VLC_EGENERIC;
107     }
108     filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &p_filter->fmt_out );
109
110     if( b_chroma && b_resize )
111         i_ret = BuildChromaResize( p_filter );
112     else if( b_chroma )
113         i_ret = BuildChromaChain( p_filter );
114     else
115         i_ret = VLC_EGENERIC;
116
117     if( i_ret )
118     {
119         /* Hum ... looks like this really isn't going to work. Too bad. */
120         filter_chain_Delete( p_sys->p_chain );
121         free( p_sys );
122         return VLC_EGENERIC;
123     }
124     /* */
125     p_filter->pf_video_filter = Chain;
126     return VLC_SUCCESS;
127 }
128
129 static void Destroy( vlc_object_t *p_this )
130 {
131     filter_t *p_filter = (filter_t *)p_this;
132     filter_chain_Delete( p_filter->p_sys->p_chain );
133     free( p_filter->p_sys );
134 }
135
136 /*****************************************************************************
137  * Chain
138  *****************************************************************************/
139 static picture_t *Chain( filter_t *p_filter, picture_t *p_pic )
140 {
141     return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
142 }
143
144 /*****************************************************************************
145  * Builders
146  *****************************************************************************/
147 static int BuildChromaResize( filter_t *p_filter )
148 {
149     filter_sys_t *p_sys = p_filter->p_sys;
150     es_format_t fmt_mid;
151     int i_ret;
152
153     /* Lets try resizing and then doing the chroma conversion */
154     msg_Dbg( p_filter, "Trying to build resize+chroma" );
155     EsFormatMergeSize( &fmt_mid, &p_filter->fmt_in, &p_filter->fmt_out );
156     i_ret = CreateChain( p_sys->p_chain, &fmt_mid );
157     es_format_Clean( &fmt_mid );
158     if( i_ret == VLC_SUCCESS )
159         return VLC_SUCCESS;
160
161     /* Lets try it the other way arround (chroma and then resize) */
162     msg_Dbg( p_filter, "Trying to build chroma+resize" );
163     EsFormatMergeSize( &fmt_mid, &p_filter->fmt_out, &p_filter->fmt_in );
164     i_ret = CreateChain( p_sys->p_chain, &fmt_mid );
165     es_format_Clean( &fmt_mid );
166     if( i_ret == VLC_SUCCESS )
167         return VLC_SUCCESS;
168
169     return VLC_EGENERIC;
170 }
171
172 static int BuildChromaChain( filter_t *p_filter )
173 {
174     filter_sys_t *p_sys = p_filter->p_sys;
175     es_format_t fmt_mid;
176     int i_ret;
177     int i;
178
179     /* We have to protect ourself against a too high recursion */
180     const char *psz_option = MODULE_STRING"-level";
181     bool b_first = !var_Type( p_filter, psz_option );
182
183     if( var_Create( p_filter, MODULE_STRING"-level", VLC_VAR_INTEGER | (b_first ? VLC_VAR_DOINHERIT : 0 ) ) )
184     {
185         msg_Err( p_filter, "Failed to create %s variable", psz_option );
186         return VLC_EGENERIC;
187     }
188     int i_level = var_GetInteger( p_filter, psz_option );
189     if( i_level >= CHAIN_LEVEL_MAX )
190     {
191         msg_Err( p_filter, "Too high level of recursion (%d)", i_level );
192         return VLC_EGENERIC;
193     }
194     var_SetInteger( p_filter, psz_option, i_level + 1 );
195
196     /* Now try chroma format list */
197     for( i = 0; pi_allowed_chromas[i]; i++ )
198     {
199         const vlc_fourcc_t i_chroma = pi_allowed_chromas[i];
200
201         msg_Dbg( p_filter, "Trying to use chroma %4.4s as middle man",
202                  (char*)&i_chroma );
203
204         es_format_Copy( &fmt_mid, &p_filter->fmt_in );
205         fmt_mid.video.i_chroma = i_chroma;
206
207         i_ret = CreateChain( p_sys->p_chain, &fmt_mid );
208         es_format_Clean( &fmt_mid );
209
210         if( i_ret == VLC_SUCCESS )
211             return VLC_SUCCESS;
212     }
213     return VLC_EGENERIC;
214 }
215
216 /*****************************************************************************
217  * Buffer management
218  *****************************************************************************/
219 static picture_t *BufferNew( filter_t *p_filter )
220 {
221     filter_t *p_parent = (filter_t*)p_filter->p_owner;
222
223     return p_parent->pf_vout_buffer_new( p_parent );
224 }
225 static void BufferDel( filter_t *p_filter, picture_t *p_pic )
226 {
227     filter_t *p_parent = (filter_t*)p_filter->p_owner;
228
229     p_parent->pf_vout_buffer_del( p_parent, p_pic );
230 }
231 static int BufferAllocationInit ( filter_t *p_filter, void *p_data )
232 {
233     p_filter->pf_vout_buffer_new = BufferNew;
234     p_filter->pf_vout_buffer_del = BufferDel;
235     p_filter->p_owner = p_data;
236     return VLC_SUCCESS;
237 }
238
239 /*****************************************************************************
240  *
241  *****************************************************************************/
242 static int CreateChain( filter_chain_t *p_chain, es_format_t *p_fmt_mid )
243 {
244     filter_t *p_filter1;
245     if( !( p_filter1 =
246            filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, p_fmt_mid )) )
247         return VLC_EGENERIC;
248     if( !filter_chain_AppendFilter( p_chain, NULL, NULL, p_fmt_mid, NULL ) )
249     {
250         filter_chain_DeleteFilter( p_chain, p_filter1 );
251         return VLC_EGENERIC;
252     }
253     return VLC_SUCCESS;
254 }
255
256 static void EsFormatMergeSize( es_format_t *p_dst,
257                                const es_format_t *p_base,
258                                const es_format_t *p_size )
259 {
260     es_format_Copy( p_dst, p_base );
261
262     p_dst->video.i_width  = p_size->video.i_width;
263     p_dst->video.i_height = p_size->video.i_height;
264
265     p_dst->video.i_visible_width  = p_size->video.i_visible_width;
266     p_dst->video.i_visible_height = p_size->video.i_visible_height;
267 }
268