1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: deinterlace.c,v 1.2 2002/01/02 14:37:42 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <videolan/vlc.h>
34 #include "video_output.h"
36 #include "filter_common.h"
38 #define DEINTERLACE_MODE_BOB 1
39 #define DEINTERLACE_MODE_BLEND 2
41 /*****************************************************************************
42 * Capabilities defined in the other files.
43 *****************************************************************************/
44 static void vout_getfunctions( function_list_t * p_function_list );
46 static void *memblend( void *, const void *, const void *, size_t );
48 /*****************************************************************************
49 * Build configuration tree.
50 *****************************************************************************/
55 SET_DESCRIPTION( "deinterlacing module" )
56 /* Capability score set to 0 because we don't want to be spawned
57 * as a video output unless explicitly requested to */
58 ADD_CAPABILITY( VOUT, 0 )
59 ADD_SHORTCUT( "deinterlace" )
63 vout_getfunctions( &p_module->p_functions->vout );
66 MODULE_DEACTIVATE_START
67 MODULE_DEACTIVATE_STOP
69 /*****************************************************************************
70 * vout_sys_t: Deinterlace video output method descriptor
71 *****************************************************************************
72 * This structure is part of the video output thread descriptor.
73 * It describes the Deinterlace specific properties of an output thread.
74 *****************************************************************************/
75 typedef struct vout_sys_s
78 struct vout_thread_s *p_vout;
82 /*****************************************************************************
84 *****************************************************************************/
85 static int vout_Probe ( probedata_t *p_data );
86 static int vout_Create ( struct vout_thread_s * );
87 static int vout_Init ( struct vout_thread_s * );
88 static void vout_End ( struct vout_thread_s * );
89 static void vout_Destroy ( struct vout_thread_s * );
90 static int vout_Manage ( struct vout_thread_s * );
91 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
93 /*****************************************************************************
94 * Functions exported as capabilities. They are declared as static so that
95 * we don't pollute the namespace too much.
96 *****************************************************************************/
97 static void vout_getfunctions( function_list_t * p_function_list )
99 p_function_list->pf_probe = vout_Probe;
100 p_function_list->functions.vout.pf_create = vout_Create;
101 p_function_list->functions.vout.pf_init = vout_Init;
102 p_function_list->functions.vout.pf_end = vout_End;
103 p_function_list->functions.vout.pf_destroy = vout_Destroy;
104 p_function_list->functions.vout.pf_manage = vout_Manage;
105 p_function_list->functions.vout.pf_display = vout_Display;
106 p_function_list->functions.vout.pf_setpalette = NULL;
109 /*****************************************************************************
110 * intf_Probe: return a score
111 *****************************************************************************/
112 static int vout_Probe( probedata_t *p_data )
117 /*****************************************************************************
118 * vout_Create: allocates Deinterlace video thread output method
119 *****************************************************************************
120 * This function allocates and initializes a Deinterlace vout method.
121 *****************************************************************************/
122 static int vout_Create( vout_thread_t *p_vout )
126 /* Allocate structure */
127 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
128 if( p_vout->p_sys == NULL )
130 intf_ErrMsg("error: %s", strerror(ENOMEM) );
134 /* Look what method was requested */
135 psz_method = main_GetPszVariable( VOUT_FILTER_VAR, "" );
137 while( *psz_method && *psz_method != ':' )
142 if( !strcmp( psz_method, ":bob" ) )
144 p_vout->p_sys->i_mode = DEINTERLACE_MODE_BOB;
146 else if( !strcmp( psz_method, ":blend" ) )
148 p_vout->p_sys->i_mode = DEINTERLACE_MODE_BLEND;
152 intf_ErrMsg( "filter error: no valid deinterlace mode provided, "
153 "using deinterlace:bob" );
154 p_vout->p_sys->i_mode = DEINTERLACE_MODE_BOB;
160 /*****************************************************************************
161 * vout_Init: initialize Deinterlace video thread output method
162 *****************************************************************************/
163 static int vout_Init( vout_thread_t *p_vout )
169 I_OUTPUTPICTURES = 0;
171 /* Initialize the output structure, full of directbuffers since we want
172 * the decoder to output directly to our structures. */
173 switch( p_vout->render.i_chroma )
179 p_vout->output.i_chroma = p_vout->render.i_chroma;
180 p_vout->output.i_width = p_vout->render.i_width;
181 p_vout->output.i_height = p_vout->render.i_height;
182 p_vout->output.i_aspect = p_vout->render.i_aspect;
186 return( 0 ); /* unknown chroma */
190 /* Try to open the real video output, with half the height our images */
191 psz_filter = main_GetPszVariable( VOUT_FILTER_VAR, "" );
192 main_PutPszVariable( VOUT_FILTER_VAR, "" );
194 intf_WarnMsg( 1, "filter: spawning the real video output" );
196 switch( p_vout->render.i_chroma )
201 switch( p_vout->p_sys->i_mode )
203 case DEINTERLACE_MODE_BOB:
204 p_vout->p_sys->p_vout =
205 vout_CreateThread( NULL,
206 p_vout->output.i_width, p_vout->output.i_height / 2,
207 p_vout->output.i_chroma, p_vout->output.i_aspect );
210 case DEINTERLACE_MODE_BLEND:
211 p_vout->p_sys->p_vout =
212 vout_CreateThread( NULL,
213 p_vout->output.i_width, p_vout->output.i_height,
214 p_vout->output.i_chroma, p_vout->output.i_aspect );
220 p_vout->p_sys->p_vout =
221 vout_CreateThread( NULL,
222 p_vout->output.i_width, p_vout->output.i_height,
223 FOURCC_I420, p_vout->output.i_aspect );
230 /* Everything failed */
231 if( p_vout->p_sys->p_vout == NULL )
233 intf_ErrMsg( "filter error: can't open vout, aborting" );
238 main_PutPszVariable( VOUT_FILTER_VAR, psz_filter );
240 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
245 /*****************************************************************************
246 * vout_End: terminate Deinterlace video thread output method
247 *****************************************************************************/
248 static void vout_End( vout_thread_t *p_vout )
252 /* Free the fake output buffers we allocated */
253 for( i_index = I_OUTPUTPICTURES ; i_index ; )
256 free( PP_OUTPUTPICTURE[ i_index ]->planes[ 0 ].p_data );
260 /*****************************************************************************
261 * vout_Destroy: destroy Deinterlace video thread output method
262 *****************************************************************************
263 * Terminate an output method created by DeinterlaceCreateOutputMethod
264 *****************************************************************************/
265 static void vout_Destroy( vout_thread_t *p_vout )
267 vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
269 free( p_vout->p_sys );
272 /*****************************************************************************
273 * vout_Manage: handle Deinterlace events
274 *****************************************************************************
275 * This function should be called regularly by video output thread. It manages
276 * console events. It returns a non null value on error.
277 *****************************************************************************/
278 static int vout_Manage( vout_thread_t *p_vout )
283 /*****************************************************************************
284 * vout_Display: displays previously rendered output
285 *****************************************************************************
286 * This function send the currently rendered image to Deinterlace image,
287 * waits until it is displayed and switch the two rendering buffers, preparing
289 *****************************************************************************/
290 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
293 int i_index, i_field;
295 for( i_field = 0 ; i_field < 2 ; i_field++ )
297 /* Get a structure from the video_output. */
298 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout,
302 if( p_vout->b_die || p_vout->b_error )
306 msleep( VOUT_OUTMEM_SLEEP );
309 /* XXX: completely arbitrary values ! */
310 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic,
311 mdate() + (mtime_t)(50000 + i_field * 20000) );
313 /* Copy image and skip lines */
314 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
316 pixel_data_t *p_in, *p_out_end, *p_out;
319 p_in = p_pic->planes[ i_index ].p_data
320 + i_field * p_pic->planes[ i_index ].i_line_bytes;
322 p_out = p_outpic->planes[ i_index ].p_data;
323 p_out_end = p_out + p_outpic->planes[ i_index ].i_bytes;
325 switch( p_vout->render.i_chroma )
331 switch( p_vout->p_sys->i_mode )
333 case DEINTERLACE_MODE_BOB:
334 for( ; p_out < p_out_end ; )
336 FAST_MEMCPY( p_out, p_in,
337 p_pic->planes[ i_index ].i_line_bytes );
339 p_out += p_pic->planes[ i_index ].i_line_bytes;
340 p_in += 2 * p_pic->planes[ i_index ].i_line_bytes;
344 case DEINTERLACE_MODE_BLEND:
345 if( i_index != Y_PLANE )
347 for( ; p_out < p_out_end ; )
349 FAST_MEMCPY( p_out, p_in,
350 p_pic->planes[ i_index ].i_line_bytes );
352 p_out += p_pic->planes[ i_index ].i_line_bytes;
354 FAST_MEMCPY( p_out, p_in,
355 p_pic->planes[ i_index ].i_line_bytes );
357 p_out += p_pic->planes[ i_index ].i_line_bytes;
358 p_in += 2 * p_pic->planes[ i_index ].i_line_bytes;
365 FAST_MEMCPY( p_out, p_in,
366 p_pic->planes[ i_index ].i_line_bytes );
367 p_in += 2 * p_pic->planes[ i_index ].i_line_bytes;
368 p_out += p_pic->planes[ i_index ].i_line_bytes;
371 for( ; p_out < p_out_end ; )
373 FAST_MEMCPY( p_out, p_in,
374 p_pic->planes[ i_index ].i_line_bytes );
376 p_out += p_pic->planes[ i_index ].i_line_bytes;
378 memblend( p_out, p_in, p_in + 2 * p_pic->planes[ i_index ].i_line_bytes, p_pic->planes[ i_index ].i_line_bytes );
380 p_in += 2 * p_pic->planes[ i_index ].i_line_bytes;
381 p_out += p_pic->planes[ i_index ].i_line_bytes;
389 i_increment = 2 * p_pic->planes[ i_index ].i_line_bytes;
391 if( i_index == Y_PLANE )
393 for( ; p_out < p_out_end ; )
395 FAST_MEMCPY( p_out, p_in,
396 p_pic->planes[ i_index ].i_line_bytes );
397 p_out += p_pic->planes[ i_index ].i_line_bytes;
398 FAST_MEMCPY( p_out, p_in,
399 p_pic->planes[ i_index ].i_line_bytes );
400 p_out += p_pic->planes[ i_index ].i_line_bytes;
406 for( ; p_out < p_out_end ; )
408 FAST_MEMCPY( p_out, p_in,
409 p_pic->planes[ i_index ].i_line_bytes );
410 p_out += p_pic->planes[ i_index ].i_line_bytes;
421 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
425 static void *memblend( void *p_dest, const void *p_s1,
426 const void *p_s2, size_t i_bytes )
428 u8* p_end = (u8*)p_dest + i_bytes - 8;
430 while( (u8*)p_dest < p_end )
432 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
433 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
434 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
435 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
436 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
437 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
438 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
439 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
444 while( (u8*)p_dest < p_end )
446 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;