1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
7 * Author: Sam 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() */
38 #include "filter_common.h"
40 #define DEINTERLACE_DISCARD 1
41 #define DEINTERLACE_MEAN 2
42 #define DEINTERLACE_BLEND 3
43 #define DEINTERLACE_BOB 4
44 #define DEINTERLACE_LINEAR 5
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Create ( vlc_object_t * );
50 static void Destroy ( vlc_object_t * );
52 static int Init ( vout_thread_t * );
53 static void End ( vout_thread_t * );
54 static void Render ( vout_thread_t *, picture_t * );
56 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
57 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
58 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
59 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
60 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
62 static void MergeGeneric ( void *, const void *, const void *, size_t );
63 #if defined(CAN_COMPILE_C_ALTIVEC)
64 static void MergeAltivec ( void *, const void *, const void *, size_t );
66 #if defined(CAN_COMPILE_MMXEXT)
67 static void MergeMMX ( void *, const void *, const void *, size_t );
69 #if defined(CAN_COMPILE_SSE)
70 static void MergeSSE2 ( void *, const void *, const void *, size_t );
72 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
73 static void EndMMX ( void );
76 static int SendEvents ( vlc_object_t *, char const *,
77 vlc_value_t, vlc_value_t, void * );
79 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
80 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
82 /*****************************************************************************
84 *****************************************************************************/
85 static int FilterCallback ( vlc_object_t *, char const *,
86 vlc_value_t, vlc_value_t, void * );
88 /*****************************************************************************
90 *****************************************************************************/
91 #define MODE_TEXT N_("Deinterlace mode")
92 #define MODE_LONGTEXT N_("You can choose the default deinterlace mode")
94 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear" };
95 static char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
96 N_("Bob"), N_("Linear") };
99 set_description( _("Deinterlacing video filter") );
100 set_shortname( N_("Deinterlace" ));
101 set_capability( "video filter", 0 );
102 set_category( CAT_VIDEO );
103 set_subcategory( SUBCAT_VIDEO_VFILTER );
105 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
106 MODE_LONGTEXT, VLC_FALSE );
107 change_string_list( mode_list, mode_list_text, 0 );
109 add_shortcut( "deinterlace" );
110 set_callbacks( Create, Destroy );
113 /*****************************************************************************
114 * vout_sys_t: Deinterlace video output method descriptor
115 *****************************************************************************
116 * This structure is part of the video output thread descriptor.
117 * It describes the Deinterlace specific properties of an output thread.
118 *****************************************************************************/
121 int i_mode; /* Deinterlace mode */
122 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
127 vout_thread_t *p_vout;
129 vlc_mutex_t filter_lock;
131 void (*pf_merge) ( void *, const void *, const void *, size_t );
132 void (*pf_end_merge) ( void );
135 /*****************************************************************************
136 * Control: control facility for the vout (forwards to child vout)
137 *****************************************************************************/
138 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
140 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
143 /*****************************************************************************
144 * Create: allocates Deinterlace video thread output method
145 *****************************************************************************
146 * This function allocates and initializes a Deinterlace vout method.
147 *****************************************************************************/
148 static int Create( vlc_object_t *p_this )
150 vout_thread_t *p_vout = (vout_thread_t *)p_this;
153 /* Allocate structure */
154 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
155 if( p_vout->p_sys == NULL )
157 msg_Err( p_vout, "out of memory" );
161 p_vout->pf_init = Init;
162 p_vout->pf_end = End;
163 p_vout->pf_manage = NULL;
164 p_vout->pf_render = Render;
165 p_vout->pf_display = NULL;
166 p_vout->pf_control = Control;
168 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
169 p_vout->p_sys->b_double_rate = 0;
170 p_vout->p_sys->last_date = 0;
171 p_vout->p_sys->p_vout = 0;
172 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
174 #if defined(CAN_COMPILE_C_ALTIVEC)
175 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
177 p_vout->p_sys->pf_merge = MergeAltivec;
178 p_vout->p_sys->pf_end_merge = NULL;
182 #if defined(CAN_COMPILE_SSE)
183 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2 )
185 p_vout->p_sys->pf_merge = MergeSSE2;
186 p_vout->p_sys->pf_end_merge = EndMMX;
190 #if defined(CAN_COMPILE_MMXEXT)
191 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
193 p_vout->p_sys->pf_merge = MergeMMX;
194 p_vout->p_sys->pf_end_merge = EndMMX;
199 p_vout->p_sys->pf_merge = MergeGeneric;
200 p_vout->p_sys->pf_end_merge = NULL;
203 /* Look what method was requested */
204 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
205 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
207 if( val.psz_string == NULL )
209 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
210 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
212 val.psz_string = strdup( "discard" );
215 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
217 SetFilterMethod( p_vout, val.psz_string );
219 free( val.psz_string );
221 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
226 /*****************************************************************************
227 * SetFilterMethod: setup the deinterlace method to use.
228 *****************************************************************************/
229 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
231 if( !strcmp( psz_method, "discard" ) )
233 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
234 p_vout->p_sys->b_double_rate = 0;
236 else if( !strcmp( psz_method, "mean" ) )
238 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
239 p_vout->p_sys->b_double_rate = 0;
241 else if( !strcmp( psz_method, "blend" )
242 || !strcmp( psz_method, "average" )
243 || !strcmp( psz_method, "combine-fields" ) )
245 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
246 p_vout->p_sys->b_double_rate = 0;
248 else if( !strcmp( psz_method, "bob" )
249 || !strcmp( psz_method, "progressive-scan" ) )
251 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
252 p_vout->p_sys->b_double_rate = 1;
254 else if( !strcmp( psz_method, "linear" ) )
256 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
257 p_vout->p_sys->b_double_rate = 1;
261 msg_Err( p_vout, "no valid deinterlace mode provided, "
262 "using \"discard\"" );
265 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
268 /*****************************************************************************
269 * Init: initialize Deinterlace video thread output method
270 *****************************************************************************/
271 static int Init( vout_thread_t *p_vout )
276 I_OUTPUTPICTURES = 0;
278 /* Initialize the output structure, full of directbuffers since we want
279 * the decoder to output directly to our structures. */
280 switch( p_vout->render.i_chroma )
282 case VLC_FOURCC('I','4','2','0'):
283 case VLC_FOURCC('I','Y','U','V'):
284 case VLC_FOURCC('Y','V','1','2'):
285 case VLC_FOURCC('I','4','2','2'):
286 p_vout->output.i_chroma = p_vout->render.i_chroma;
287 p_vout->output.i_width = p_vout->render.i_width;
288 p_vout->output.i_height = p_vout->render.i_height;
289 p_vout->output.i_aspect = p_vout->render.i_aspect;
293 return VLC_EGENERIC; /* unknown chroma */
297 /* Try to open the real video output */
298 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
300 if( p_vout->p_sys->p_vout == NULL )
302 /* Everything failed */
303 msg_Err( p_vout, "cannot open vout, aborting" );
308 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
310 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
312 ADD_PARENT_CALLBACKS( SendEventsToChild );
317 /*****************************************************************************
318 * SpawnRealVout: spawn the real video output.
319 *****************************************************************************/
320 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
322 vout_thread_t *p_real_vout = NULL;
323 video_format_t fmt = {0};
325 msg_Dbg( p_vout, "spawning the real video output" );
327 fmt.i_width = fmt.i_visible_width = p_vout->output.i_width;
328 fmt.i_height = fmt.i_visible_height = p_vout->output.i_height;
329 fmt.i_x_offset = fmt.i_y_offset = 0;
330 fmt.i_chroma = p_vout->output.i_chroma;
331 fmt.i_aspect = p_vout->output.i_aspect;
332 fmt.i_sar_num = p_vout->output.i_aspect * fmt.i_height / fmt.i_width;
333 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
335 switch( p_vout->render.i_chroma )
337 case VLC_FOURCC('I','4','2','0'):
338 case VLC_FOURCC('I','Y','U','V'):
339 case VLC_FOURCC('Y','V','1','2'):
340 switch( p_vout->p_sys->i_mode )
342 case DEINTERLACE_MEAN:
343 case DEINTERLACE_DISCARD:
344 fmt.i_height = fmt.i_visible_height = p_vout->output.i_height / 2;
345 p_real_vout = vout_Create( p_vout, &fmt );
348 case DEINTERLACE_BOB:
349 case DEINTERLACE_BLEND:
350 case DEINTERLACE_LINEAR:
351 p_real_vout = vout_Create( p_vout, &fmt );
356 case VLC_FOURCC('I','4','2','2'):
357 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
358 p_real_vout = vout_Create( p_vout, &fmt );
368 /*****************************************************************************
369 * End: terminate Deinterlace video thread output method
370 *****************************************************************************/
371 static void End( vout_thread_t *p_vout )
375 /* Free the fake output buffers we allocated */
376 for( i_index = I_OUTPUTPICTURES ; i_index ; )
379 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
383 /*****************************************************************************
384 * Destroy: destroy Deinterlace video thread output method
385 *****************************************************************************
386 * Terminate an output method created by DeinterlaceCreateOutputMethod
387 *****************************************************************************/
388 static void Destroy( vlc_object_t *p_this )
390 vout_thread_t *p_vout = (vout_thread_t *)p_this;
392 if( p_vout->p_sys->p_vout )
394 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
395 vlc_object_detach( p_vout->p_sys->p_vout );
396 vout_Destroy( p_vout->p_sys->p_vout );
399 DEL_PARENT_CALLBACKS( SendEventsToChild );
401 free( p_vout->p_sys );
404 /*****************************************************************************
405 * Render: displays previously rendered output
406 *****************************************************************************
407 * This function send the currently rendered image to Deinterlace image,
408 * waits until it is displayed and switch the two rendering buffers, preparing
410 *****************************************************************************/
411 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
413 picture_t *pp_outpic[2];
415 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
417 /* Get a new picture */
418 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
422 if( p_vout->b_die || p_vout->b_error )
424 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
427 msleep( VOUT_OUTMEM_SLEEP );
430 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
432 /* If we are using double rate, get an additional new picture */
433 if( p_vout->p_sys->b_double_rate )
435 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
439 if( p_vout->b_die || p_vout->b_error )
441 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
442 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
445 msleep( VOUT_OUTMEM_SLEEP );
448 /* 20ms is a bit arbitrary, but it's only for the first image we get */
449 if( !p_vout->p_sys->last_date )
451 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
452 p_pic->date + 20000 );
456 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
457 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
459 p_vout->p_sys->last_date = p_pic->date;
462 switch( p_vout->p_sys->i_mode )
464 case DEINTERLACE_DISCARD:
465 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
466 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
469 case DEINTERLACE_BOB:
470 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
471 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
472 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
473 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
476 case DEINTERLACE_LINEAR:
477 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
478 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
479 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
480 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
483 case DEINTERLACE_MEAN:
484 RenderMean( p_vout, pp_outpic[0], p_pic );
485 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
488 case DEINTERLACE_BLEND:
489 RenderBlend( p_vout, pp_outpic[0], p_pic );
490 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
494 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
497 /*****************************************************************************
498 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
499 *****************************************************************************/
500 static void RenderDiscard( vout_thread_t *p_vout,
501 picture_t *p_outpic, picture_t *p_pic, int i_field )
505 /* Copy image and skip lines */
506 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
508 uint8_t *p_in, *p_out_end, *p_out;
511 p_in = p_pic->p[i_plane].p_pixels
512 + i_field * p_pic->p[i_plane].i_pitch;
514 p_out = p_outpic->p[i_plane].p_pixels;
515 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
516 * p_outpic->p[i_plane].i_visible_lines;
518 switch( p_vout->render.i_chroma )
520 case VLC_FOURCC('I','4','2','0'):
521 case VLC_FOURCC('I','Y','U','V'):
522 case VLC_FOURCC('Y','V','1','2'):
524 for( ; p_out < p_out_end ; )
526 p_vout->p_vlc->pf_memcpy( p_out, p_in,
527 p_pic->p[i_plane].i_pitch );
529 p_out += p_pic->p[i_plane].i_pitch;
530 p_in += 2 * p_pic->p[i_plane].i_pitch;
534 case VLC_FOURCC('I','4','2','2'):
536 i_increment = 2 * p_pic->p[i_plane].i_pitch;
538 if( i_plane == Y_PLANE )
540 for( ; p_out < p_out_end ; )
542 p_vout->p_vlc->pf_memcpy( p_out, p_in,
543 p_pic->p[i_plane].i_pitch );
544 p_out += p_pic->p[i_plane].i_pitch;
545 p_vout->p_vlc->pf_memcpy( p_out, p_in,
546 p_pic->p[i_plane].i_pitch );
547 p_out += p_pic->p[i_plane].i_pitch;
553 for( ; p_out < p_out_end ; )
555 p_vout->p_vlc->pf_memcpy( p_out, p_in,
556 p_pic->p[i_plane].i_pitch );
557 p_out += p_pic->p[i_plane].i_pitch;
569 /*****************************************************************************
570 * RenderBob: renders a BOB picture - simple copy
571 *****************************************************************************/
572 static void RenderBob( vout_thread_t *p_vout,
573 picture_t *p_outpic, picture_t *p_pic, int i_field )
577 /* Copy image and skip lines */
578 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
580 uint8_t *p_in, *p_out_end, *p_out;
582 p_in = p_pic->p[i_plane].p_pixels;
583 p_out = p_outpic->p[i_plane].p_pixels;
584 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
585 * p_outpic->p[i_plane].i_visible_lines;
587 switch( p_vout->render.i_chroma )
589 case VLC_FOURCC('I','4','2','0'):
590 case VLC_FOURCC('I','Y','U','V'):
591 case VLC_FOURCC('Y','V','1','2'):
592 /* For BOTTOM field we need to add the first line */
595 p_vout->p_vlc->pf_memcpy( p_out, p_in,
596 p_pic->p[i_plane].i_pitch );
597 p_in += p_pic->p[i_plane].i_pitch;
598 p_out += p_pic->p[i_plane].i_pitch;
601 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
603 for( ; p_out < p_out_end ; )
605 p_vout->p_vlc->pf_memcpy( p_out, p_in,
606 p_pic->p[i_plane].i_pitch );
608 p_out += p_pic->p[i_plane].i_pitch;
610 p_vout->p_vlc->pf_memcpy( p_out, p_in,
611 p_pic->p[i_plane].i_pitch );
613 p_in += 2 * p_pic->p[i_plane].i_pitch;
614 p_out += p_pic->p[i_plane].i_pitch;
617 p_vout->p_vlc->pf_memcpy( p_out, p_in,
618 p_pic->p[i_plane].i_pitch );
620 /* For TOP field we need to add the last line */
623 p_in += p_pic->p[i_plane].i_pitch;
624 p_out += p_pic->p[i_plane].i_pitch;
625 p_vout->p_vlc->pf_memcpy( p_out, p_in,
626 p_pic->p[i_plane].i_pitch );
630 case VLC_FOURCC('I','4','2','2'):
631 /* For BOTTOM field we need to add the first line */
634 p_vout->p_vlc->pf_memcpy( p_out, p_in,
635 p_pic->p[i_plane].i_pitch );
636 p_in += p_pic->p[i_plane].i_pitch;
637 p_out += p_pic->p[i_plane].i_pitch;
640 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
642 if( i_plane == Y_PLANE )
644 for( ; p_out < p_out_end ; )
646 p_vout->p_vlc->pf_memcpy( p_out, p_in,
647 p_pic->p[i_plane].i_pitch );
649 p_out += p_pic->p[i_plane].i_pitch;
651 p_vout->p_vlc->pf_memcpy( p_out, p_in,
652 p_pic->p[i_plane].i_pitch );
654 p_in += 2 * p_pic->p[i_plane].i_pitch;
655 p_out += p_pic->p[i_plane].i_pitch;
660 for( ; p_out < p_out_end ; )
662 p_vout->p_vlc->pf_memcpy( p_out, p_in,
663 p_pic->p[i_plane].i_pitch );
665 p_out += p_pic->p[i_plane].i_pitch;
666 p_in += 2 * p_pic->p[i_plane].i_pitch;
670 p_vout->p_vlc->pf_memcpy( p_out, p_in,
671 p_pic->p[i_plane].i_pitch );
673 /* For TOP field we need to add the last line */
676 p_in += p_pic->p[i_plane].i_pitch;
677 p_out += p_pic->p[i_plane].i_pitch;
678 p_vout->p_vlc->pf_memcpy( p_out, p_in,
679 p_pic->p[i_plane].i_pitch );
686 #define Merge p_vout->p_sys->pf_merge
687 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
689 /*****************************************************************************
690 * RenderLinear: BOB with linear interpolation
691 *****************************************************************************/
692 static void RenderLinear( vout_thread_t *p_vout,
693 picture_t *p_outpic, picture_t *p_pic, int i_field )
697 /* Copy image and skip lines */
698 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
700 uint8_t *p_in, *p_out_end, *p_out;
702 p_in = p_pic->p[i_plane].p_pixels;
703 p_out = p_outpic->p[i_plane].p_pixels;
704 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
705 * p_outpic->p[i_plane].i_visible_lines;
707 /* For BOTTOM field we need to add the first line */
710 p_vout->p_vlc->pf_memcpy( p_out, p_in,
711 p_pic->p[i_plane].i_pitch );
712 p_in += p_pic->p[i_plane].i_pitch;
713 p_out += p_pic->p[i_plane].i_pitch;
716 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
718 for( ; p_out < p_out_end ; )
720 p_vout->p_vlc->pf_memcpy( p_out, p_in,
721 p_pic->p[i_plane].i_pitch );
723 p_out += p_pic->p[i_plane].i_pitch;
725 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
726 p_pic->p[i_plane].i_pitch );
728 p_in += 2 * p_pic->p[i_plane].i_pitch;
729 p_out += p_pic->p[i_plane].i_pitch;
732 p_vout->p_vlc->pf_memcpy( p_out, p_in,
733 p_pic->p[i_plane].i_pitch );
735 /* For TOP field we need to add the last line */
738 p_in += p_pic->p[i_plane].i_pitch;
739 p_out += p_pic->p[i_plane].i_pitch;
740 p_vout->p_vlc->pf_memcpy( p_out, p_in,
741 p_pic->p[i_plane].i_pitch );
747 static void RenderMean( vout_thread_t *p_vout,
748 picture_t *p_outpic, picture_t *p_pic )
752 /* Copy image and skip lines */
753 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
755 uint8_t *p_in, *p_out_end, *p_out;
757 p_in = p_pic->p[i_plane].p_pixels;
759 p_out = p_outpic->p[i_plane].p_pixels;
760 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
761 * p_outpic->p[i_plane].i_visible_lines;
763 /* All lines: mean value */
764 for( ; p_out < p_out_end ; )
766 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
767 p_pic->p[i_plane].i_pitch );
769 p_out += p_pic->p[i_plane].i_pitch;
770 p_in += 2 * p_pic->p[i_plane].i_pitch;
776 static void RenderBlend( vout_thread_t *p_vout,
777 picture_t *p_outpic, picture_t *p_pic )
781 /* Copy image and skip lines */
782 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
784 uint8_t *p_in, *p_out_end, *p_out;
786 p_in = p_pic->p[i_plane].p_pixels;
788 p_out = p_outpic->p[i_plane].p_pixels;
789 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
790 * p_outpic->p[i_plane].i_visible_lines;
792 switch( p_vout->render.i_chroma )
794 case VLC_FOURCC('I','4','2','0'):
795 case VLC_FOURCC('I','Y','U','V'):
796 case VLC_FOURCC('Y','V','1','2'):
797 /* First line: simple copy */
798 p_vout->p_vlc->pf_memcpy( p_out, p_in,
799 p_pic->p[i_plane].i_pitch );
800 p_out += p_pic->p[i_plane].i_pitch;
802 /* Remaining lines: mean value */
803 for( ; p_out < p_out_end ; )
805 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
806 p_pic->p[i_plane].i_pitch );
808 p_out += p_pic->p[i_plane].i_pitch;
809 p_in += p_pic->p[i_plane].i_pitch;
813 case VLC_FOURCC('I','4','2','2'):
814 /* First line: simple copy */
815 p_vout->p_vlc->pf_memcpy( p_out, p_in,
816 p_pic->p[i_plane].i_pitch );
817 p_out += p_pic->p[i_plane].i_pitch;
819 /* Remaining lines: mean value */
820 if( i_plane == Y_PLANE )
822 for( ; p_out < p_out_end ; )
824 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
825 p_pic->p[i_plane].i_pitch );
827 p_out += p_pic->p[i_plane].i_pitch;
828 p_in += p_pic->p[i_plane].i_pitch;
834 for( ; p_out < p_out_end ; )
836 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
837 p_pic->p[i_plane].i_pitch );
839 p_out += p_pic->p[i_plane].i_pitch;
840 p_in += 2*p_pic->p[i_plane].i_pitch;
851 static void MergeGeneric( void *_p_dest, const void *_p_s1,
852 const void *_p_s2, size_t i_bytes )
854 uint8_t* p_dest = (uint8_t*)_p_dest;
855 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
856 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
857 uint8_t* p_end = p_dest + i_bytes - 8;
859 while( p_dest < p_end )
861 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
862 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
863 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
864 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
865 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
866 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
867 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
868 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
873 while( p_dest < p_end )
875 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
879 #if defined(CAN_COMPILE_MMXEXT)
880 static void MergeMMX( void *_p_dest, const void *_p_s1, const void *_p_s2,
883 uint8_t* p_dest = (uint8_t*)_p_dest;
884 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
885 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
886 uint8_t* p_end = p_dest + i_bytes - 8;
887 while( p_dest < p_end )
889 __asm__ __volatile__( "movq %2,%%mm1;"
891 "movq %%mm1, %0" :"=m" (*p_dest):
901 while( p_dest < p_end )
903 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
908 #if defined(CAN_COMPILE_SSE)
909 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
912 uint8_t* p_dest = (uint8_t*)_p_dest;
913 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
914 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
916 while( (int)p_s1 % 16 )
918 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
920 p_end = p_dest + i_bytes - 16;
921 while( p_dest < p_end )
923 __asm__ __volatile__( "movdqu %2,%%xmm1;"
925 "movdqu %%xmm1, %0" :"=m" (*p_dest):
935 while( p_dest < p_end )
937 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
942 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
943 static void EndMMX( void )
945 __asm__ __volatile__( "emms" :: );
949 #ifdef CAN_COMPILE_C_ALTIVEC
950 static void MergeAltivec( void *_p_dest, const void *_p_s1,
951 const void *_p_s2, size_t i_bytes )
953 uint8_t *p_dest = (uint8_t *)_p_dest;
954 uint8_t *p_s1 = (uint8_t *)_p_s1;
955 uint8_t *p_s2 = (uint8_t *)_p_s2;
956 uint8_t *p_end = p_dest + i_bytes - 15;
958 /* Use C until the first 16-bytes aligned destination pixel */
959 while( (int)p_dest & 0xF )
961 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
964 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
966 /* Unaligned source */
967 vector unsigned char s1v, s2v, destv;
968 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
969 vector unsigned char perm1v, perm2v;
971 perm1v = vec_lvsl( 0, p_s1 );
972 perm2v = vec_lvsl( 0, p_s2 );
973 s1oldv = vec_ld( 0, p_s1 );
974 s2oldv = vec_ld( 0, p_s2 );
976 while( p_dest < p_end )
978 s1newv = vec_ld( 16, p_s1 );
979 s2newv = vec_ld( 16, p_s2 );
980 s1v = vec_perm( s1oldv, s1newv, perm1v );
981 s2v = vec_perm( s2oldv, s2newv, perm2v );
984 destv = vec_avg( s1v, s2v );
985 vec_st( destv, 0, p_dest );
995 vector unsigned char s1v, s2v, destv;
997 while( p_dest < p_end )
999 s1v = vec_ld( 0, p_s1 );
1000 s2v = vec_ld( 0, p_s2 );
1001 destv = vec_avg( s1v, s2v );
1002 vec_st( destv, 0, p_dest );
1012 while( p_dest < p_end )
1014 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1019 /*****************************************************************************
1020 * SendEvents: forward mouse and keyboard events to the parent p_vout
1021 *****************************************************************************/
1022 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1023 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1025 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1026 vlc_value_t sentval = newval;
1028 if( !strcmp( psz_var, "mouse-y" ) )
1030 switch( p_vout->p_sys->i_mode )
1032 case DEINTERLACE_MEAN:
1033 case DEINTERLACE_DISCARD:
1039 var_Set( p_vout, psz_var, sentval );
1044 /*****************************************************************************
1045 * FilterCallback: called when changing the deinterlace method on the fly.
1046 *****************************************************************************/
1047 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1048 vlc_value_t oldval, vlc_value_t newval,
1051 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1052 int i_old_mode = p_vout->p_sys->i_mode;
1054 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1056 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1058 SetFilterMethod( p_vout, newval.psz_string );
1060 switch( p_vout->render.i_chroma )
1062 case VLC_FOURCC('I','4','2','2'):
1063 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1067 case VLC_FOURCC('I','4','2','0'):
1068 case VLC_FOURCC('I','Y','U','V'):
1069 case VLC_FOURCC('Y','V','1','2'):
1070 switch( p_vout->p_sys->i_mode )
1072 case DEINTERLACE_MEAN:
1073 case DEINTERLACE_DISCARD:
1074 if( ( i_old_mode == DEINTERLACE_MEAN )
1075 || ( i_old_mode == DEINTERLACE_DISCARD ) )
1077 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1082 case DEINTERLACE_BOB:
1083 case DEINTERLACE_BLEND:
1084 case DEINTERLACE_LINEAR:
1085 if( ( i_old_mode == DEINTERLACE_BOB )
1086 || ( i_old_mode == DEINTERLACE_BLEND )
1087 || ( i_old_mode == DEINTERLACE_LINEAR ) )
1089 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1100 /* We need to kill the old vout */
1102 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1104 vlc_object_detach( p_vout->p_sys->p_vout );
1105 vout_Destroy( p_vout->p_sys->p_vout );
1107 /* Try to open a new video output */
1108 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
1110 if( p_vout->p_sys->p_vout == NULL )
1112 /* Everything failed */
1113 msg_Err( p_vout, "cannot open vout, aborting" );
1115 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1116 return VLC_EGENERIC;
1119 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1121 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1125 /*****************************************************************************
1126 * SendEventsToChild: forward events to the child/children vout
1127 *****************************************************************************/
1128 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
1129 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1131 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1132 var_Set( p_vout->p_sys->p_vout, psz_var, newval );