1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
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() */
34 #include "vlc_filter.h"
40 #ifdef CAN_COMPILE_MMXEXT
44 #include "filter_common.h"
46 #define DEINTERLACE_DISCARD 1
47 #define DEINTERLACE_MEAN 2
48 #define DEINTERLACE_BLEND 3
49 #define DEINTERLACE_BOB 4
50 #define DEINTERLACE_LINEAR 5
51 #define DEINTERLACE_X 6
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Create ( vlc_object_t * );
57 static void Destroy ( vlc_object_t * );
59 static int Init ( vout_thread_t * );
60 static void End ( vout_thread_t * );
61 static void Render ( vout_thread_t *, picture_t * );
63 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
64 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
65 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
66 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
67 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
68 static void RenderX ( vout_thread_t *, picture_t *, picture_t * );
70 static void MergeGeneric ( void *, const void *, const void *, size_t );
71 #if defined(CAN_COMPILE_C_ALTIVEC)
72 static void MergeAltivec ( void *, const void *, const void *, size_t );
74 #if defined(CAN_COMPILE_MMXEXT)
75 static void MergeMMX ( void *, const void *, const void *, size_t );
77 #if defined(CAN_COMPILE_SSE)
78 static void MergeSSE2 ( void *, const void *, const void *, size_t );
80 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
81 static void EndMMX ( void );
84 static int SendEvents ( vlc_object_t *, char const *,
85 vlc_value_t, vlc_value_t, void * );
87 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
88 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
90 static int OpenFilter( vlc_object_t *p_this );
91 static void CloseFilter( vlc_object_t *p_this );
93 /*****************************************************************************
95 *****************************************************************************/
96 static int FilterCallback ( vlc_object_t *, char const *,
97 vlc_value_t, vlc_value_t, void * );
99 /*****************************************************************************
101 *****************************************************************************/
102 #define MODE_TEXT N_("Deinterlace mode")
103 #define MODE_LONGTEXT N_("You can choose the default deinterlace mode")
105 #define FILTER_CFG_PREFIX "sout-deinterlace-"
107 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", "x" };
108 static char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
109 N_("Bob"), N_("Linear"), "X" };
112 set_description( _("Deinterlacing video filter") );
113 set_shortname( N_("Deinterlace" ));
114 set_capability( "video filter", 0 );
115 set_category( CAT_VIDEO );
116 set_subcategory( SUBCAT_VIDEO_VFILTER );
118 set_section( N_("Display"),NULL);
119 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
120 MODE_LONGTEXT, VLC_FALSE );
121 change_string_list( mode_list, mode_list_text, 0 );
123 add_shortcut( "deinterlace" );
124 set_callbacks( Create, Destroy );
127 set_capability( "video filter2", 0 );
128 set_section( N_("Streamming"),NULL);
129 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, MODE_TEXT,
130 MODE_LONGTEXT, VLC_FALSE );
131 change_string_list( mode_list, mode_list_text, 0 );
132 set_callbacks( OpenFilter, CloseFilter );
135 static const char *ppsz_filter_options[] = {
139 /*****************************************************************************
140 * vout_sys_t: Deinterlace video output method descriptor
141 *****************************************************************************
142 * This structure is part of the video output thread descriptor.
143 * It describes the Deinterlace specific properties of an output thread.
144 *****************************************************************************/
147 int i_mode; /* Deinterlace mode */
148 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
153 vout_thread_t *p_vout;
155 vlc_mutex_t filter_lock;
157 void (*pf_merge) ( void *, const void *, const void *, size_t );
158 void (*pf_end_merge) ( void );
161 /*****************************************************************************
162 * Control: control facility for the vout (forwards to child vout)
163 *****************************************************************************/
164 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
166 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
169 /*****************************************************************************
170 * Create: allocates Deinterlace video thread output method
171 *****************************************************************************
172 * This function allocates and initializes a Deinterlace vout method.
173 *****************************************************************************/
174 static int Create( vlc_object_t *p_this )
176 vout_thread_t *p_vout = (vout_thread_t *)p_this;
179 /* Allocate structure */
180 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
181 if( p_vout->p_sys == NULL )
183 msg_Err( p_vout, "out of memory" );
187 p_vout->pf_init = Init;
188 p_vout->pf_end = End;
189 p_vout->pf_manage = NULL;
190 p_vout->pf_render = Render;
191 p_vout->pf_display = NULL;
192 p_vout->pf_control = Control;
194 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
195 p_vout->p_sys->b_double_rate = VLC_FALSE;
196 p_vout->p_sys->last_date = 0;
197 p_vout->p_sys->p_vout = 0;
198 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
200 #if defined(CAN_COMPILE_C_ALTIVEC)
201 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
203 p_vout->p_sys->pf_merge = MergeAltivec;
204 p_vout->p_sys->pf_end_merge = NULL;
208 #if defined(CAN_COMPILE_SSE)
209 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2 )
211 p_vout->p_sys->pf_merge = MergeSSE2;
212 p_vout->p_sys->pf_end_merge = EndMMX;
216 #if defined(CAN_COMPILE_MMXEXT)
217 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
219 p_vout->p_sys->pf_merge = MergeMMX;
220 p_vout->p_sys->pf_end_merge = EndMMX;
225 p_vout->p_sys->pf_merge = MergeGeneric;
226 p_vout->p_sys->pf_end_merge = NULL;
229 /* Look what method was requested */
230 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
231 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
233 if( val.psz_string == NULL )
235 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
236 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
238 val.psz_string = strdup( "discard" );
241 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
243 SetFilterMethod( p_vout, val.psz_string );
245 free( val.psz_string );
250 /*****************************************************************************
251 * SetFilterMethod: setup the deinterlace method to use.
252 *****************************************************************************/
253 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
255 if( !strcmp( psz_method, "discard" ) )
257 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
258 p_vout->p_sys->b_double_rate = VLC_FALSE;
260 else if( !strcmp( psz_method, "mean" ) )
262 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
263 p_vout->p_sys->b_double_rate = VLC_FALSE;
265 else if( !strcmp( psz_method, "blend" )
266 || !strcmp( psz_method, "average" )
267 || !strcmp( psz_method, "combine-fields" ) )
269 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
270 p_vout->p_sys->b_double_rate = VLC_FALSE;
272 else if( !strcmp( psz_method, "bob" )
273 || !strcmp( psz_method, "progressive-scan" ) )
275 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
276 p_vout->p_sys->b_double_rate = VLC_TRUE;
278 else if( !strcmp( psz_method, "linear" ) )
280 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
281 p_vout->p_sys->b_double_rate = VLC_TRUE;
283 else if( !strcmp( psz_method, "x" ) )
285 p_vout->p_sys->i_mode = DEINTERLACE_X;
286 p_vout->p_sys->b_double_rate = VLC_FALSE;
290 msg_Err( p_vout, "no valid deinterlace mode provided, "
291 "using \"discard\"" );
294 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
297 /*****************************************************************************
298 * Init: initialize Deinterlace video thread output method
299 *****************************************************************************/
300 static int Init( vout_thread_t *p_vout )
305 I_OUTPUTPICTURES = 0;
307 /* Initialize the output structure, full of directbuffers since we want
308 * the decoder to output directly to our structures. */
309 switch( p_vout->render.i_chroma )
311 case VLC_FOURCC('I','4','2','0'):
312 case VLC_FOURCC('I','Y','U','V'):
313 case VLC_FOURCC('Y','V','1','2'):
314 case VLC_FOURCC('I','4','2','2'):
315 p_vout->output.i_chroma = p_vout->render.i_chroma;
316 p_vout->output.i_width = p_vout->render.i_width;
317 p_vout->output.i_height = p_vout->render.i_height;
318 p_vout->output.i_aspect = p_vout->render.i_aspect;
322 return VLC_EGENERIC; /* unknown chroma */
326 /* Try to open the real video output */
327 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
329 if( p_vout->p_sys->p_vout == NULL )
331 /* Everything failed */
332 msg_Err( p_vout, "cannot open vout, aborting" );
337 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
339 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
341 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
343 ADD_PARENT_CALLBACKS( SendEventsToChild );
348 /*****************************************************************************
349 * SpawnRealVout: spawn the real video output.
350 *****************************************************************************/
351 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
353 vout_thread_t *p_real_vout = NULL;
354 video_format_t fmt = {0};
356 msg_Dbg( p_vout, "spawning the real video output" );
358 fmt.i_width = fmt.i_visible_width = p_vout->output.i_width;
359 fmt.i_height = fmt.i_visible_height = p_vout->output.i_height;
360 fmt.i_x_offset = fmt.i_y_offset = 0;
361 fmt.i_chroma = p_vout->output.i_chroma;
362 fmt.i_aspect = p_vout->output.i_aspect;
363 fmt.i_sar_num = p_vout->output.i_aspect * fmt.i_height / fmt.i_width;
364 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
366 switch( p_vout->render.i_chroma )
368 case VLC_FOURCC('I','4','2','0'):
369 case VLC_FOURCC('I','Y','U','V'):
370 case VLC_FOURCC('Y','V','1','2'):
371 switch( p_vout->p_sys->i_mode )
373 case DEINTERLACE_MEAN:
374 case DEINTERLACE_DISCARD:
375 fmt.i_height = fmt.i_visible_height = p_vout->output.i_height / 2;
376 p_real_vout = vout_Create( p_vout, &fmt );
379 case DEINTERLACE_BOB:
380 case DEINTERLACE_BLEND:
381 case DEINTERLACE_LINEAR:
383 p_real_vout = vout_Create( p_vout, &fmt );
388 case VLC_FOURCC('I','4','2','2'):
389 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
390 p_real_vout = vout_Create( p_vout, &fmt );
400 /*****************************************************************************
401 * End: terminate Deinterlace video thread output method
402 *****************************************************************************/
403 static void End( vout_thread_t *p_vout )
407 /* Free the fake output buffers we allocated */
408 for( i_index = I_OUTPUTPICTURES ; i_index ; )
411 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
414 if( p_vout->p_sys->p_vout )
416 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
417 vlc_object_detach( p_vout->p_sys->p_vout );
418 vout_Destroy( p_vout->p_sys->p_vout );
421 DEL_PARENT_CALLBACKS( SendEventsToChild );
424 /*****************************************************************************
425 * Destroy: destroy Deinterlace video thread output method
426 *****************************************************************************
427 * Terminate an output method created by DeinterlaceCreateOutputMethod
428 *****************************************************************************/
429 static void Destroy( vlc_object_t *p_this )
431 vout_thread_t *p_vout = (vout_thread_t *)p_this;
432 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
433 free( p_vout->p_sys );
436 /*****************************************************************************
437 * Render: displays previously rendered output
438 *****************************************************************************
439 * This function send the currently rendered image to Deinterlace image,
440 * waits until it is displayed and switch the two rendering buffers, preparing
442 *****************************************************************************/
443 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
445 picture_t *pp_outpic[2];
447 pp_outpic[0] = pp_outpic[1] = NULL;
449 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
451 /* Get a new picture */
452 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
456 if( p_vout->b_die || p_vout->b_error )
458 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
461 msleep( VOUT_OUTMEM_SLEEP );
464 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
466 /* If we are using double rate, get an additional new picture */
467 if( p_vout->p_sys->b_double_rate )
469 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
473 if( p_vout->b_die || p_vout->b_error )
475 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
476 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
479 msleep( VOUT_OUTMEM_SLEEP );
482 /* 20ms is a bit arbitrary, but it's only for the first image we get */
483 if( !p_vout->p_sys->last_date )
485 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
486 p_pic->date + 20000 );
490 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
491 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
493 p_vout->p_sys->last_date = p_pic->date;
496 switch( p_vout->p_sys->i_mode )
498 case DEINTERLACE_DISCARD:
499 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
500 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
503 case DEINTERLACE_BOB:
504 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
505 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
506 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
507 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
510 case DEINTERLACE_LINEAR:
511 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
512 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
513 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
514 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
517 case DEINTERLACE_MEAN:
518 RenderMean( p_vout, pp_outpic[0], p_pic );
519 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
522 case DEINTERLACE_BLEND:
523 RenderBlend( p_vout, pp_outpic[0], p_pic );
524 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
528 RenderX( p_vout, pp_outpic[0], p_pic );
529 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
532 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
535 /*****************************************************************************
536 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
537 *****************************************************************************/
538 static void RenderDiscard( vout_thread_t *p_vout,
539 picture_t *p_outpic, picture_t *p_pic, int i_field )
543 /* Copy image and skip lines */
544 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
546 uint8_t *p_in, *p_out_end, *p_out;
549 p_in = p_pic->p[i_plane].p_pixels
550 + i_field * p_pic->p[i_plane].i_pitch;
552 p_out = p_outpic->p[i_plane].p_pixels;
553 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
554 * p_outpic->p[i_plane].i_visible_lines;
556 switch( p_vout->render.i_chroma )
558 case VLC_FOURCC('I','4','2','0'):
559 case VLC_FOURCC('I','Y','U','V'):
560 case VLC_FOURCC('Y','V','1','2'):
562 for( ; p_out < p_out_end ; )
564 p_vout->p_vlc->pf_memcpy( p_out, p_in,
565 p_pic->p[i_plane].i_pitch );
567 p_out += p_pic->p[i_plane].i_pitch;
568 p_in += 2 * p_pic->p[i_plane].i_pitch;
572 case VLC_FOURCC('I','4','2','2'):
574 i_increment = 2 * p_pic->p[i_plane].i_pitch;
576 if( i_plane == Y_PLANE )
578 for( ; p_out < p_out_end ; )
580 p_vout->p_vlc->pf_memcpy( p_out, p_in,
581 p_pic->p[i_plane].i_pitch );
582 p_out += p_pic->p[i_plane].i_pitch;
583 p_vout->p_vlc->pf_memcpy( p_out, p_in,
584 p_pic->p[i_plane].i_pitch );
585 p_out += p_pic->p[i_plane].i_pitch;
591 for( ; p_out < p_out_end ; )
593 p_vout->p_vlc->pf_memcpy( p_out, p_in,
594 p_pic->p[i_plane].i_pitch );
595 p_out += p_pic->p[i_plane].i_pitch;
607 /*****************************************************************************
608 * RenderBob: renders a BOB picture - simple copy
609 *****************************************************************************/
610 static void RenderBob( vout_thread_t *p_vout,
611 picture_t *p_outpic, picture_t *p_pic, int i_field )
615 /* Copy image and skip lines */
616 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
618 uint8_t *p_in, *p_out_end, *p_out;
620 p_in = p_pic->p[i_plane].p_pixels;
621 p_out = p_outpic->p[i_plane].p_pixels;
622 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
623 * p_outpic->p[i_plane].i_visible_lines;
625 switch( p_vout->render.i_chroma )
627 case VLC_FOURCC('I','4','2','0'):
628 case VLC_FOURCC('I','Y','U','V'):
629 case VLC_FOURCC('Y','V','1','2'):
630 /* For BOTTOM field we need to add the first line */
633 p_vout->p_vlc->pf_memcpy( p_out, p_in,
634 p_pic->p[i_plane].i_pitch );
635 p_in += p_pic->p[i_plane].i_pitch;
636 p_out += p_pic->p[i_plane].i_pitch;
639 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
641 for( ; p_out < p_out_end ; )
643 p_vout->p_vlc->pf_memcpy( p_out, p_in,
644 p_pic->p[i_plane].i_pitch );
646 p_out += p_pic->p[i_plane].i_pitch;
648 p_vout->p_vlc->pf_memcpy( p_out, p_in,
649 p_pic->p[i_plane].i_pitch );
651 p_in += 2 * p_pic->p[i_plane].i_pitch;
652 p_out += p_pic->p[i_plane].i_pitch;
655 p_vout->p_vlc->pf_memcpy( p_out, p_in,
656 p_pic->p[i_plane].i_pitch );
658 /* For TOP field we need to add the last line */
661 p_in += p_pic->p[i_plane].i_pitch;
662 p_out += p_pic->p[i_plane].i_pitch;
663 p_vout->p_vlc->pf_memcpy( p_out, p_in,
664 p_pic->p[i_plane].i_pitch );
668 case VLC_FOURCC('I','4','2','2'):
669 /* For BOTTOM field we need to add the first line */
672 p_vout->p_vlc->pf_memcpy( p_out, p_in,
673 p_pic->p[i_plane].i_pitch );
674 p_in += p_pic->p[i_plane].i_pitch;
675 p_out += p_pic->p[i_plane].i_pitch;
678 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
680 if( i_plane == Y_PLANE )
682 for( ; p_out < p_out_end ; )
684 p_vout->p_vlc->pf_memcpy( p_out, p_in,
685 p_pic->p[i_plane].i_pitch );
687 p_out += p_pic->p[i_plane].i_pitch;
689 p_vout->p_vlc->pf_memcpy( p_out, p_in,
690 p_pic->p[i_plane].i_pitch );
692 p_in += 2 * p_pic->p[i_plane].i_pitch;
693 p_out += p_pic->p[i_plane].i_pitch;
698 for( ; p_out < p_out_end ; )
700 p_vout->p_vlc->pf_memcpy( p_out, p_in,
701 p_pic->p[i_plane].i_pitch );
703 p_out += p_pic->p[i_plane].i_pitch;
704 p_in += 2 * p_pic->p[i_plane].i_pitch;
708 p_vout->p_vlc->pf_memcpy( p_out, p_in,
709 p_pic->p[i_plane].i_pitch );
711 /* For TOP field we need to add the last line */
714 p_in += p_pic->p[i_plane].i_pitch;
715 p_out += p_pic->p[i_plane].i_pitch;
716 p_vout->p_vlc->pf_memcpy( p_out, p_in,
717 p_pic->p[i_plane].i_pitch );
724 #define Merge p_vout->p_sys->pf_merge
725 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
727 /*****************************************************************************
728 * RenderLinear: BOB with linear interpolation
729 *****************************************************************************/
730 static void RenderLinear( vout_thread_t *p_vout,
731 picture_t *p_outpic, picture_t *p_pic, int i_field )
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;
741 p_out = p_outpic->p[i_plane].p_pixels;
742 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
743 * p_outpic->p[i_plane].i_visible_lines;
745 /* For BOTTOM field we need to add the first line */
748 p_vout->p_vlc->pf_memcpy( p_out, p_in,
749 p_pic->p[i_plane].i_pitch );
750 p_in += p_pic->p[i_plane].i_pitch;
751 p_out += p_pic->p[i_plane].i_pitch;
754 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
756 for( ; p_out < p_out_end ; )
758 p_vout->p_vlc->pf_memcpy( p_out, p_in,
759 p_pic->p[i_plane].i_pitch );
761 p_out += p_pic->p[i_plane].i_pitch;
763 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
764 p_pic->p[i_plane].i_pitch );
766 p_in += 2 * p_pic->p[i_plane].i_pitch;
767 p_out += p_pic->p[i_plane].i_pitch;
770 p_vout->p_vlc->pf_memcpy( p_out, p_in,
771 p_pic->p[i_plane].i_pitch );
773 /* For TOP field we need to add the last line */
776 p_in += p_pic->p[i_plane].i_pitch;
777 p_out += p_pic->p[i_plane].i_pitch;
778 p_vout->p_vlc->pf_memcpy( p_out, p_in,
779 p_pic->p[i_plane].i_pitch );
785 static void RenderMean( vout_thread_t *p_vout,
786 picture_t *p_outpic, picture_t *p_pic )
790 /* Copy image and skip lines */
791 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
793 uint8_t *p_in, *p_out_end, *p_out;
795 p_in = p_pic->p[i_plane].p_pixels;
797 p_out = p_outpic->p[i_plane].p_pixels;
798 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
799 * p_outpic->p[i_plane].i_visible_lines;
801 /* All lines: mean value */
802 for( ; p_out < p_out_end ; )
804 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
805 p_pic->p[i_plane].i_pitch );
807 p_out += p_pic->p[i_plane].i_pitch;
808 p_in += 2 * p_pic->p[i_plane].i_pitch;
814 static void RenderBlend( vout_thread_t *p_vout,
815 picture_t *p_outpic, picture_t *p_pic )
819 /* Copy image and skip lines */
820 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
822 uint8_t *p_in, *p_out_end, *p_out;
824 p_in = p_pic->p[i_plane].p_pixels;
826 p_out = p_outpic->p[i_plane].p_pixels;
827 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
828 * p_outpic->p[i_plane].i_visible_lines;
830 switch( p_vout->render.i_chroma )
832 case VLC_FOURCC('I','4','2','0'):
833 case VLC_FOURCC('I','Y','U','V'):
834 case VLC_FOURCC('Y','V','1','2'):
835 /* First line: simple copy */
836 p_vout->p_vlc->pf_memcpy( p_out, p_in,
837 p_pic->p[i_plane].i_pitch );
838 p_out += p_pic->p[i_plane].i_pitch;
840 /* Remaining lines: mean value */
841 for( ; p_out < p_out_end ; )
843 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
844 p_pic->p[i_plane].i_pitch );
846 p_out += p_pic->p[i_plane].i_pitch;
847 p_in += p_pic->p[i_plane].i_pitch;
851 case VLC_FOURCC('I','4','2','2'):
852 /* First line: simple copy */
853 p_vout->p_vlc->pf_memcpy( p_out, p_in,
854 p_pic->p[i_plane].i_pitch );
855 p_out += p_pic->p[i_plane].i_pitch;
857 /* Remaining lines: mean value */
858 if( i_plane == Y_PLANE )
860 for( ; p_out < p_out_end ; )
862 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
863 p_pic->p[i_plane].i_pitch );
865 p_out += p_pic->p[i_plane].i_pitch;
866 p_in += p_pic->p[i_plane].i_pitch;
872 for( ; p_out < p_out_end ; )
874 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
875 p_pic->p[i_plane].i_pitch );
877 p_out += p_pic->p[i_plane].i_pitch;
878 p_in += 2*p_pic->p[i_plane].i_pitch;
889 static void MergeGeneric( void *_p_dest, const void *_p_s1,
890 const void *_p_s2, size_t i_bytes )
892 uint8_t* p_dest = (uint8_t*)_p_dest;
893 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
894 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
895 uint8_t* p_end = p_dest + i_bytes - 8;
897 while( p_dest < p_end )
899 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
900 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
901 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
902 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
903 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
904 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
905 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
906 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
911 while( p_dest < p_end )
913 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
917 #if defined(CAN_COMPILE_MMXEXT)
918 static void MergeMMX( void *_p_dest, const void *_p_s1, const void *_p_s2,
921 uint8_t* p_dest = (uint8_t*)_p_dest;
922 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
923 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
924 uint8_t* p_end = p_dest + i_bytes - 8;
925 while( p_dest < p_end )
927 __asm__ __volatile__( "movq %2,%%mm1;"
929 "movq %%mm1, %0" :"=m" (*p_dest):
939 while( p_dest < p_end )
941 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
946 #if defined(CAN_COMPILE_SSE)
947 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
950 uint8_t* p_dest = (uint8_t*)_p_dest;
951 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
952 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
954 while( (ptrdiff_t)p_s1 % 16 )
956 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
958 p_end = p_dest + i_bytes - 16;
959 while( p_dest < p_end )
961 __asm__ __volatile__( "movdqu %2,%%xmm1;"
963 "movdqu %%xmm1, %0" :"=m" (*p_dest):
973 while( p_dest < p_end )
975 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
980 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
981 static void EndMMX( void )
983 __asm__ __volatile__( "emms" :: );
987 #ifdef CAN_COMPILE_C_ALTIVEC
988 static void MergeAltivec( void *_p_dest, const void *_p_s1,
989 const void *_p_s2, size_t i_bytes )
991 uint8_t *p_dest = (uint8_t *)_p_dest;
992 uint8_t *p_s1 = (uint8_t *)_p_s1;
993 uint8_t *p_s2 = (uint8_t *)_p_s2;
994 uint8_t *p_end = p_dest + i_bytes - 15;
996 /* Use C until the first 16-bytes aligned destination pixel */
997 while( (int)p_dest & 0xF )
999 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1002 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1004 /* Unaligned source */
1005 vector unsigned char s1v, s2v, destv;
1006 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1007 vector unsigned char perm1v, perm2v;
1009 perm1v = vec_lvsl( 0, p_s1 );
1010 perm2v = vec_lvsl( 0, p_s2 );
1011 s1oldv = vec_ld( 0, p_s1 );
1012 s2oldv = vec_ld( 0, p_s2 );
1014 while( p_dest < p_end )
1016 s1newv = vec_ld( 16, p_s1 );
1017 s2newv = vec_ld( 16, p_s2 );
1018 s1v = vec_perm( s1oldv, s1newv, perm1v );
1019 s2v = vec_perm( s2oldv, s2newv, perm2v );
1022 destv = vec_avg( s1v, s2v );
1023 vec_st( destv, 0, p_dest );
1032 /* Aligned source */
1033 vector unsigned char s1v, s2v, destv;
1035 while( p_dest < p_end )
1037 s1v = vec_ld( 0, p_s1 );
1038 s2v = vec_ld( 0, p_s2 );
1039 destv = vec_avg( s1v, s2v );
1040 vec_st( destv, 0, p_dest );
1050 while( p_dest < p_end )
1052 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1057 /*****************************************************************************
1058 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1059 * and apply a process to recreate the bottom field :
1060 * If a 8x8 block is classified as :
1061 * - progressive: it applies a small blend (1,6,1)
1063 * * in the MMX version: we do a ME between the 2 fields, if there is a
1064 * good match we use MC to recreate the bottom field (with a small
1066 * * otherwise: it recreates the bottom field by an edge oriented
1068 *****************************************************************************/
1070 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1071 * XXX: It need to access to 8x10
1072 * We use more than 8 lines to help with scrolling (text)
1073 * (and because XDeint8x8Frame use line 9)
1074 * XXX: smooth/uniform area with noise detection doesn't works well
1075 * but it's not really a problem because they don't have much details anyway
1077 static inline int ssd( int a ) { return a*a; }
1078 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1084 /* Detect interlacing */
1086 for( y = 0; y < 7; y += 2 )
1089 for( x = 0; x < 8; x++ )
1091 fr += ssd(src[ x] - src[1*i_src+x]) +
1092 ssd(src[i_src+x] - src[2*i_src+x]);
1093 ff += ssd(src[ x] - src[2*i_src+x]) +
1094 ssd(src[i_src+x] - src[3*i_src+x]);
1096 if( ff < 6*fr/8 && fr > 32 )
1102 return fc < 1 ? VLC_FALSE : VLC_TRUE;
1104 #ifdef CAN_COMPILE_MMXEXT
1105 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1112 /* Detect interlacing */
1114 pxor_r2r( mm7, mm7 );
1115 for( y = 0; y < 9; y += 2 )
1118 pxor_r2r( mm5, mm5 );
1119 pxor_r2r( mm6, mm6 );
1120 for( x = 0; x < 8; x+=4 )
1122 movd_m2r( src[ x], mm0 );
1123 movd_m2r( src[1*i_src+x], mm1 );
1124 movd_m2r( src[2*i_src+x], mm2 );
1125 movd_m2r( src[3*i_src+x], mm3 );
1127 punpcklbw_r2r( mm7, mm0 );
1128 punpcklbw_r2r( mm7, mm1 );
1129 punpcklbw_r2r( mm7, mm2 );
1130 punpcklbw_r2r( mm7, mm3 );
1132 movq_r2r( mm0, mm4 );
1134 psubw_r2r( mm1, mm0 );
1135 psubw_r2r( mm2, mm4 );
1137 psubw_r2r( mm1, mm2 );
1138 psubw_r2r( mm1, mm3 );
1140 pmaddwd_r2r( mm0, mm0 );
1141 pmaddwd_r2r( mm4, mm4 );
1142 pmaddwd_r2r( mm2, mm2 );
1143 pmaddwd_r2r( mm3, mm3 );
1144 paddd_r2r( mm0, mm2 );
1145 paddd_r2r( mm4, mm3 );
1146 paddd_r2r( mm2, mm5 );
1147 paddd_r2r( mm3, mm6 );
1150 movq_r2r( mm5, mm0 );
1151 psrlq_i2r( 32, mm0 );
1152 paddd_r2r( mm0, mm5 );
1153 movd_r2m( mm5, fr );
1155 movq_r2r( mm6, mm0 );
1156 psrlq_i2r( 32, mm0 );
1157 paddd_r2r( mm0, mm6 );
1158 movd_r2m( mm6, ff );
1160 if( ff < 6*fr/8 && fr > 32 )
1169 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1170 * This won't destroy details, and help if there is a bit of interlacing.
1171 * (It helps with paning to avoid flickers)
1175 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1176 uint8_t *src, int i_src )
1181 for( y = 0; y < 8; y += 2 )
1183 memcpy( dst, src, 8 );
1186 for( x = 0; x < 8; x++ )
1187 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1193 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1194 uint8_t *src1, int i_src1,
1195 uint8_t *src2, int i_src2 )
1200 for( y = 0; y < 8; y += 2 )
1202 memcpy( dst, src1, 8 );
1205 for( x = 0; x < 8; x++ )
1206 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1214 #ifdef CAN_COMPILE_MMXEXT
1215 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1216 uint8_t *src1, int i_src1,
1217 uint8_t *src2, int i_src2 )
1219 static const uint64_t m_4 = I64C(0x0004000400040004);
1223 pxor_r2r( mm7, mm7 );
1224 for( y = 0; y < 8; y += 2 )
1226 for( x = 0; x < 8; x +=4 )
1228 movd_m2r( src1[x], mm0 );
1229 movd_r2m( mm0, dst[x] );
1231 movd_m2r( src2[x], mm1 );
1232 movd_m2r( src1[i_src1+x], mm2 );
1234 punpcklbw_r2r( mm7, mm0 );
1235 punpcklbw_r2r( mm7, mm1 );
1236 punpcklbw_r2r( mm7, mm2 );
1237 paddw_r2r( mm1, mm1 );
1238 movq_r2r( mm1, mm3 );
1239 paddw_r2r( mm3, mm3 );
1240 paddw_r2r( mm2, mm0 );
1241 paddw_r2r( mm3, mm1 );
1242 paddw_m2r( m_4, mm1 );
1243 paddw_r2r( mm1, mm0 );
1244 psraw_i2r( 3, mm0 );
1245 packuswb_r2r( mm7, mm0 );
1246 movd_r2m( mm0, dst[i_dst+x] );
1257 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1260 for( y = 0; y < 8; y++ )
1261 memset( &dst[y*i_dst], v, 8 );
1264 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1267 * TODO: a better one for the inner part.
1269 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1270 uint8_t *src, int i_src )
1275 for( y = 0; y < 8; y += 2 )
1277 memcpy( dst, src, 8 );
1280 for( x = 0; x < 8; x++ )
1281 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1286 #ifdef CAN_COMPILE_MMXEXT
1287 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1288 uint8_t *src, int i_src )
1293 for( y = 0; y < 8; y += 2 )
1295 movq_m2r( src[0], mm0 );
1296 movq_r2m( mm0, dst[0] );
1299 movq_m2r( src[2*i_src], mm1 );
1300 pavgb_r2r( mm1, mm0 );
1302 movq_r2m( mm0, dst[0] );
1310 /* XDeint8x8Field: Edge oriented interpolation
1311 * (Need -4 and +5 pixels H, +1 line)
1313 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1314 uint8_t *src, int i_src )
1319 for( y = 0; y < 8; y += 2 )
1321 memcpy( dst, src, 8 );
1324 for( x = 0; x < 8; x++ )
1326 uint8_t *src2 = &src[2*i_src];
1327 /* I use 8 pixels just to match the MMX version, but it's overkill
1328 * 5 would be enough (less isn't good) */
1329 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1330 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1331 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1332 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1334 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1335 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1336 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1337 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1339 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1340 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1341 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1342 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1344 if( c0 < c1 && c1 <= c2 )
1345 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1346 else if( c2 < c1 && c1 <= c0 )
1347 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1349 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1356 #ifdef CAN_COMPILE_MMXEXT
1357 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1358 uint8_t *src, int i_src )
1363 for( y = 0; y < 8; y += 2 )
1365 memcpy( dst, src, 8 );
1368 for( x = 0; x < 8; x++ )
1370 uint8_t *src2 = &src[2*i_src];
1373 movq_m2r( src[x-2], mm0 );
1374 movq_m2r( src[x-3], mm1 );
1375 movq_m2r( src[x-4], mm2 );
1377 psadbw_m2r( src2[x-4], mm0 );
1378 psadbw_m2r( src2[x-3], mm1 );
1379 psadbw_m2r( src2[x-2], mm2 );
1381 movd_r2m( mm0, c2 );
1382 movd_r2m( mm1, c1 );
1383 movd_r2m( mm2, c0 );
1385 if( c0 < c1 && c1 <= c2 )
1386 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1387 else if( c2 < c1 && c1 <= c0 )
1388 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1390 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1400 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1401 uint8_t *pix2, int i_pix2 )
1406 for( y = 0; y < 8; y++ )
1407 for( x = 0; x < 8; x++ )
1408 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1412 #ifdef CAN_COMPILE_MMXEXT
1413 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1414 uint8_t *pix2, int i_pix2 )
1419 pxor_r2r( mm7, mm7 );
1420 pxor_r2r( mm6, mm6 );
1422 for( y = 0; y < 8; y++ )
1424 movq_m2r( pix1[0], mm0 );
1425 movq_m2r( pix2[0], mm1 );
1427 movq_r2r( mm0, mm2 );
1428 movq_r2r( mm1, mm3 );
1430 punpcklbw_r2r( mm7, mm0 );
1431 punpckhbw_r2r( mm7, mm2 );
1432 punpcklbw_r2r( mm7, mm1 );
1433 punpckhbw_r2r( mm7, mm3 );
1435 psubw_r2r( mm1, mm0 );
1436 psubw_r2r( mm3, mm2 );
1438 pmaddwd_r2r( mm0, mm0 );
1439 pmaddwd_r2r( mm2, mm2 );
1441 paddd_r2r( mm2, mm0 );
1442 paddd_r2r( mm0, mm6 );
1448 movq_r2r( mm6, mm7 );
1449 psrlq_i2r( 32, mm7 );
1450 paddd_r2r( mm6, mm7 );
1459 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1460 #ifdef CAN_COMPILE_MMXEXT
1463 * TODO: mmx version (easier in sse2)
1465 static inline void XDeintMC( uint8_t *dst, int i_dst,
1466 uint8_t *src, int i_src,
1468 int i_width, int i_height )
1470 const int d4x = mvx&0x03;
1471 const int d4y = mvy&0x03;
1473 const int cA = (4-d4x)*(4-d4y);
1474 const int cB = d4x *(4-d4y);
1475 const int cC = (4-d4x)*d4y;
1476 const int cD = d4x *d4y;
1482 src += (mvy >> 2) * i_src + (mvx >> 2);
1485 for( y = 0; y < i_height; y++ )
1487 for( x = 0; x < i_width; x++ )
1489 dst[x] = ( cA*src[x] + cB*src[x+1] +
1490 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1498 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1499 uint8_t *pix2, int i_pix2 )
1503 movq_m2r( pix1[0*i_pix1], mm0 );
1504 movq_m2r( pix1[1*i_pix1], mm1 );
1506 psadbw_m2r( pix2[0*i_pix2], mm0 );
1507 psadbw_m2r( pix2[1*i_pix2], mm1 );
1509 movq_m2r( pix1[2*i_pix1], mm2 );
1510 movq_m2r( pix1[3*i_pix1], mm3 );
1511 psadbw_m2r( pix2[2*i_pix2], mm2 );
1512 psadbw_m2r( pix2[3*i_pix2], mm3 );
1514 paddd_r2r( mm1, mm0 );
1515 paddd_r2r( mm3, mm2 );
1516 paddd_r2r( mm2, mm0 );
1522 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1523 uint8_t *ref, int i_stride,
1525 int xmax, int ymax )
1527 uint8_t buffer[8*4];
1529 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1532 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1533 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1535 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1536 uint8_t *ref, int i_stride,
1538 int xmax, int ymax )
1540 if( abs(mx) >= xmax || abs(my) >= ymax )
1543 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1546 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1547 uint8_t *src, int i_src,
1549 int xmax, int ymax )
1551 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1552 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1553 uint8_t *next = &src[i_src];
1554 const int i_src2 = 2*i_src;
1559 uint8_t *rec = &dst[i_dst];
1561 /* We construct with intra method the missing field */
1562 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1564 /* Now we will try to find a match with ME with the other field */
1566 /* ME: A small/partial EPZS
1567 * We search only for small MV (with high motion intra will be perfect */
1568 if( xmax > 4 ) xmax = 4;
1569 if( ymax > 4 ) ymax = 4;
1571 /* Init with NULL Mv */
1573 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1575 /* Try predicted Mv */
1576 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1582 /* Search interger pel (small mv) */
1583 for( i_step = 0; i_step < 4; i_step++ )
1589 for( i = 0; i < 4; i++ )
1591 s = XDeint8x4TestInt( rec, i_src2,
1592 next, i_src2, mvx+dx[i], mvy+dy[i],
1612 if( mvs > 4 && mvs < 256 )
1615 /* XXX: for now only HPEL (too slow) */
1616 for( i_step = 0; i_step < 4; i_step++ )
1622 for( i = 0; i < 8; i++ )
1624 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1625 mvx+dx[i], mvy+dy[i],
1643 uint8_t buffer[8*4];
1644 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1645 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1647 //XDeint8x8Set( dst, i_dst, 0 );
1654 /* Kernel interpolation (1,-5,20,20,-5,1)
1655 * Loose a bit more details+add aliasing than edge interpol but avoid
1658 static inline uint8_t clip1( int a )
1667 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1668 uint8_t *src, int i_src )
1673 for( y = 0; y < 8; y += 2 )
1675 const int i_src2 = i_src*2;
1677 memcpy( dst, src, 8 );
1680 for( x = 0; x < 8; x++ )
1684 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1685 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1686 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1688 dst[x] = clip1( ( pix + 16 ) >> 5 );
1698 /* NxN arbitray size (and then only use pixel in the NxN block)
1700 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1701 int i_height, int i_width )
1708 /* Detect interlacing */
1709 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1712 for( y = 0; y < i_height - 2; y += 2 )
1714 const uint8_t *s = &src[y*i_src];
1715 for( x = 0; x < i_width; x++ )
1717 fr += ssd(s[ x] - s[1*i_src+x]);
1718 ff += ssd(s[ x] - s[2*i_src+x]);
1720 if( ff < fr && fr > i_width / 2 )
1724 return fc < 2 ? VLC_FALSE : VLC_TRUE;
1727 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1728 uint8_t *src, int i_src,
1729 int i_width, int i_height )
1734 for( y = 0; y < i_height; y += 2 )
1736 memcpy( dst, src, i_width );
1739 if( y < i_height - 2 )
1741 for( x = 0; x < i_width; x++ )
1742 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1746 /* Blend last line */
1747 for( x = 0; x < i_width; x++ )
1748 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1755 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1756 uint8_t *src, int i_src,
1757 int i_width, int i_height )
1762 for( y = 0; y < i_height; y += 2 )
1764 memcpy( dst, src, i_width );
1767 if( y < i_height - 2 )
1769 for( x = 0; x < i_width; x++ )
1770 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1774 /* Blend last line */
1775 for( x = 0; x < i_width; x++ )
1776 dst[x] = (src[x] + src[i_src+x]) >> 1;
1783 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1784 int i_width, int i_height )
1786 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1787 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1789 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1793 static inline int median( int a, int b, int c )
1795 int min = a, max =a;
1806 return a + b + c - min - max;
1812 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1813 uint8_t *src, int i_src,
1814 const int i_mbx, int i_modx )
1818 for( x = 0; x < i_mbx; x++ )
1821 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1823 if( x == 0 || x == i_mbx - 1 )
1824 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1826 XDeint8x8FieldC( dst, i_dst, src, i_src );
1830 XDeint8x8MergeC( dst, i_dst,
1831 &src[0*i_src], 2*i_src,
1832 &src[1*i_src], 2*i_src );
1840 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1842 #ifdef CAN_COMPILE_MMXEXT
1843 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1844 uint8_t *src, int i_src,
1845 const int i_mbx, int i_modx )
1849 /* Reset current line */
1850 for( x = 0; x < i_mbx; x++ )
1853 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1855 if( x == 0 || x == i_mbx - 1 )
1856 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1858 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1862 XDeint8x8MergeMMXEXT( dst, i_dst,
1863 &src[0*i_src], 2*i_src,
1864 &src[1*i_src], 2*i_src );
1872 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1876 static void RenderX( vout_thread_t *p_vout,
1877 picture_t *p_outpic, picture_t *p_pic )
1881 /* Copy image and skip lines */
1882 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1884 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1885 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1887 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1888 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1890 const int i_dst = p_outpic->p[i_plane].i_pitch;
1891 const int i_src = p_pic->p[i_plane].i_pitch;
1895 for( y = 0; y < i_mby; y++ )
1897 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1898 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1900 #ifdef CAN_COMPILE_MMXEXT
1901 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
1902 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1905 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1908 /* Last line (C only)*/
1911 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1912 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1914 for( x = 0; x < i_mbx; x++ )
1916 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1923 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1927 #ifdef CAN_COMPILE_MMXEXT
1928 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
1933 /*****************************************************************************
1934 * SendEvents: forward mouse and keyboard events to the parent p_vout
1935 *****************************************************************************/
1936 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1937 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1939 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1940 vlc_value_t sentval = newval;
1942 if( !strcmp( psz_var, "mouse-y" ) )
1944 switch( p_vout->p_sys->i_mode )
1946 case DEINTERLACE_MEAN:
1947 case DEINTERLACE_DISCARD:
1953 var_Set( p_vout, psz_var, sentval );
1958 /*****************************************************************************
1959 * FilterCallback: called when changing the deinterlace method on the fly.
1960 *****************************************************************************/
1961 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1962 vlc_value_t oldval, vlc_value_t newval,
1965 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1966 int i_old_mode = p_vout->p_sys->i_mode;
1968 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1970 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1972 SetFilterMethod( p_vout, newval.psz_string );
1974 switch( p_vout->render.i_chroma )
1976 case VLC_FOURCC('I','4','2','2'):
1977 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1981 case VLC_FOURCC('I','4','2','0'):
1982 case VLC_FOURCC('I','Y','U','V'):
1983 case VLC_FOURCC('Y','V','1','2'):
1984 switch( p_vout->p_sys->i_mode )
1986 case DEINTERLACE_MEAN:
1987 case DEINTERLACE_DISCARD:
1988 if( ( i_old_mode == DEINTERLACE_MEAN )
1989 || ( i_old_mode == DEINTERLACE_DISCARD ) )
1991 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1996 case DEINTERLACE_BOB:
1997 case DEINTERLACE_BLEND:
1998 case DEINTERLACE_LINEAR:
1999 if( ( i_old_mode == DEINTERLACE_BOB )
2000 || ( i_old_mode == DEINTERLACE_BLEND )
2001 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2003 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2014 /* We need to kill the old vout */
2016 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2018 vlc_object_detach( p_vout->p_sys->p_vout );
2019 vout_Destroy( p_vout->p_sys->p_vout );
2021 /* Try to open a new video output */
2022 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2024 if( p_vout->p_sys->p_vout == NULL )
2026 /* Everything failed */
2027 msg_Err( p_vout, "cannot open vout, aborting" );
2029 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2030 return VLC_EGENERIC;
2033 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2035 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2039 /*****************************************************************************
2040 * SendEventsToChild: forward events to the child/children vout
2041 *****************************************************************************/
2042 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2043 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2045 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2046 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2051 /*****************************************************************************
2052 * video filter2 functions
2053 *****************************************************************************/
2054 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2056 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2057 picture_t *p_pic_dst;
2059 /* Request output picture */
2060 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
2061 if( p_pic_dst == NULL )
2063 msg_Warn( p_filter, "can't get output picture" );
2067 switch( p_vout->p_sys->i_mode )
2069 case DEINTERLACE_DISCARD:
2071 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2073 msg_Err( p_vout, "discarding lines is not supported yet" );
2074 p_pic_dst->pf_release( p_pic_dst );
2078 case DEINTERLACE_BOB:
2080 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2081 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2085 case DEINTERLACE_LINEAR:
2087 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2088 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2090 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2091 p_pic_dst->pf_release( p_pic_dst );
2095 case DEINTERLACE_MEAN:
2096 RenderMean( p_vout, p_pic_dst, p_pic );
2099 case DEINTERLACE_BLEND:
2100 RenderBlend( p_vout, p_pic_dst, p_pic );
2104 RenderX( p_vout, p_pic_dst, p_pic );
2108 p_pic_dst->date = p_pic->date;
2109 p_pic_dst->b_force = p_pic->b_force;
2110 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2111 p_pic_dst->b_progressive = VLC_TRUE;
2112 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2114 p_pic->pf_release( p_pic );
2118 /*****************************************************************************
2120 *****************************************************************************/
2121 static int OpenFilter( vlc_object_t *p_this )
2123 filter_t *p_filter = (filter_t*)p_this;
2124 vout_thread_t *p_vout;
2127 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2128 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2129 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2130 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2132 return VLC_EGENERIC;
2135 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2137 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2138 vlc_object_attach( p_vout, p_filter );
2139 p_filter->p_sys = (filter_sys_t *)p_vout;
2140 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2142 sout_CfgParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2144 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2145 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2146 var_Set( p_filter, "deinterlace-mode", val );
2148 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2150 vlc_object_detach( p_vout );
2151 vlc_object_release( p_vout );
2152 return VLC_EGENERIC;
2155 p_filter->pf_video_filter = Deinterlace;
2157 msg_Dbg( p_filter, "deinterlacing" );
2162 /*****************************************************************************
2163 * CloseFilter: clean up the filter
2164 *****************************************************************************/
2165 static void CloseFilter( vlc_object_t *p_this )
2167 filter_t *p_filter = (filter_t*)p_this;
2168 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2170 Destroy( VLC_OBJECT(p_vout) );
2171 vlc_object_detach( p_vout );
2172 vlc_object_release( p_vout );