]> git.sesse.net Git - vlc/blob - modules/video_chroma/chain.c
Three new chroma converters:
[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 = 0;
73     vout_thread_t *p_vout = (vout_thread_t *)p_this;
74
75     if( hack )
76     {
77         msg_Err( p_this, "Preventing chain chroma reccursion" );
78         return VLC_EGENERIC;
79     }
80
81     hack = 1;
82
83     chroma_sys_t *p_sys = (chroma_sys_t *)malloc( sizeof( chroma_sys_t ) );
84     if( !p_sys )
85     {
86         hack = 0;
87         return VLC_ENOMEM;
88     }
89     memset( p_sys, 0, sizeof( chroma_sys_t ) );
90
91     int i;
92     vlc_fourcc_t i_output_chroma = p_vout->output.i_chroma;
93     vlc_fourcc_t i_render_chroma = p_vout->render.i_chroma;
94
95     for( i = 0; pi_allowed_chromas[i]; i++ )
96     {
97         msg_Warn( p_vout, "Trying %4s as a chroma chain",
98                   (const char *)&pi_allowed_chromas[i] );
99         p_vout->output.i_chroma = pi_allowed_chromas[i];
100         p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
101         p_vout->output.i_chroma = i_output_chroma;
102
103         if( !p_vout->chroma.p_module )
104             continue;
105
106         p_sys->chroma1 = p_vout->chroma;
107         memset( &p_vout->chroma, 0, sizeof( vout_chroma_t ) );
108
109         p_vout->render.i_chroma = pi_allowed_chromas[i];
110         p_vout->chroma.p_module = module_Need( p_vout, "chroma", NULL, 0 );
111         p_vout->render.i_chroma = i_render_chroma;
112
113         if( !p_vout->chroma.p_module )
114         {
115             p_vout->chroma = p_sys->chroma1;
116             module_Unneed( p_vout, p_vout->chroma.p_module );
117             continue;
118         }
119
120         p_sys->chroma2 = p_vout->chroma;
121         memset( &p_vout->chroma, 0, sizeof( vout_chroma_t ) );
122
123         p_sys->i_chroma = pi_allowed_chromas[i];
124         p_vout->chroma.pf_convert = Chain;
125         p_vout->chroma.p_sys = p_sys;
126         hack = 0;
127         return VLC_SUCCESS;
128     }
129
130     free( p_sys );
131     hack = 0;
132     return VLC_EGENERIC;
133 }
134
135 static void Destroy( vlc_object_t *p_this )
136 {
137     vout_thread_t *p_vout = (vout_thread_t *)p_this;
138     vout_chroma_t chroma = p_vout->chroma;
139
140
141     p_vout->chroma = chroma.p_sys->chroma1;
142     module_Unneed( p_vout, p_vout->chroma.p_module );
143     p_vout->chroma = chroma.p_sys->chroma2;
144     module_Unneed( p_vout, p_vout->chroma.p_module );
145     p_vout->chroma = chroma;
146
147     if( chroma.p_sys->p_tmp )
148     {
149         free( chroma.p_sys->p_tmp->p_data_orig );
150         free( chroma.p_sys->p_tmp );
151     }
152     free( chroma.p_sys );
153 }
154
155 /*****************************************************************************
156  * Chain
157  *****************************************************************************/
158 static void Chain( vout_thread_t *p_vout, picture_t *p_source,
159                    picture_t *p_dest )
160 {
161     chroma_sys_t *p_sys = p_vout->chroma.p_sys;
162
163     if( !p_sys->p_tmp )
164     {
165         picture_t *p_tmp = malloc( sizeof( picture_t ) );
166         vout_AllocatePicture( VLC_OBJECT( p_vout ), p_tmp,
167                               p_sys->i_chroma,
168                               p_source->p_heap->i_width,
169                               p_source->p_heap->i_height,
170                               p_source->p_heap->i_aspect );
171         if( !p_tmp )
172             return;
173         p_sys->p_tmp = p_tmp;
174         p_tmp->pf_release = NULL;
175         p_tmp->i_status = RESERVED_PICTURE;
176         p_tmp->p_sys = NULL;
177     }
178
179     p_sys->chroma1.pf_convert( p_vout, p_source, p_sys->p_tmp );
180     p_sys->chroma2.pf_convert( p_vout, p_sys->p_tmp, p_dest );
181 }