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_MMX)
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_MMX) || 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_capability( "video filter", 0 );
102 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
103 MODE_LONGTEXT, VLC_FALSE );
104 change_string_list( mode_list, mode_list_text, 0 );
106 add_shortcut( "deinterlace" );
107 set_callbacks( Create, Destroy );
110 /*****************************************************************************
111 * vout_sys_t: Deinterlace video output method descriptor
112 *****************************************************************************
113 * This structure is part of the video output thread descriptor.
114 * It describes the Deinterlace specific properties of an output thread.
115 *****************************************************************************/
118 int i_mode; /* Deinterlace mode */
119 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
124 vout_thread_t *p_vout;
126 vlc_mutex_t filter_lock;
128 void (*pf_merge) ( void *, const void *, const void *, size_t );
129 void (*pf_end_merge) ( void );
132 /*****************************************************************************
133 * Create: allocates Deinterlace video thread output method
134 *****************************************************************************
135 * This function allocates and initializes a Deinterlace vout method.
136 *****************************************************************************/
137 static int Create( vlc_object_t *p_this )
139 vout_thread_t *p_vout = (vout_thread_t *)p_this;
142 /* Allocate structure */
143 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
144 if( p_vout->p_sys == NULL )
146 msg_Err( p_vout, "out of memory" );
150 p_vout->pf_init = Init;
151 p_vout->pf_end = End;
152 p_vout->pf_manage = NULL;
153 p_vout->pf_render = Render;
154 p_vout->pf_display = NULL;
156 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
157 p_vout->p_sys->b_double_rate = 0;
158 p_vout->p_sys->last_date = 0;
159 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
161 #if defined(CAN_COMPILE_C_ALTIVEC)
162 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
164 p_vout->p_sys->pf_merge = MergeAltivec;
165 p_vout->p_sys->pf_end_merge = NULL;
169 #if defined(CAN_COMPILE_SSE)
170 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2 )
172 p_vout->p_sys->pf_merge = MergeSSE2;
173 p_vout->p_sys->pf_end_merge = EndMMX;
177 #if defined(CAN_COMPILE_MMX)
178 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
180 p_vout->p_sys->pf_merge = MergeMMX;
181 p_vout->p_sys->pf_end_merge = EndMMX;
186 p_vout->p_sys->pf_merge = MergeGeneric;
187 p_vout->p_sys->pf_end_merge = NULL;
190 /* Look what method was requested */
191 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
192 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
194 if( val.psz_string == NULL )
196 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
197 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
199 val.psz_string = strdup( "discard" );
202 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
204 SetFilterMethod( p_vout, val.psz_string );
206 free( val.psz_string );
208 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
213 /*****************************************************************************
214 * SetFilterMethod: setup the deinterlace method to use.
215 *****************************************************************************/
216 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
218 if( !strcmp( psz_method, "discard" ) )
220 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
221 p_vout->p_sys->b_double_rate = 0;
223 else if( !strcmp( psz_method, "mean" ) )
225 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
226 p_vout->p_sys->b_double_rate = 0;
228 else if( !strcmp( psz_method, "blend" )
229 || !strcmp( psz_method, "average" )
230 || !strcmp( psz_method, "combine-fields" ) )
232 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
233 p_vout->p_sys->b_double_rate = 0;
235 else if( !strcmp( psz_method, "bob" )
236 || !strcmp( psz_method, "progressive-scan" ) )
238 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
239 p_vout->p_sys->b_double_rate = 1;
241 else if( !strcmp( psz_method, "linear" ) )
243 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
244 p_vout->p_sys->b_double_rate = 1;
248 msg_Err( p_vout, "no valid deinterlace mode provided, "
249 "using \"discard\"" );
252 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
255 /*****************************************************************************
256 * Init: initialize Deinterlace video thread output method
257 *****************************************************************************/
258 static int Init( vout_thread_t *p_vout )
263 I_OUTPUTPICTURES = 0;
265 /* Initialize the output structure, full of directbuffers since we want
266 * the decoder to output directly to our structures. */
267 switch( p_vout->render.i_chroma )
269 case VLC_FOURCC('I','4','2','0'):
270 case VLC_FOURCC('I','Y','U','V'):
271 case VLC_FOURCC('Y','V','1','2'):
272 case VLC_FOURCC('I','4','2','2'):
273 p_vout->output.i_chroma = p_vout->render.i_chroma;
274 p_vout->output.i_width = p_vout->render.i_width;
275 p_vout->output.i_height = p_vout->render.i_height;
276 p_vout->output.i_aspect = p_vout->render.i_aspect;
280 return VLC_EGENERIC; /* unknown chroma */
284 /* Try to open the real video output */
285 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
287 if( p_vout->p_sys->p_vout == NULL )
289 /* Everything failed */
290 msg_Err( p_vout, "cannot open vout, aborting" );
295 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
297 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
299 ADD_PARENT_CALLBACKS( SendEventsToChild );
304 /*****************************************************************************
305 * SpawnRealVout: spawn the real video output.
306 *****************************************************************************/
307 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
309 vout_thread_t *p_real_vout = NULL;
311 msg_Dbg( p_vout, "spawning the real video output" );
313 switch( p_vout->render.i_chroma )
315 case VLC_FOURCC('I','4','2','0'):
316 case VLC_FOURCC('I','Y','U','V'):
317 case VLC_FOURCC('Y','V','1','2'):
318 switch( p_vout->p_sys->i_mode )
320 case DEINTERLACE_MEAN:
321 case DEINTERLACE_DISCARD:
324 p_vout->output.i_width, p_vout->output.i_height / 2,
325 p_vout->output.i_chroma, p_vout->output.i_aspect );
328 case DEINTERLACE_BOB:
329 case DEINTERLACE_BLEND:
330 case DEINTERLACE_LINEAR:
333 p_vout->output.i_width, p_vout->output.i_height,
334 p_vout->output.i_chroma, p_vout->output.i_aspect );
339 case VLC_FOURCC('I','4','2','2'):
342 p_vout->output.i_width, p_vout->output.i_height,
343 VLC_FOURCC('I','4','2','0'), p_vout->output.i_aspect );
353 /*****************************************************************************
354 * End: terminate Deinterlace video thread output method
355 *****************************************************************************/
356 static void End( vout_thread_t *p_vout )
360 /* Free the fake output buffers we allocated */
361 for( i_index = I_OUTPUTPICTURES ; i_index ; )
364 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
368 /*****************************************************************************
369 * Destroy: destroy Deinterlace video thread output method
370 *****************************************************************************
371 * Terminate an output method created by DeinterlaceCreateOutputMethod
372 *****************************************************************************/
373 static void Destroy( vlc_object_t *p_this )
375 vout_thread_t *p_vout = (vout_thread_t *)p_this;
377 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
379 vlc_object_detach( p_vout->p_sys->p_vout );
380 vout_Destroy( p_vout->p_sys->p_vout );
382 DEL_PARENT_CALLBACKS( SendEventsToChild );
384 free( p_vout->p_sys );
387 /*****************************************************************************
388 * Render: displays previously rendered output
389 *****************************************************************************
390 * This function send the currently rendered image to Deinterlace image,
391 * waits until it is displayed and switch the two rendering buffers, preparing
393 *****************************************************************************/
394 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
396 picture_t *pp_outpic[2];
398 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
400 /* Get a new picture */
401 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
405 if( p_vout->b_die || p_vout->b_error )
407 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
410 msleep( VOUT_OUTMEM_SLEEP );
413 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
415 /* If we are using double rate, get an additional new picture */
416 if( p_vout->p_sys->b_double_rate )
418 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
422 if( p_vout->b_die || p_vout->b_error )
424 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
425 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
428 msleep( VOUT_OUTMEM_SLEEP );
431 /* 20ms is a bit arbitrary, but it's only for the first image we get */
432 if( !p_vout->p_sys->last_date )
434 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
435 p_pic->date + 20000 );
439 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
440 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
442 p_vout->p_sys->last_date = p_pic->date;
445 switch( p_vout->p_sys->i_mode )
447 case DEINTERLACE_DISCARD:
448 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
449 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
452 case DEINTERLACE_BOB:
453 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
454 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
455 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
456 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
459 case DEINTERLACE_LINEAR:
460 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
461 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
462 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
463 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
466 case DEINTERLACE_MEAN:
467 RenderMean( p_vout, pp_outpic[0], p_pic );
468 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
471 case DEINTERLACE_BLEND:
472 RenderBlend( p_vout, pp_outpic[0], p_pic );
473 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
477 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
480 /*****************************************************************************
481 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
482 *****************************************************************************/
483 static void RenderDiscard( vout_thread_t *p_vout,
484 picture_t *p_outpic, picture_t *p_pic, int i_field )
488 /* Copy image and skip lines */
489 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
491 uint8_t *p_in, *p_out_end, *p_out;
494 p_in = p_pic->p[i_plane].p_pixels
495 + i_field * p_pic->p[i_plane].i_pitch;
497 p_out = p_outpic->p[i_plane].p_pixels;
498 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
499 * p_outpic->p[i_plane].i_lines;
501 switch( p_vout->render.i_chroma )
503 case VLC_FOURCC('I','4','2','0'):
504 case VLC_FOURCC('I','Y','U','V'):
505 case VLC_FOURCC('Y','V','1','2'):
507 for( ; p_out < p_out_end ; )
509 p_vout->p_vlc->pf_memcpy( p_out, p_in,
510 p_pic->p[i_plane].i_pitch );
512 p_out += p_pic->p[i_plane].i_pitch;
513 p_in += 2 * p_pic->p[i_plane].i_pitch;
517 case VLC_FOURCC('I','4','2','2'):
519 i_increment = 2 * p_pic->p[i_plane].i_pitch;
521 if( i_plane == Y_PLANE )
523 for( ; p_out < p_out_end ; )
525 p_vout->p_vlc->pf_memcpy( p_out, p_in,
526 p_pic->p[i_plane].i_pitch );
527 p_out += p_pic->p[i_plane].i_pitch;
528 p_vout->p_vlc->pf_memcpy( p_out, p_in,
529 p_pic->p[i_plane].i_pitch );
530 p_out += p_pic->p[i_plane].i_pitch;
536 for( ; p_out < p_out_end ; )
538 p_vout->p_vlc->pf_memcpy( p_out, p_in,
539 p_pic->p[i_plane].i_pitch );
540 p_out += p_pic->p[i_plane].i_pitch;
552 /*****************************************************************************
553 * RenderBob: renders a BOB picture - simple copy
554 *****************************************************************************/
555 static void RenderBob( vout_thread_t *p_vout,
556 picture_t *p_outpic, picture_t *p_pic, int i_field )
560 /* Copy image and skip lines */
561 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
563 uint8_t *p_in, *p_out_end, *p_out;
565 p_in = p_pic->p[i_plane].p_pixels;
566 p_out = p_outpic->p[i_plane].p_pixels;
567 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
568 * p_outpic->p[i_plane].i_lines;
570 switch( p_vout->render.i_chroma )
572 case VLC_FOURCC('I','4','2','0'):
573 case VLC_FOURCC('I','Y','U','V'):
574 case VLC_FOURCC('Y','V','1','2'):
575 /* For BOTTOM field we need to add the first line */
578 p_vout->p_vlc->pf_memcpy( p_out, p_in,
579 p_pic->p[i_plane].i_pitch );
580 p_in += p_pic->p[i_plane].i_pitch;
581 p_out += p_pic->p[i_plane].i_pitch;
584 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
586 for( ; p_out < p_out_end ; )
588 p_vout->p_vlc->pf_memcpy( p_out, p_in,
589 p_pic->p[i_plane].i_pitch );
591 p_out += p_pic->p[i_plane].i_pitch;
593 p_vout->p_vlc->pf_memcpy( p_out, p_in,
594 p_pic->p[i_plane].i_pitch );
596 p_in += 2 * p_pic->p[i_plane].i_pitch;
597 p_out += p_pic->p[i_plane].i_pitch;
600 p_vout->p_vlc->pf_memcpy( p_out, p_in,
601 p_pic->p[i_plane].i_pitch );
603 /* For TOP field we need to add the last line */
606 p_in += p_pic->p[i_plane].i_pitch;
607 p_out += p_pic->p[i_plane].i_pitch;
608 p_vout->p_vlc->pf_memcpy( p_out, p_in,
609 p_pic->p[i_plane].i_pitch );
613 case VLC_FOURCC('I','4','2','2'):
614 /* For BOTTOM field we need to add the first line */
617 p_vout->p_vlc->pf_memcpy( p_out, p_in,
618 p_pic->p[i_plane].i_pitch );
619 p_in += p_pic->p[i_plane].i_pitch;
620 p_out += p_pic->p[i_plane].i_pitch;
623 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
625 if( i_plane == Y_PLANE )
627 for( ; p_out < p_out_end ; )
629 p_vout->p_vlc->pf_memcpy( p_out, p_in,
630 p_pic->p[i_plane].i_pitch );
632 p_out += p_pic->p[i_plane].i_pitch;
634 p_vout->p_vlc->pf_memcpy( p_out, p_in,
635 p_pic->p[i_plane].i_pitch );
637 p_in += 2 * p_pic->p[i_plane].i_pitch;
638 p_out += p_pic->p[i_plane].i_pitch;
643 for( ; p_out < p_out_end ; )
645 p_vout->p_vlc->pf_memcpy( p_out, p_in,
646 p_pic->p[i_plane].i_pitch );
648 p_out += p_pic->p[i_plane].i_pitch;
649 p_in += 2 * p_pic->p[i_plane].i_pitch;
653 p_vout->p_vlc->pf_memcpy( p_out, p_in,
654 p_pic->p[i_plane].i_pitch );
656 /* For TOP field we need to add the last line */
659 p_in += p_pic->p[i_plane].i_pitch;
660 p_out += p_pic->p[i_plane].i_pitch;
661 p_vout->p_vlc->pf_memcpy( p_out, p_in,
662 p_pic->p[i_plane].i_pitch );
669 #define Merge p_vout->p_sys->pf_merge
670 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
672 /*****************************************************************************
673 * RenderLinear: BOB with linear interpolation
674 *****************************************************************************/
675 static void RenderLinear( vout_thread_t *p_vout,
676 picture_t *p_outpic, picture_t *p_pic, int i_field )
680 /* Copy image and skip lines */
681 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
683 uint8_t *p_in, *p_out_end, *p_out;
685 p_in = p_pic->p[i_plane].p_pixels;
686 p_out = p_outpic->p[i_plane].p_pixels;
687 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
688 * p_outpic->p[i_plane].i_lines;
690 /* For BOTTOM field we need to add the first line */
693 p_vout->p_vlc->pf_memcpy( p_out, p_in,
694 p_pic->p[i_plane].i_pitch );
695 p_in += p_pic->p[i_plane].i_pitch;
696 p_out += p_pic->p[i_plane].i_pitch;
699 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
701 for( ; p_out < p_out_end ; )
703 p_vout->p_vlc->pf_memcpy( p_out, p_in,
704 p_pic->p[i_plane].i_pitch );
706 p_out += p_pic->p[i_plane].i_pitch;
708 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
709 p_pic->p[i_plane].i_pitch );
711 p_in += 2 * p_pic->p[i_plane].i_pitch;
712 p_out += p_pic->p[i_plane].i_pitch;
715 p_vout->p_vlc->pf_memcpy( p_out, p_in,
716 p_pic->p[i_plane].i_pitch );
718 /* For TOP field we need to add the last line */
721 p_in += p_pic->p[i_plane].i_pitch;
722 p_out += p_pic->p[i_plane].i_pitch;
723 p_vout->p_vlc->pf_memcpy( p_out, p_in,
724 p_pic->p[i_plane].i_pitch );
730 static void RenderMean( vout_thread_t *p_vout,
731 picture_t *p_outpic, picture_t *p_pic )
735 /* Copy image and skip lines */
736 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
738 uint8_t *p_in, *p_out_end, *p_out;
740 p_in = p_pic->p[i_plane].p_pixels;
742 p_out = p_outpic->p[i_plane].p_pixels;
743 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
744 * p_outpic->p[i_plane].i_lines;
746 /* All lines: mean value */
747 for( ; p_out < p_out_end ; )
749 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
750 p_pic->p[i_plane].i_pitch );
752 p_out += p_pic->p[i_plane].i_pitch;
753 p_in += 2 * p_pic->p[i_plane].i_pitch;
759 static void RenderBlend( vout_thread_t *p_vout,
760 picture_t *p_outpic, picture_t *p_pic )
764 /* Copy image and skip lines */
765 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
767 uint8_t *p_in, *p_out_end, *p_out;
769 p_in = p_pic->p[i_plane].p_pixels;
771 p_out = p_outpic->p[i_plane].p_pixels;
772 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
773 * p_outpic->p[i_plane].i_lines;
775 switch( p_vout->render.i_chroma )
777 case VLC_FOURCC('I','4','2','0'):
778 case VLC_FOURCC('I','Y','U','V'):
779 case VLC_FOURCC('Y','V','1','2'):
780 /* First line: simple copy */
781 p_vout->p_vlc->pf_memcpy( p_out, p_in,
782 p_pic->p[i_plane].i_pitch );
783 p_out += p_pic->p[i_plane].i_pitch;
785 /* Remaining lines: mean value */
786 for( ; p_out < p_out_end ; )
788 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
789 p_pic->p[i_plane].i_pitch );
791 p_out += p_pic->p[i_plane].i_pitch;
792 p_in += p_pic->p[i_plane].i_pitch;
796 case VLC_FOURCC('I','4','2','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 if( i_plane == Y_PLANE )
805 for( ; p_out < p_out_end ; )
807 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
808 p_pic->p[i_plane].i_pitch );
810 p_out += p_pic->p[i_plane].i_pitch;
811 p_in += p_pic->p[i_plane].i_pitch;
817 for( ; p_out < p_out_end ; )
819 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
820 p_pic->p[i_plane].i_pitch );
822 p_out += p_pic->p[i_plane].i_pitch;
823 p_in += 2*p_pic->p[i_plane].i_pitch;
834 static void MergeGeneric( void *_p_dest, const void *_p_s1,
835 const void *_p_s2, size_t i_bytes )
837 uint8_t* p_dest = (uint8_t*)_p_dest;
838 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
839 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
840 uint8_t* p_end = p_dest + i_bytes - 8;
842 while( p_dest < p_end )
844 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
845 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
846 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
847 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
848 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
849 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
850 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
851 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
856 while( p_dest < p_end )
858 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
862 #if defined(CAN_COMPILE_MMX)
863 static void MergeMMX( void *_p_dest, const void *_p_s1, const void *_p_s2,
866 uint8_t* p_dest = (uint8_t*)_p_dest;
867 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
868 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
869 uint8_t* p_end = p_dest + i_bytes - 8;
870 while( p_dest < p_end )
872 __asm__ __volatile__( "movq %2,%%mm1;"
874 "movq %%mm1, %0" :"=m" (*p_dest):
884 while( p_dest < p_end )
886 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
891 #if defined(CAN_COMPILE_SSE)
892 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
895 uint8_t* p_dest = (uint8_t*)_p_dest;
896 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
897 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
898 while( (int)p_s1 % 16 )
900 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
902 uint8_t* p_end = p_dest + i_bytes - 16;
903 while( p_dest < p_end )
905 __asm__ __volatile__( "movdqu %2,%%xmm1;"
907 "movdqu %%xmm1, %0" :"=m" (*p_dest):
917 while( p_dest < p_end )
919 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
924 #if defined(CAN_COMPILE_MMX) || defined(CAN_COMPILE_SSE)
925 static void EndMMX( void )
927 __asm__ __volatile__( "emms" :: );
931 #ifdef CAN_COMPILE_C_ALTIVEC
932 static void MergeAltivec( void *_p_dest, const void *_p_s1,
933 const void *_p_s2, size_t i_bytes )
935 uint8_t *p_dest = (uint8_t *)_p_dest;
936 uint8_t *p_s1 = (uint8_t *)_p_s1;
937 uint8_t *p_s2 = (uint8_t *)_p_s2;
938 uint8_t *p_end = p_dest + i_bytes - 15;
940 /* Use C until the first 16-bytes aligned destination pixel */
941 while( (int)p_dest & 0xF )
943 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
946 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
948 /* Unaligned source */
949 vector unsigned char s1v, s2v, destv;
950 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
951 vector unsigned char perm1v, perm2v;
953 perm1v = vec_lvsl( 0, p_s1 );
954 perm2v = vec_lvsl( 0, p_s2 );
955 s1oldv = vec_ld( 0, p_s1 );
956 s2oldv = vec_ld( 0, p_s2 );
958 while( p_dest < p_end )
960 s1newv = vec_ld( 16, p_s1 );
961 s2newv = vec_ld( 16, p_s2 );
962 s1v = vec_perm( s1oldv, s1newv, perm1v );
963 s2v = vec_perm( s2oldv, s2newv, perm2v );
966 destv = vec_avg( s1v, s2v );
967 vec_st( destv, 0, p_dest );
977 vector unsigned char s1v, s2v, destv;
979 while( p_dest < p_end )
981 s1v = vec_ld( 0, p_s1 );
982 s2v = vec_ld( 0, p_s2 );
983 destv = vec_avg( s1v, s2v );
984 vec_st( destv, 0, p_dest );
994 while( p_dest < p_end )
996 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1001 /*****************************************************************************
1002 * SendEvents: forward mouse and keyboard events to the parent p_vout
1003 *****************************************************************************/
1004 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1005 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1007 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1008 vlc_value_t sentval = newval;
1010 if( !strcmp( psz_var, "mouse-y" ) )
1012 switch( p_vout->p_sys->i_mode )
1014 case DEINTERLACE_MEAN:
1015 case DEINTERLACE_DISCARD:
1021 var_Set( p_vout, psz_var, sentval );
1026 /*****************************************************************************
1027 * FilterCallback: called when changing the deinterlace method on the fly.
1028 *****************************************************************************/
1029 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1030 vlc_value_t oldval, vlc_value_t newval,
1033 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1034 int i_old_mode = p_vout->p_sys->i_mode;
1036 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1038 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1040 SetFilterMethod( p_vout, newval.psz_string );
1042 switch( p_vout->render.i_chroma )
1044 case VLC_FOURCC('I','4','2','2'):
1045 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1049 case VLC_FOURCC('I','4','2','0'):
1050 case VLC_FOURCC('I','Y','U','V'):
1051 case VLC_FOURCC('Y','V','1','2'):
1052 switch( p_vout->p_sys->i_mode )
1054 case DEINTERLACE_MEAN:
1055 case DEINTERLACE_DISCARD:
1056 if( ( i_old_mode == DEINTERLACE_MEAN )
1057 || ( i_old_mode == DEINTERLACE_DISCARD ) )
1059 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1064 case DEINTERLACE_BOB:
1065 case DEINTERLACE_BLEND:
1066 case DEINTERLACE_LINEAR:
1067 if( ( i_old_mode == DEINTERLACE_BOB )
1068 || ( i_old_mode == DEINTERLACE_BLEND )
1069 || ( i_old_mode == DEINTERLACE_LINEAR ) )
1071 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1082 /* We need to kill the old vout */
1084 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1086 vlc_object_detach( p_vout->p_sys->p_vout );
1087 vout_Destroy( p_vout->p_sys->p_vout );
1089 /* Try to open a new video output */
1090 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
1092 if( p_vout->p_sys->p_vout == NULL )
1094 /* Everything failed */
1095 msg_Err( p_vout, "cannot open vout, aborting" );
1097 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1098 return VLC_EGENERIC;
1101 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1103 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1107 /*****************************************************************************
1108 * SendEventsToChild: forward events to the child/children vout
1109 *****************************************************************************/
1110 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
1111 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1113 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1114 var_Set( p_vout->p_sys->p_vout, psz_var, newval );