]> git.sesse.net Git - vlc/blob - modules/video_chroma/chain.c
Change the chain module so it allows chaining more than 2 chroma modules (you'll...
[vlc] / modules / video_chroma / chain.c
1 /*****************************************************************************
2  * chain.c : chain multiple chroma modules as a last resort solution
3  *****************************************************************************
4  * Copyright (C) 2007 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 #include <vlc/vlc.h>
29 #include <vlc_vout.h>
30
31 /*****************************************************************************
32  * Local and extern prototypes.
33  *****************************************************************************/
34 static int  Activate ( vlc_object_t * );
35 static void Destroy  ( vlc_object_t * );
36 static void Chain    ( vout_thread_t *, picture_t *, picture_t * );
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 vlc_module_begin();
42     set_description( _("Chroma conversions using a chain of chroma conversion modules") );
43     set_capability( "chroma", 1 );
44     set_callbacks( Activate, Destroy );
45 vlc_module_end();
46
47 #define MAX_CHROMAS 2
48
49 struct chroma_sys_t
50 {
51     vlc_fourcc_t i_chroma;
52
53     vout_chroma_t chroma1;
54     vout_chroma_t chroma2;
55
56     picture_t *p_tmp;
57 };
58
59 static const vlc_fourcc_t pi_allowed_chromas[] = {
60     VLC_FOURCC('I','4','2','0'),
61     VLC_FOURCC('I','4','2','2'),
62     0
63 };
64
65 /*****************************************************************************
66  * Activate: allocate a chroma function
67  *****************************************************************************
68  * This function allocates and initializes a chroma function
69  *****************************************************************************/
70 static int Activate( vlc_object_t *p_this )
71 {
72     static int hack = 1;
73     vout_thread_t *p_vout = (vout_thread_t *)p_this;
74
75     hack++;
76     if( hack > MAX_CHROMAS )
77     {
78         hack--;
79         msg_Err( p_this, "Preventing chain chroma reccursion (already %d long)",
80                  hack );
81         return VLC_EGENERIC;
82     }
83
84     chroma_sys_t *p_sys = (chroma_sys_t *)malloc( sizeof( chroma_sys_t ) );
85     if( !p_sys )
86     {
87         hack--;
88         return VLC_ENOMEM;
89     }
90     memset( p_sys, 0, sizeof( chroma_sys_t ) );
91
92     int i;
93     vlc_fourcc_t i_output_chroma = p_vout->output.i_chroma;
94     vlc_fourcc_t i_render_chroma = p_vout->render.i_chroma;
95
96     for( i = 0; pi_allowed_chromas[i]; i++ )
97     {
98         msg_Warn( p_vout, "Trying %4s as a chroma chain",
99                   (const char *)&pi_allowed_chromas[i] );
100         p_vout->output.i_chroma = pi_allowed_chromas[i];
101         p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
102         p_vout->output.i_chroma = i_output_chroma;
103
104         if( !p_vout->chroma.p_module )
105             continue;
106
107         p_sys->chroma1 = p_vout->chroma;
108         memset( &p_vout->chroma, 0, sizeof( vout_chroma_t ) );
109
110         p_vout->render.i_chroma = pi_allowed_chromas[i];
111         p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
112         p_vout->render.i_chroma = i_render_chroma;
113
114         if( !p_vout->chroma.p_module )
115         {
116             p_vout->chroma = p_sys->chroma1;
117             module_Unneed( p_vout, p_vout->chroma.p_module );
118             continue;
119         }
120
121         p_sys->chroma2 = p_vout->chroma;
122         memset( &p_vout->chroma, 0, sizeof( vout_chroma_t ) );
123
124         p_sys->i_chroma = pi_allowed_chromas[i];
125         p_vout->chroma.pf_convert = Chain;
126         p_vout->chroma.p_sys = p_sys;
127         hack--;
128         printf("Init: p_sys->p_tmp= %p\n", p_sys->p_tmp );
129         return VLC_SUCCESS;
130     }
131
132     free( p_sys );
133     hack--;
134     return VLC_EGENERIC;
135 }
136
137 static void Destroy( vlc_object_t *p_this )
138 {
139     vout_thread_t *p_vout = (vout_thread_t *)p_this;
140     vout_chroma_t chroma = p_vout->chroma;
141
142
143     p_vout->chroma = chroma.p_sys->chroma1;
144     module_Unneed( p_vout, p_vout->chroma.p_module );
145     p_vout->chroma = chroma.p_sys->chroma2;
146     module_Unneed( p_vout, p_vout->chroma.p_module );
147     p_vout->chroma = chroma;
148
149     if( chroma.p_sys->p_tmp )
150     {
151         free( chroma.p_sys->p_tmp->p_data_orig );
152         free( chroma.p_sys->p_tmp );
153     }
154     free( chroma.p_sys );
155     chroma.p_sys = NULL;
156 }
157
158 /*****************************************************************************
159  * Chain
160  *****************************************************************************/
161 static void Chain( vout_thread_t *p_vout, picture_t *p_source,
162                    picture_t *p_dest )
163 {
164     chroma_sys_t *p_sys = p_vout->chroma.p_sys;
165
166     if( !p_sys->p_tmp )
167     {
168         picture_t *p_tmp = malloc( sizeof( picture_t ) );
169         vout_AllocatePicture( VLC_OBJECT( p_vout ), p_tmp,
170                               p_sys->i_chroma,
171                               p_source->p_heap->i_width,
172                               p_source->p_heap->i_height,
173                               p_source->p_heap->i_aspect );
174         if( !p_tmp )
175             return;
176         p_sys->p_tmp = p_tmp;
177         p_tmp->pf_release = NULL;
178         p_tmp->i_status = RESERVED_PICTURE;
179         p_tmp->p_sys = NULL;
180     }
181
182     vout_chroma_t chroma = p_vout->chroma;
183     p_vout->chroma = p_sys->chroma1;
184     p_sys->chroma1.pf_convert( p_vout, p_source, p_sys->p_tmp );
185     p_vout->chroma = p_sys->chroma2;
186     p_sys->chroma2.pf_convert( p_vout, p_sys->p_tmp, p_dest );
187     p_vout->chroma = chroma;
188 }