]> git.sesse.net Git - vlc/blob - modules/video_filter/chain.c
Fix implicit filter chain module.
[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  * Local and extern prototypes.
38  *****************************************************************************/
39 static int  Activate   ( vlc_object_t * );
40 static void Destroy    ( vlc_object_t * );
41 static picture_t *Chain( filter_t *, picture_t * );
42 static int AllocInit( filter_t *p_filter, void *p_data );
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 vlc_module_begin();
48     set_description( N_("Video filtering using a chain of video filter modules") );
49     set_capability( "video filter2", 1 );
50     set_callbacks( Activate, Destroy );
51 vlc_module_end();
52
53 #define MAX_FILTERS 4
54
55 struct filter_sys_t
56 {
57     filter_chain_t *p_chain;
58 };
59
60 static const vlc_fourcc_t pi_allowed_chromas[] = {
61     VLC_FOURCC('I','4','2','0'),
62     VLC_FOURCC('I','4','2','2'),
63     VLC_FOURCC('R','V','3','2'),
64     VLC_FOURCC('R','V','2','4'),
65     0
66 };
67
68 static int CreateChain( filter_chain_t *p_chain, es_format_t *p_fmt_mid )
69 {
70     filter_t *p_filter1;
71     if( !( p_filter1 =
72            filter_chain_AppendFilter( p_chain, NULL, NULL, NULL, p_fmt_mid )) )
73         return VLC_EGENERIC;
74     if( !filter_chain_AppendFilter( p_chain, NULL, NULL, p_fmt_mid, NULL ) )
75     {
76         filter_chain_DeleteFilter( p_chain, p_filter1 );
77         return VLC_EGENERIC;
78     }
79     return VLC_SUCCESS;
80 }
81
82 static int AllocInit( filter_t *p_filter, void *p_data )
83 {
84     /* Not sure about all of this ... it should work */
85     p_filter->pf_vout_buffer_new = ((filter_t*)p_data)->pf_vout_buffer_new;
86     p_filter->pf_vout_buffer_del = ((filter_t*)p_data)->pf_vout_buffer_del;
87     p_filter->p_owner = ((filter_t*)p_data)->p_owner;
88     return VLC_SUCCESS;
89 }
90
91 /*****************************************************************************
92  * Activate: allocate a chroma function
93  *****************************************************************************
94  * This function allocates and initializes a chroma function
95  *****************************************************************************/
96 static int Activate( vlc_object_t *p_this )
97 {
98     filter_t *p_filter = (filter_t *)p_this;
99     static int hack = 0; /* FIXME */
100     es_format_t fmt_mid;
101
102     if( p_filter->fmt_in.video.i_chroma == p_filter->fmt_out.video.i_chroma )
103         return VLC_EGENERIC;
104
105     hack++;
106     if( hack >= MAX_FILTERS )
107     {
108         msg_Err( p_this, "Preventing chain filter reccursion (already %d long)",
109                  hack );
110         return VLC_EGENERIC;
111     }
112
113     filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
114     if( !p_sys )
115     {
116         hack--;
117         return VLC_ENOMEM;
118     }
119     memset( p_sys, 0, sizeof( filter_sys_t ) );
120     p_filter->p_sys = p_sys;
121
122     p_sys->p_chain = filter_chain_New( p_filter, "video filter2", false, AllocInit, NULL, p_filter );
123     if( !p_sys->p_chain )
124     {
125         free( p_sys );
126         return VLC_EGENERIC;
127     }
128     filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &p_filter->fmt_out );
129
130     if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
131         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height ||
132         p_filter->fmt_in.video.i_visible_width != p_filter->fmt_out.video.i_visible_width ||
133         p_filter->fmt_in.video.i_visible_height != p_filter->fmt_out.video.i_visible_height )
134     {
135         /* Lets try resizing and then doing the chroma conversion */
136         es_format_Copy( &fmt_mid, &p_filter->fmt_out );
137         fmt_mid.video.i_chroma = p_filter->fmt_out.video.i_chroma;
138         if( CreateChain( p_sys->p_chain, &fmt_mid ) == VLC_SUCCESS )
139         {
140             es_format_Clean( &fmt_mid );
141             p_filter->pf_video_filter = Chain;
142             return VLC_SUCCESS;
143         }
144
145         /* Lets try it the other way arround (chroma and then resize) */
146         es_format_Clean( &fmt_mid );
147         es_format_Copy( &fmt_mid, &p_filter->fmt_in );
148         fmt_mid.video.i_chroma = p_filter->fmt_out.video.i_chroma;
149         if( CreateChain( p_sys->p_chain, &fmt_mid ) == VLC_SUCCESS )
150         {
151             es_format_Clean( &fmt_mid );
152             p_filter->pf_video_filter = Chain;
153             return VLC_SUCCESS;
154         }
155     }
156     else
157     {
158         /* Lets try doing a chroma chain */
159         int i;
160         es_format_Copy( &fmt_mid, &p_filter->fmt_in );
161         for( i = 0; pi_allowed_chromas[i]; i++ )
162         {
163             fmt_mid.video.i_chroma = pi_allowed_chromas[i];
164             if( CreateChain( p_sys->p_chain, &fmt_mid ) == VLC_SUCCESS )
165             {
166                 es_format_Clean( &fmt_mid );
167                 p_filter->pf_video_filter = Chain;
168                 return VLC_SUCCESS;
169             }
170         }
171     }
172
173     /* Hum ... looks like this really isn't going to work. Too bad. */
174     es_format_Clean( &fmt_mid );
175     filter_chain_Delete( p_sys->p_chain );
176     free( p_sys );
177     hack--;
178     return VLC_EGENERIC;
179 }
180
181 static void Destroy( vlc_object_t *p_this )
182 {
183     filter_t *p_filter = (filter_t *)p_this;
184     filter_chain_Delete( p_filter->p_sys->p_chain );
185     free( p_filter->p_sys );
186 }
187
188 /*****************************************************************************
189  * Chain
190  *****************************************************************************/
191 static picture_t *Chain( filter_t *p_filter, picture_t *p_pic )
192 {
193     return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
194 }