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_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 * Control: control facility for the vout (forwards to child vout)
134 *****************************************************************************/
135 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
137 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
140 /*****************************************************************************
141 * Create: allocates Deinterlace video thread output method
142 *****************************************************************************
143 * This function allocates and initializes a Deinterlace vout method.
144 *****************************************************************************/
145 static int Create( vlc_object_t *p_this )
147 vout_thread_t *p_vout = (vout_thread_t *)p_this;
150 /* Allocate structure */
151 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
152 if( p_vout->p_sys == NULL )
154 msg_Err( p_vout, "out of memory" );
158 p_vout->pf_init = Init;
159 p_vout->pf_end = End;
160 p_vout->pf_manage = NULL;
161 p_vout->pf_render = Render;
162 p_vout->pf_display = NULL;
163 p_vout->pf_control = Control;
165 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
166 p_vout->p_sys->b_double_rate = 0;
167 p_vout->p_sys->last_date = 0;
168 p_vout->p_sys->p_vout = 0;
169 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
171 #if defined(CAN_COMPILE_C_ALTIVEC)
172 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
174 p_vout->p_sys->pf_merge = MergeAltivec;
175 p_vout->p_sys->pf_end_merge = NULL;
179 #if defined(CAN_COMPILE_SSE)
180 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2 )
182 p_vout->p_sys->pf_merge = MergeSSE2;
183 p_vout->p_sys->pf_end_merge = EndMMX;
187 #if defined(CAN_COMPILE_MMXEXT)
188 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
190 p_vout->p_sys->pf_merge = MergeMMX;
191 p_vout->p_sys->pf_end_merge = EndMMX;
196 p_vout->p_sys->pf_merge = MergeGeneric;
197 p_vout->p_sys->pf_end_merge = NULL;
200 /* Look what method was requested */
201 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
202 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
204 if( val.psz_string == NULL )
206 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
207 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
209 val.psz_string = strdup( "discard" );
212 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
214 SetFilterMethod( p_vout, val.psz_string );
216 free( val.psz_string );
218 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
223 /*****************************************************************************
224 * SetFilterMethod: setup the deinterlace method to use.
225 *****************************************************************************/
226 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
228 if( !strcmp( psz_method, "discard" ) )
230 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
231 p_vout->p_sys->b_double_rate = 0;
233 else if( !strcmp( psz_method, "mean" ) )
235 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
236 p_vout->p_sys->b_double_rate = 0;
238 else if( !strcmp( psz_method, "blend" )
239 || !strcmp( psz_method, "average" )
240 || !strcmp( psz_method, "combine-fields" ) )
242 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
243 p_vout->p_sys->b_double_rate = 0;
245 else if( !strcmp( psz_method, "bob" )
246 || !strcmp( psz_method, "progressive-scan" ) )
248 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
249 p_vout->p_sys->b_double_rate = 1;
251 else if( !strcmp( psz_method, "linear" ) )
253 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
254 p_vout->p_sys->b_double_rate = 1;
258 msg_Err( p_vout, "no valid deinterlace mode provided, "
259 "using \"discard\"" );
262 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
265 /*****************************************************************************
266 * Init: initialize Deinterlace video thread output method
267 *****************************************************************************/
268 static int Init( vout_thread_t *p_vout )
273 I_OUTPUTPICTURES = 0;
275 /* Initialize the output structure, full of directbuffers since we want
276 * the decoder to output directly to our structures. */
277 switch( p_vout->render.i_chroma )
279 case VLC_FOURCC('I','4','2','0'):
280 case VLC_FOURCC('I','Y','U','V'):
281 case VLC_FOURCC('Y','V','1','2'):
282 case VLC_FOURCC('I','4','2','2'):
283 p_vout->output.i_chroma = p_vout->render.i_chroma;
284 p_vout->output.i_width = p_vout->render.i_width;
285 p_vout->output.i_height = p_vout->render.i_height;
286 p_vout->output.i_aspect = p_vout->render.i_aspect;
290 return VLC_EGENERIC; /* unknown chroma */
294 /* Try to open the real video output */
295 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
297 if( p_vout->p_sys->p_vout == NULL )
299 /* Everything failed */
300 msg_Err( p_vout, "cannot open vout, aborting" );
305 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
307 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
309 ADD_PARENT_CALLBACKS( SendEventsToChild );
314 /*****************************************************************************
315 * SpawnRealVout: spawn the real video output.
316 *****************************************************************************/
317 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
319 vout_thread_t *p_real_vout = NULL;
321 msg_Dbg( p_vout, "spawning the real video output" );
323 switch( p_vout->render.i_chroma )
325 case VLC_FOURCC('I','4','2','0'):
326 case VLC_FOURCC('I','Y','U','V'):
327 case VLC_FOURCC('Y','V','1','2'):
328 switch( p_vout->p_sys->i_mode )
330 case DEINTERLACE_MEAN:
331 case DEINTERLACE_DISCARD:
334 p_vout->output.i_width, p_vout->output.i_height / 2,
335 p_vout->output.i_chroma, p_vout->output.i_aspect );
338 case DEINTERLACE_BOB:
339 case DEINTERLACE_BLEND:
340 case DEINTERLACE_LINEAR:
343 p_vout->output.i_width, p_vout->output.i_height,
344 p_vout->output.i_chroma, p_vout->output.i_aspect );
349 case VLC_FOURCC('I','4','2','2'):
352 p_vout->output.i_width, p_vout->output.i_height,
353 VLC_FOURCC('I','4','2','0'), p_vout->output.i_aspect );
363 /*****************************************************************************
364 * End: terminate Deinterlace video thread output method
365 *****************************************************************************/
366 static void End( vout_thread_t *p_vout )
370 /* Free the fake output buffers we allocated */
371 for( i_index = I_OUTPUTPICTURES ; i_index ; )
374 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
378 /*****************************************************************************
379 * Destroy: destroy Deinterlace video thread output method
380 *****************************************************************************
381 * Terminate an output method created by DeinterlaceCreateOutputMethod
382 *****************************************************************************/
383 static void Destroy( vlc_object_t *p_this )
385 vout_thread_t *p_vout = (vout_thread_t *)p_this;
387 if( p_vout->p_sys->p_vout )
389 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
390 vlc_object_detach( p_vout->p_sys->p_vout );
391 vout_Destroy( p_vout->p_sys->p_vout );
394 DEL_PARENT_CALLBACKS( SendEventsToChild );
396 free( p_vout->p_sys );
399 /*****************************************************************************
400 * Render: displays previously rendered output
401 *****************************************************************************
402 * This function send the currently rendered image to Deinterlace image,
403 * waits until it is displayed and switch the two rendering buffers, preparing
405 *****************************************************************************/
406 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
408 picture_t *pp_outpic[2];
410 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
412 /* Get a new picture */
413 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
417 if( p_vout->b_die || p_vout->b_error )
419 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
422 msleep( VOUT_OUTMEM_SLEEP );
425 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
427 /* If we are using double rate, get an additional new picture */
428 if( p_vout->p_sys->b_double_rate )
430 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
434 if( p_vout->b_die || p_vout->b_error )
436 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
437 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
440 msleep( VOUT_OUTMEM_SLEEP );
443 /* 20ms is a bit arbitrary, but it's only for the first image we get */
444 if( !p_vout->p_sys->last_date )
446 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
447 p_pic->date + 20000 );
451 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
452 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
454 p_vout->p_sys->last_date = p_pic->date;
457 switch( p_vout->p_sys->i_mode )
459 case DEINTERLACE_DISCARD:
460 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
461 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
464 case DEINTERLACE_BOB:
465 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
466 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
467 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
468 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
471 case DEINTERLACE_LINEAR:
472 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
473 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
474 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
475 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
478 case DEINTERLACE_MEAN:
479 RenderMean( p_vout, pp_outpic[0], p_pic );
480 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
483 case DEINTERLACE_BLEND:
484 RenderBlend( p_vout, pp_outpic[0], p_pic );
485 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
489 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
492 /*****************************************************************************
493 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
494 *****************************************************************************/
495 static void RenderDiscard( vout_thread_t *p_vout,
496 picture_t *p_outpic, picture_t *p_pic, int i_field )
500 /* Copy image and skip lines */
501 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
503 uint8_t *p_in, *p_out_end, *p_out;
506 p_in = p_pic->p[i_plane].p_pixels
507 + i_field * p_pic->p[i_plane].i_pitch;
509 p_out = p_outpic->p[i_plane].p_pixels;
510 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
511 * p_outpic->p[i_plane].i_visible_lines;
513 switch( p_vout->render.i_chroma )
515 case VLC_FOURCC('I','4','2','0'):
516 case VLC_FOURCC('I','Y','U','V'):
517 case VLC_FOURCC('Y','V','1','2'):
519 for( ; p_out < p_out_end ; )
521 p_vout->p_vlc->pf_memcpy( p_out, p_in,
522 p_pic->p[i_plane].i_pitch );
524 p_out += p_pic->p[i_plane].i_pitch;
525 p_in += 2 * p_pic->p[i_plane].i_pitch;
529 case VLC_FOURCC('I','4','2','2'):
531 i_increment = 2 * p_pic->p[i_plane].i_pitch;
533 if( i_plane == Y_PLANE )
535 for( ; p_out < p_out_end ; )
537 p_vout->p_vlc->pf_memcpy( p_out, p_in,
538 p_pic->p[i_plane].i_pitch );
539 p_out += p_pic->p[i_plane].i_pitch;
540 p_vout->p_vlc->pf_memcpy( p_out, p_in,
541 p_pic->p[i_plane].i_pitch );
542 p_out += p_pic->p[i_plane].i_pitch;
548 for( ; p_out < p_out_end ; )
550 p_vout->p_vlc->pf_memcpy( p_out, p_in,
551 p_pic->p[i_plane].i_pitch );
552 p_out += p_pic->p[i_plane].i_pitch;
564 /*****************************************************************************
565 * RenderBob: renders a BOB picture - simple copy
566 *****************************************************************************/
567 static void RenderBob( vout_thread_t *p_vout,
568 picture_t *p_outpic, picture_t *p_pic, int i_field )
572 /* Copy image and skip lines */
573 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
575 uint8_t *p_in, *p_out_end, *p_out;
577 p_in = p_pic->p[i_plane].p_pixels;
578 p_out = p_outpic->p[i_plane].p_pixels;
579 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
580 * p_outpic->p[i_plane].i_visible_lines;
582 switch( p_vout->render.i_chroma )
584 case VLC_FOURCC('I','4','2','0'):
585 case VLC_FOURCC('I','Y','U','V'):
586 case VLC_FOURCC('Y','V','1','2'):
587 /* For BOTTOM field we need to add the first line */
590 p_vout->p_vlc->pf_memcpy( p_out, p_in,
591 p_pic->p[i_plane].i_pitch );
592 p_in += p_pic->p[i_plane].i_pitch;
593 p_out += p_pic->p[i_plane].i_pitch;
596 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
598 for( ; p_out < p_out_end ; )
600 p_vout->p_vlc->pf_memcpy( p_out, p_in,
601 p_pic->p[i_plane].i_pitch );
603 p_out += p_pic->p[i_plane].i_pitch;
605 p_vout->p_vlc->pf_memcpy( p_out, p_in,
606 p_pic->p[i_plane].i_pitch );
608 p_in += 2 * p_pic->p[i_plane].i_pitch;
609 p_out += p_pic->p[i_plane].i_pitch;
612 p_vout->p_vlc->pf_memcpy( p_out, p_in,
613 p_pic->p[i_plane].i_pitch );
615 /* For TOP field we need to add the last line */
618 p_in += p_pic->p[i_plane].i_pitch;
619 p_out += p_pic->p[i_plane].i_pitch;
620 p_vout->p_vlc->pf_memcpy( p_out, p_in,
621 p_pic->p[i_plane].i_pitch );
625 case VLC_FOURCC('I','4','2','2'):
626 /* For BOTTOM field we need to add the first line */
629 p_vout->p_vlc->pf_memcpy( p_out, p_in,
630 p_pic->p[i_plane].i_pitch );
631 p_in += p_pic->p[i_plane].i_pitch;
632 p_out += p_pic->p[i_plane].i_pitch;
635 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
637 if( i_plane == Y_PLANE )
639 for( ; p_out < p_out_end ; )
641 p_vout->p_vlc->pf_memcpy( p_out, p_in,
642 p_pic->p[i_plane].i_pitch );
644 p_out += p_pic->p[i_plane].i_pitch;
646 p_vout->p_vlc->pf_memcpy( p_out, p_in,
647 p_pic->p[i_plane].i_pitch );
649 p_in += 2 * p_pic->p[i_plane].i_pitch;
650 p_out += p_pic->p[i_plane].i_pitch;
655 for( ; p_out < p_out_end ; )
657 p_vout->p_vlc->pf_memcpy( p_out, p_in,
658 p_pic->p[i_plane].i_pitch );
660 p_out += p_pic->p[i_plane].i_pitch;
661 p_in += 2 * p_pic->p[i_plane].i_pitch;
665 p_vout->p_vlc->pf_memcpy( p_out, p_in,
666 p_pic->p[i_plane].i_pitch );
668 /* For TOP field we need to add the last line */
671 p_in += p_pic->p[i_plane].i_pitch;
672 p_out += p_pic->p[i_plane].i_pitch;
673 p_vout->p_vlc->pf_memcpy( p_out, p_in,
674 p_pic->p[i_plane].i_pitch );
681 #define Merge p_vout->p_sys->pf_merge
682 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
684 /*****************************************************************************
685 * RenderLinear: BOB with linear interpolation
686 *****************************************************************************/
687 static void RenderLinear( vout_thread_t *p_vout,
688 picture_t *p_outpic, picture_t *p_pic, int i_field )
692 /* Copy image and skip lines */
693 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
695 uint8_t *p_in, *p_out_end, *p_out;
697 p_in = p_pic->p[i_plane].p_pixels;
698 p_out = p_outpic->p[i_plane].p_pixels;
699 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
700 * p_outpic->p[i_plane].i_visible_lines;
702 /* For BOTTOM field we need to add the first line */
705 p_vout->p_vlc->pf_memcpy( p_out, p_in,
706 p_pic->p[i_plane].i_pitch );
707 p_in += p_pic->p[i_plane].i_pitch;
708 p_out += p_pic->p[i_plane].i_pitch;
711 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
713 for( ; p_out < p_out_end ; )
715 p_vout->p_vlc->pf_memcpy( p_out, p_in,
716 p_pic->p[i_plane].i_pitch );
718 p_out += p_pic->p[i_plane].i_pitch;
720 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
721 p_pic->p[i_plane].i_pitch );
723 p_in += 2 * p_pic->p[i_plane].i_pitch;
724 p_out += p_pic->p[i_plane].i_pitch;
727 p_vout->p_vlc->pf_memcpy( p_out, p_in,
728 p_pic->p[i_plane].i_pitch );
730 /* For TOP field we need to add the last line */
733 p_in += p_pic->p[i_plane].i_pitch;
734 p_out += p_pic->p[i_plane].i_pitch;
735 p_vout->p_vlc->pf_memcpy( p_out, p_in,
736 p_pic->p[i_plane].i_pitch );
742 static void RenderMean( vout_thread_t *p_vout,
743 picture_t *p_outpic, picture_t *p_pic )
747 /* Copy image and skip lines */
748 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
750 uint8_t *p_in, *p_out_end, *p_out;
752 p_in = p_pic->p[i_plane].p_pixels;
754 p_out = p_outpic->p[i_plane].p_pixels;
755 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
756 * p_outpic->p[i_plane].i_visible_lines;
758 /* All lines: mean value */
759 for( ; p_out < p_out_end ; )
761 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
762 p_pic->p[i_plane].i_pitch );
764 p_out += p_pic->p[i_plane].i_pitch;
765 p_in += 2 * p_pic->p[i_plane].i_pitch;
771 static void RenderBlend( vout_thread_t *p_vout,
772 picture_t *p_outpic, picture_t *p_pic )
776 /* Copy image and skip lines */
777 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
779 uint8_t *p_in, *p_out_end, *p_out;
781 p_in = p_pic->p[i_plane].p_pixels;
783 p_out = p_outpic->p[i_plane].p_pixels;
784 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
785 * p_outpic->p[i_plane].i_visible_lines;
787 switch( p_vout->render.i_chroma )
789 case VLC_FOURCC('I','4','2','0'):
790 case VLC_FOURCC('I','Y','U','V'):
791 case VLC_FOURCC('Y','V','1','2'):
792 /* First line: simple copy */
793 p_vout->p_vlc->pf_memcpy( p_out, p_in,
794 p_pic->p[i_plane].i_pitch );
795 p_out += p_pic->p[i_plane].i_pitch;
797 /* Remaining lines: mean value */
798 for( ; p_out < p_out_end ; )
800 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
801 p_pic->p[i_plane].i_pitch );
803 p_out += p_pic->p[i_plane].i_pitch;
804 p_in += p_pic->p[i_plane].i_pitch;
808 case VLC_FOURCC('I','4','2','2'):
809 /* First line: simple copy */
810 p_vout->p_vlc->pf_memcpy( p_out, p_in,
811 p_pic->p[i_plane].i_pitch );
812 p_out += p_pic->p[i_plane].i_pitch;
814 /* Remaining lines: mean value */
815 if( i_plane == Y_PLANE )
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 += p_pic->p[i_plane].i_pitch;
829 for( ; p_out < p_out_end ; )
831 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
832 p_pic->p[i_plane].i_pitch );
834 p_out += p_pic->p[i_plane].i_pitch;
835 p_in += 2*p_pic->p[i_plane].i_pitch;
846 static void MergeGeneric( void *_p_dest, const void *_p_s1,
847 const void *_p_s2, size_t i_bytes )
849 uint8_t* p_dest = (uint8_t*)_p_dest;
850 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
851 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
852 uint8_t* p_end = p_dest + i_bytes - 8;
854 while( p_dest < p_end )
856 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
857 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
858 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
859 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
860 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
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;
868 while( p_dest < p_end )
870 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
874 #if defined(CAN_COMPILE_MMXEXT)
875 static void MergeMMX( void *_p_dest, const void *_p_s1, const void *_p_s2,
878 uint8_t* p_dest = (uint8_t*)_p_dest;
879 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
880 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
881 uint8_t* p_end = p_dest + i_bytes - 8;
882 while( p_dest < p_end )
884 __asm__ __volatile__( "movq %2,%%mm1;"
886 "movq %%mm1, %0" :"=m" (*p_dest):
896 while( p_dest < p_end )
898 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
903 #if defined(CAN_COMPILE_SSE)
904 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
907 uint8_t* p_dest = (uint8_t*)_p_dest;
908 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
909 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
911 while( (int)p_s1 % 16 )
913 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
915 p_end = p_dest + i_bytes - 16;
916 while( p_dest < p_end )
918 __asm__ __volatile__( "movdqu %2,%%xmm1;"
920 "movdqu %%xmm1, %0" :"=m" (*p_dest):
930 while( p_dest < p_end )
932 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
937 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
938 static void EndMMX( void )
940 __asm__ __volatile__( "emms" :: );
944 #ifdef CAN_COMPILE_C_ALTIVEC
945 static void MergeAltivec( void *_p_dest, const void *_p_s1,
946 const void *_p_s2, size_t i_bytes )
948 uint8_t *p_dest = (uint8_t *)_p_dest;
949 uint8_t *p_s1 = (uint8_t *)_p_s1;
950 uint8_t *p_s2 = (uint8_t *)_p_s2;
951 uint8_t *p_end = p_dest + i_bytes - 15;
953 /* Use C until the first 16-bytes aligned destination pixel */
954 while( (int)p_dest & 0xF )
956 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
959 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
961 /* Unaligned source */
962 vector unsigned char s1v, s2v, destv;
963 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
964 vector unsigned char perm1v, perm2v;
966 perm1v = vec_lvsl( 0, p_s1 );
967 perm2v = vec_lvsl( 0, p_s2 );
968 s1oldv = vec_ld( 0, p_s1 );
969 s2oldv = vec_ld( 0, p_s2 );
971 while( p_dest < p_end )
973 s1newv = vec_ld( 16, p_s1 );
974 s2newv = vec_ld( 16, p_s2 );
975 s1v = vec_perm( s1oldv, s1newv, perm1v );
976 s2v = vec_perm( s2oldv, s2newv, perm2v );
979 destv = vec_avg( s1v, s2v );
980 vec_st( destv, 0, p_dest );
990 vector unsigned char s1v, s2v, destv;
992 while( p_dest < p_end )
994 s1v = vec_ld( 0, p_s1 );
995 s2v = vec_ld( 0, p_s2 );
996 destv = vec_avg( s1v, s2v );
997 vec_st( destv, 0, p_dest );
1007 while( p_dest < p_end )
1009 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1014 /*****************************************************************************
1015 * SendEvents: forward mouse and keyboard events to the parent p_vout
1016 *****************************************************************************/
1017 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1018 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1020 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1021 vlc_value_t sentval = newval;
1023 if( !strcmp( psz_var, "mouse-y" ) )
1025 switch( p_vout->p_sys->i_mode )
1027 case DEINTERLACE_MEAN:
1028 case DEINTERLACE_DISCARD:
1034 var_Set( p_vout, psz_var, sentval );
1039 /*****************************************************************************
1040 * FilterCallback: called when changing the deinterlace method on the fly.
1041 *****************************************************************************/
1042 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1043 vlc_value_t oldval, vlc_value_t newval,
1046 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1047 int i_old_mode = p_vout->p_sys->i_mode;
1049 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1051 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1053 SetFilterMethod( p_vout, newval.psz_string );
1055 switch( p_vout->render.i_chroma )
1057 case VLC_FOURCC('I','4','2','2'):
1058 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1062 case VLC_FOURCC('I','4','2','0'):
1063 case VLC_FOURCC('I','Y','U','V'):
1064 case VLC_FOURCC('Y','V','1','2'):
1065 switch( p_vout->p_sys->i_mode )
1067 case DEINTERLACE_MEAN:
1068 case DEINTERLACE_DISCARD:
1069 if( ( i_old_mode == DEINTERLACE_MEAN )
1070 || ( i_old_mode == DEINTERLACE_DISCARD ) )
1072 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1077 case DEINTERLACE_BOB:
1078 case DEINTERLACE_BLEND:
1079 case DEINTERLACE_LINEAR:
1080 if( ( i_old_mode == DEINTERLACE_BOB )
1081 || ( i_old_mode == DEINTERLACE_BLEND )
1082 || ( i_old_mode == DEINTERLACE_LINEAR ) )
1084 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1095 /* We need to kill the old vout */
1097 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1099 vlc_object_detach( p_vout->p_sys->p_vout );
1100 vout_Destroy( p_vout->p_sys->p_vout );
1102 /* Try to open a new video output */
1103 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
1105 if( p_vout->p_sys->p_vout == NULL )
1107 /* Everything failed */
1108 msg_Err( p_vout, "cannot open vout, aborting" );
1110 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1111 return VLC_EGENERIC;
1114 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1116 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1120 /*****************************************************************************
1121 * SendEventsToChild: forward events to the child/children vout
1122 *****************************************************************************/
1123 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
1124 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1126 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1127 var_Set( p_vout->p_sys->p_vout, psz_var, newval );