]> git.sesse.net Git - vlc/blob - modules/video_filter/chain.c
Distribute mmx.h
[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, config_chain_t * );
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_CODEC_I420,
64     VLC_CODEC_I422,
65     VLC_CODEC_RGB32,
66     VLC_CODEC_RGB24,
67     0
68 };
69
70 struct filter_sys_t
71 {
72     filter_chain_t *p_chain;
73 };
74
75 #define CHAIN_LEVEL_MAX 1
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     if( !b_chroma && !b_resize )
92         return VLC_EGENERIC;
93
94     p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
95     if( !p_sys )
96         return VLC_ENOMEM;
97
98     memset( p_sys, 0, sizeof( *p_sys ) );
99
100     p_sys->p_chain = filter_chain_New( p_filter, "video filter2", false, BufferAllocationInit, NULL, p_filter );
101     if( !p_sys->p_chain )
102     {
103         free( p_sys );
104         return VLC_EGENERIC;
105     }
106
107     if( b_chroma && b_resize )
108         i_ret = BuildChromaResize( p_filter );
109     else if( b_chroma )
110         i_ret = BuildChromaChain( p_filter );
111     else
112         i_ret = VLC_EGENERIC;
113
114     if( i_ret )
115     {
116         /* Hum ... looks like this really isn't going to work. Too bad. */
117         filter_chain_Delete( p_sys->p_chain );
118         free( p_sys );
119         return VLC_EGENERIC;
120     }
121     /* */
122     p_filter->pf_video_filter = Chain;
123     return VLC_SUCCESS;
124 }
125
126 static void Destroy( vlc_object_t *p_this )
127 {
128     filter_t *p_filter = (filter_t *)p_this;
129     filter_chain_Delete( p_filter->p_sys->p_chain );
130     free( p_filter->p_sys );
131 }
132
133 /*****************************************************************************
134  * Chain
135  *****************************************************************************/
136 static picture_t *Chain( filter_t *p_filter, picture_t *p_pic )
137 {
138     return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
139 }
140
141 /*****************************************************************************
142  * Builders
143  *****************************************************************************/
144 static int BuildChromaResize( filter_t *p_filter )
145 {
146     filter_sys_t *p_sys = p_filter->p_sys;
147     es_format_t fmt_mid;
148     int i_ret;
149
150     /* Lets try resizing and then doing the chroma conversion */
151     filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &p_filter->fmt_out );
152
153     msg_Dbg( p_filter, "Trying to build resize+chroma" );
154     EsFormatMergeSize( &fmt_mid, &p_filter->fmt_in, &p_filter->fmt_out );
155     i_ret = CreateChain( p_sys->p_chain, &fmt_mid, NULL );
156     es_format_Clean( &fmt_mid );
157     if( i_ret == VLC_SUCCESS )
158         return VLC_SUCCESS;
159
160     /* Lets try it the other way arround (chroma and then resize) */
161     filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &p_filter->fmt_out );
162
163     msg_Dbg( p_filter, "Trying to build chroma+resize" );
164     EsFormatMergeSize( &fmt_mid, &p_filter->fmt_out, &p_filter->fmt_in );
165     i_ret = CreateChain( p_sys->p_chain, &fmt_mid, NULL );
166     es_format_Clean( &fmt_mid );
167     if( i_ret == VLC_SUCCESS )
168         return VLC_SUCCESS;
169
170     return VLC_EGENERIC;
171 }
172
173 static int BuildChromaChain( filter_t *p_filter )
174 {
175     filter_sys_t *p_sys = p_filter->p_sys;
176     es_format_t fmt_mid;
177
178     /* We have to protect ourself against a too high recursion */
179     const char *psz_option = MODULE_STRING"-level";
180     int i_level = 0;
181     for( const config_chain_t *c = p_filter->p_cfg; c != NULL; c = c->p_next)
182     {
183         if( c->psz_name && c->psz_value && !strcmp(c->psz_name, psz_option) )
184         {
185             i_level = atoi(c->psz_value);
186             if( i_level < 0 || i_level > CHAIN_LEVEL_MAX )
187             {
188                 msg_Err( p_filter, "Too high level of recursion (%d)", i_level );
189                 return VLC_EGENERIC;
190             }
191             break;
192         }
193     }
194
195     /* */
196     int i_ret = VLC_EGENERIC;
197
198     /* */
199     config_chain_t cfg_level;
200     memset(&cfg_level, 0, sizeof(cfg_level));
201     cfg_level.psz_name = strdup(psz_option);
202     if( asprintf( &cfg_level.psz_value, "%d", i_level + 1) < 0 )
203         cfg_level.psz_value = NULL;
204     if( !cfg_level.psz_name || !cfg_level.psz_value )
205         goto exit;
206
207     /* Now try chroma format list */
208     for( int i = 0; pi_allowed_chromas[i]; i++ )
209     {
210         const vlc_fourcc_t i_chroma = pi_allowed_chromas[i];
211         if( i_chroma == p_filter->fmt_in.i_codec ||
212             i_chroma == p_filter->fmt_out.i_codec )
213             continue;
214
215         msg_Dbg( p_filter, "Trying to use chroma %4.4s as middle man",
216                  (char*)&i_chroma );
217
218         es_format_Copy( &fmt_mid, &p_filter->fmt_in );
219         fmt_mid.i_codec        =
220         fmt_mid.video.i_chroma = i_chroma;
221         fmt_mid.video.i_rmask  = 0;
222         fmt_mid.video.i_gmask  = 0;
223         fmt_mid.video.i_bmask  = 0;
224         video_format_FixRgb(&fmt_mid.video);
225
226         filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &p_filter->fmt_out );
227
228         i_ret = CreateChain( p_sys->p_chain, &fmt_mid, &cfg_level );
229         es_format_Clean( &fmt_mid );
230
231         if( i_ret == VLC_SUCCESS )
232             break;
233     }
234
235 exit:
236     free( cfg_level.psz_name );
237     free( cfg_level.psz_value );
238     return i_ret;
239 }
240
241 /*****************************************************************************
242  * Buffer management
243  *****************************************************************************/
244 static picture_t *BufferNew( filter_t *p_filter )
245 {
246     filter_t *p_parent = (filter_t*)p_filter->p_owner;
247
248     return filter_NewPicture( p_parent );
249 }
250 static void BufferDel( filter_t *p_filter, picture_t *p_pic )
251 {
252     filter_t *p_parent = (filter_t*)p_filter->p_owner;
253
254     return filter_DeletePicture( p_parent, p_pic );
255 }
256 static int BufferAllocationInit ( filter_t *p_filter, void *p_data )
257 {
258     p_filter->pf_video_buffer_new = BufferNew;
259     p_filter->pf_video_buffer_del = BufferDel;
260     p_filter->p_owner = p_data;
261     return VLC_SUCCESS;
262 }
263
264 /*****************************************************************************
265  *
266  *****************************************************************************/
267 static int CreateChain( filter_chain_t *p_chain, es_format_t *p_fmt_mid, config_chain_t *p_cfg )
268 {
269     filter_t *p_filter1;
270     if( !( p_filter1 =
271            filter_chain_AppendFilter( p_chain, NULL, p_cfg, NULL, p_fmt_mid )) )
272         return VLC_EGENERIC;
273     if( !filter_chain_AppendFilter( p_chain, NULL, p_cfg, p_fmt_mid, NULL ) )
274     {
275         filter_chain_DeleteFilter( p_chain, p_filter1 );
276         return VLC_EGENERIC;
277     }
278     return VLC_SUCCESS;
279 }
280
281 static void EsFormatMergeSize( es_format_t *p_dst,
282                                const es_format_t *p_base,
283                                const es_format_t *p_size )
284 {
285     es_format_Copy( p_dst, p_base );
286
287     p_dst->video.i_width  = p_size->video.i_width;
288     p_dst->video.i_height = p_size->video.i_height;
289
290     p_dst->video.i_visible_width  = p_size->video.i_visible_width;
291     p_dst->video.i_visible_height = p_size->video.i_visible_height;
292 }
293