1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
29 #include <vlc_common.h>
32 #include "interlacing.h"
34 /*****************************************************************************
36 *****************************************************************************/
38 * You can use the non vout filter if and only if the video properties stay the
39 * same (width/height/chroma/fps), at least for now.
41 static const char *deinterlace_modes[] = {
53 static bool DeinterlaceIsModeValid(const char *mode)
55 for (unsigned i = 0; deinterlace_modes[i]; i++) {
56 if( !strcmp(deinterlace_modes[i], mode))
62 static char *FilterFind( char *psz_filter_base, const char *psz_module )
64 const size_t i_module = strlen( psz_module );
65 const char *psz_filter = psz_filter_base;
67 if( !psz_filter || i_module <= 0 )
72 char *psz_find = strstr( psz_filter, psz_module );
75 if( psz_find[i_module] == '\0' || psz_find[i_module] == ':' )
77 psz_filter = &psz_find[i_module];
81 static bool DeinterlaceIsPresent( vout_thread_t *p_vout )
83 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
85 bool b_found = FilterFind( psz_filter, "deinterlace" ) != NULL;
92 static void DeinterlaceRemove( vout_thread_t *p_vout )
94 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
96 char *psz = FilterFind( psz_filter, "deinterlace" );
104 strcpy( &psz[0], &psz[strlen("deinterlace")] );
106 strcpy( &psz[0], &psz[1] );
108 var_SetString( p_vout, "video-filter", psz_filter );
111 static void DeinterlaceAdd( vout_thread_t *p_vout )
113 char *psz_filter = var_GetNonEmptyString( p_vout, "video-filter" );
115 if( FilterFind( psz_filter, "deinterlace" ) )
124 char *psz_tmp = psz_filter;
125 if( asprintf( &psz_filter, "%s:%s", psz_tmp, "deinterlace" ) < 0 )
126 psz_filter = psz_tmp;
132 psz_filter = strdup( "deinterlace" );
137 var_SetString( p_vout, "video-filter", psz_filter );
142 static int DeinterlaceCallback( vlc_object_t *p_this, char const *psz_cmd,
143 vlc_value_t oldval, vlc_value_t newval, void *p_data )
145 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(p_data);
146 vout_thread_t *p_vout = (vout_thread_t *)p_this;
149 const int i_deinterlace = var_GetInteger( p_this, "deinterlace" );
150 char *psz_mode = var_GetString( p_this, "deinterlace-mode" );
151 const bool is_needed = var_GetBool( p_this, "deinterlace-needed" );
152 if( !psz_mode || !DeinterlaceIsModeValid(psz_mode) )
156 char *psz_old = var_CreateGetString( p_vout, "sout-deinterlace-mode" );
157 var_SetString( p_vout, "sout-deinterlace-mode", psz_mode );
159 msg_Dbg( p_vout, "deinterlace %d, mode %s, is_needed %d", i_deinterlace, psz_mode, is_needed );
160 if( i_deinterlace == 0 || ( i_deinterlace == -1 && !is_needed ) )
162 DeinterlaceRemove( p_vout );
164 else if( !DeinterlaceIsPresent( p_vout ) )
166 DeinterlaceAdd( p_vout );
168 else if( psz_old && strcmp( psz_old, psz_mode ) )
170 var_TriggerCallback( p_vout, "video-filter" );
179 void vout_InitInterlacingSupport( vout_thread_t *p_vout, bool is_interlaced )
181 vlc_value_t val, text;
183 msg_Dbg( p_vout, "Deinterlacing available" );
185 /* Create the configuration variables */
187 var_Create( p_vout, "deinterlace", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT | VLC_VAR_HASCHOICE );
188 int i_deinterlace = var_GetInteger( p_vout, "deinterlace" );
189 i_deinterlace = __MAX( __MIN( i_deinterlace, 1 ), -1 );
191 text.psz_string = _("Deinterlace");
192 var_Change( p_vout, "deinterlace", VLC_VAR_SETTEXT, &text, NULL );
194 const module_config_t *p_optd = config_FindConfig( VLC_OBJECT(p_vout), "deinterlace" );
195 var_Change( p_vout, "deinterlace", VLC_VAR_CLEARCHOICES, NULL, NULL );
196 for( int i = 0; p_optd && i < p_optd->i_list; i++ )
198 val.i_int = p_optd->pi_list[i];
199 text.psz_string = (char*)vlc_gettext(p_optd->ppsz_list_text[i]);
200 var_Change( p_vout, "deinterlace", VLC_VAR_ADDCHOICE, &val, &text );
202 var_AddCallback( p_vout, "deinterlace", DeinterlaceCallback, NULL );
204 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_HASCHOICE );
205 char *psz_deinterlace = var_GetNonEmptyString( p_vout, "deinterlace-mode" );
207 text.psz_string = _("Deinterlace mode");
208 var_Change( p_vout, "deinterlace-mode", VLC_VAR_SETTEXT, &text, NULL );
210 const module_config_t *p_optm = config_FindConfig( VLC_OBJECT(p_vout), "deinterlace-mode" );
211 var_Change( p_vout, "deinterlace-mode", VLC_VAR_CLEARCHOICES, NULL, NULL );
212 for( int i = 0; p_optm && i < p_optm->i_list; i++ )
214 if( !DeinterlaceIsModeValid( p_optm->ppsz_list[i] ) )
217 val.psz_string = p_optm->ppsz_list[i];
218 text.psz_string = (char*)vlc_gettext(p_optm->ppsz_list_text[i]);
219 var_Change( p_vout, "deinterlace-mode", VLC_VAR_ADDCHOICE, &val, &text );
221 var_AddCallback( p_vout, "deinterlace-mode", DeinterlaceCallback, NULL );
223 var_Create( p_vout, "deinterlace-needed", VLC_VAR_BOOL );
224 var_AddCallback( p_vout, "deinterlace-needed", DeinterlaceCallback, NULL );
226 /* Override the initial value from filters if present */
227 char *psz_filter_mode = NULL;
228 if( DeinterlaceIsPresent( p_vout ) )
229 psz_filter_mode = var_CreateGetNonEmptyString( p_vout, "sout-deinterlace-mode" );
230 if( psz_filter_mode )
233 free( psz_deinterlace );
234 psz_deinterlace = psz_filter_mode;
238 val.psz_string = psz_deinterlace ? psz_deinterlace : p_optm->orig.psz;
239 var_Change( p_vout, "deinterlace-mode", VLC_VAR_SETVALUE, &val, NULL );
240 val.b_bool = is_interlaced;
241 var_Change( p_vout, "deinterlace-needed", VLC_VAR_SETVALUE, &val, NULL );
243 var_SetInteger( p_vout, "deinterlace", i_deinterlace );
244 free( psz_deinterlace );
247 void vout_SetInterlacingState( vout_thread_t *p_vout, vout_interlacing_support_t *state, bool is_interlaced )
249 /* Wait 30s before quiting interlacing mode */
250 const int interlacing_change = (!!is_interlaced) - (!!state->is_interlaced);
251 if ((interlacing_change == 1) ||
252 (interlacing_change == -1 && state->date + 30000000 < mdate())) {
254 msg_Dbg( p_vout, "Detected %s video",
255 is_interlaced ? "interlaced" : "progressive" );
256 var_SetBool( p_vout, "deinterlace-needed", is_interlaced );
258 state->is_interlaced = is_interlaced;
261 state->date = mdate();