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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_plugin.h>
37 #include "vlc_filter.h"
43 #ifdef CAN_COMPILE_MMXEXT
47 #include "filter_common.h"
49 #define DEINTERLACE_DISCARD 1
50 #define DEINTERLACE_MEAN 2
51 #define DEINTERLACE_BLEND 3
52 #define DEINTERLACE_BOB 4
53 #define DEINTERLACE_LINEAR 5
54 #define DEINTERLACE_X 6
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Create ( vlc_object_t * );
60 static void Destroy ( vlc_object_t * );
62 static int Init ( vout_thread_t * );
63 static void End ( vout_thread_t * );
64 static void Render ( vout_thread_t *, picture_t * );
66 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
67 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
68 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
69 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
70 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
71 static void RenderX ( picture_t *, picture_t * );
73 static void MergeGeneric ( void *, const void *, const void *, size_t );
74 #if defined(CAN_COMPILE_C_ALTIVEC)
75 static void MergeAltivec ( void *, const void *, const void *, size_t );
77 #if defined(CAN_COMPILE_MMXEXT)
78 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
80 #if defined(CAN_COMPILE_3DNOW)
81 static void Merge3DNow ( void *, const void *, const void *, size_t );
83 #if defined(CAN_COMPILE_SSE)
84 static void MergeSSE2 ( void *, const void *, const void *, size_t );
86 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
87 static void EndMMX ( void );
89 #if defined(CAN_COMPILE_3DNOW)
90 static void End3DNow ( void );
93 static int SendEvents ( vlc_object_t *, char const *,
94 vlc_value_t, vlc_value_t, void * );
96 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
97 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
99 static int OpenFilter( vlc_object_t *p_this );
100 static void CloseFilter( vlc_object_t *p_this );
102 /*****************************************************************************
103 * Callback prototypes
104 *****************************************************************************/
105 static int FilterCallback ( vlc_object_t *, char const *,
106 vlc_value_t, vlc_value_t, void * );
108 /*****************************************************************************
110 *****************************************************************************/
111 #define MODE_TEXT N_("Deinterlace mode")
112 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
114 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
115 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
117 #define FILTER_CFG_PREFIX "sout-deinterlace-"
119 static const char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", "x" };
120 static const char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
121 N_("Bob"), N_("Linear"), "X" };
124 set_description( N_("Deinterlacing video filter") );
125 set_shortname( N_("Deinterlace" ));
126 set_capability( "video filter", 0 );
127 set_category( CAT_VIDEO );
128 set_subcategory( SUBCAT_VIDEO_VFILTER );
130 set_section( N_("Display"),NULL);
131 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
132 MODE_LONGTEXT, false );
133 change_string_list( mode_list, mode_list_text, 0 );
135 add_shortcut( "deinterlace" );
136 set_callbacks( Create, Destroy );
139 set_capability( "video filter2", 0 );
140 set_section( N_("Streaming"),NULL);
141 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
142 SOUT_MODE_LONGTEXT, false );
143 change_string_list( mode_list, mode_list_text, 0 );
144 set_callbacks( OpenFilter, CloseFilter );
147 static const char *ppsz_filter_options[] = {
151 /*****************************************************************************
152 * vout_sys_t: Deinterlace video output method descriptor
153 *****************************************************************************
154 * This structure is part of the video output thread descriptor.
155 * It describes the Deinterlace specific properties of an output thread.
156 *****************************************************************************/
159 int i_mode; /* Deinterlace mode */
160 bool b_double_rate; /* Shall we double the framerate? */
165 vout_thread_t *p_vout;
167 vlc_mutex_t filter_lock;
169 void (*pf_merge) ( void *, const void *, const void *, size_t );
170 void (*pf_end_merge) ( void );
173 /*****************************************************************************
174 * Control: control facility for the vout (forwards to child vout)
175 *****************************************************************************/
176 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
178 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
181 /*****************************************************************************
182 * Create: allocates Deinterlace video thread output method
183 *****************************************************************************
184 * This function allocates and initializes a Deinterlace vout method.
185 *****************************************************************************/
186 static int Create( vlc_object_t *p_this )
188 vout_thread_t *p_vout = (vout_thread_t *)p_this;
191 /* Allocate structure */
192 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
193 if( p_vout->p_sys == NULL )
195 msg_Err( p_vout, "out of memory" );
199 p_vout->pf_init = Init;
200 p_vout->pf_end = End;
201 p_vout->pf_manage = NULL;
202 p_vout->pf_render = Render;
203 p_vout->pf_display = NULL;
204 p_vout->pf_control = Control;
206 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
207 p_vout->p_sys->b_double_rate = false;
208 p_vout->p_sys->last_date = 0;
209 p_vout->p_sys->p_vout = 0;
210 vlc_mutex_init( &p_vout->p_sys->filter_lock );
212 #if defined(CAN_COMPILE_C_ALTIVEC)
213 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
215 p_vout->p_sys->pf_merge = MergeAltivec;
216 p_vout->p_sys->pf_end_merge = NULL;
220 #if defined(CAN_COMPILE_SSE)
221 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
223 p_vout->p_sys->pf_merge = MergeSSE2;
224 p_vout->p_sys->pf_end_merge = EndMMX;
228 #if defined(CAN_COMPILE_MMXEXT)
229 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
231 p_vout->p_sys->pf_merge = MergeMMXEXT;
232 p_vout->p_sys->pf_end_merge = EndMMX;
236 #if defined(CAN_COMPILE_3DNOW)
237 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
239 p_vout->p_sys->pf_merge = Merge3DNow;
240 p_vout->p_sys->pf_end_merge = End3DNow;
245 p_vout->p_sys->pf_merge = MergeGeneric;
246 p_vout->p_sys->pf_end_merge = NULL;
249 /* Look what method was requested */
250 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
251 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
253 if( val.psz_string == NULL )
255 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
256 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
258 val.psz_string = strdup( "discard" );
261 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
263 SetFilterMethod( p_vout, val.psz_string );
265 free( val.psz_string );
270 /*****************************************************************************
271 * SetFilterMethod: setup the deinterlace method to use.
272 *****************************************************************************/
273 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
275 if( !strcmp( psz_method, "discard" ) )
277 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
278 p_vout->p_sys->b_double_rate = false;
280 else if( !strcmp( psz_method, "mean" ) )
282 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
283 p_vout->p_sys->b_double_rate = false;
285 else if( !strcmp( psz_method, "blend" )
286 || !strcmp( psz_method, "average" )
287 || !strcmp( psz_method, "combine-fields" ) )
289 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
290 p_vout->p_sys->b_double_rate = false;
292 else if( !strcmp( psz_method, "bob" )
293 || !strcmp( psz_method, "progressive-scan" ) )
295 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
296 p_vout->p_sys->b_double_rate = true;
298 else if( !strcmp( psz_method, "linear" ) )
300 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
301 p_vout->p_sys->b_double_rate = true;
303 else if( !strcmp( psz_method, "x" ) )
305 p_vout->p_sys->i_mode = DEINTERLACE_X;
306 p_vout->p_sys->b_double_rate = false;
310 msg_Err( p_vout, "no valid deinterlace mode provided, "
311 "using \"discard\"" );
314 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
317 /*****************************************************************************
318 * Init: initialize Deinterlace video thread output method
319 *****************************************************************************/
320 static int Init( vout_thread_t *p_vout )
325 I_OUTPUTPICTURES = 0;
327 /* Initialize the output structure, full of directbuffers since we want
328 * the decoder to output directly to our structures. */
329 switch( p_vout->render.i_chroma )
331 case VLC_FOURCC('I','4','2','0'):
332 case VLC_FOURCC('I','Y','U','V'):
333 case VLC_FOURCC('Y','V','1','2'):
334 case VLC_FOURCC('I','4','2','2'):
335 p_vout->output.i_chroma = p_vout->render.i_chroma;
336 p_vout->output.i_width = p_vout->render.i_width;
337 p_vout->output.i_height = p_vout->render.i_height;
338 p_vout->output.i_aspect = p_vout->render.i_aspect;
339 p_vout->fmt_out = p_vout->fmt_in;
343 return VLC_EGENERIC; /* unknown chroma */
347 /* Try to open the real video output */
348 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
350 if( p_vout->p_sys->p_vout == NULL )
352 /* Everything failed */
353 msg_Err( p_vout, "cannot open vout, aborting" );
358 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
360 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
362 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
364 ADD_PARENT_CALLBACKS( SendEventsToChild );
369 /*****************************************************************************
370 * SpawnRealVout: spawn the real video output.
371 *****************************************************************************/
372 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
374 vout_thread_t *p_real_vout = NULL;
376 memset( &fmt, 0, sizeof( video_format_t ) );
378 msg_Dbg( p_vout, "spawning the real video output" );
380 fmt = p_vout->fmt_out;
382 switch( p_vout->render.i_chroma )
384 case VLC_FOURCC('I','4','2','0'):
385 case VLC_FOURCC('I','Y','U','V'):
386 case VLC_FOURCC('Y','V','1','2'):
387 switch( p_vout->p_sys->i_mode )
389 case DEINTERLACE_MEAN:
390 case DEINTERLACE_DISCARD:
391 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
393 p_real_vout = vout_Create( p_vout, &fmt );
396 case DEINTERLACE_BOB:
397 case DEINTERLACE_BLEND:
398 case DEINTERLACE_LINEAR:
400 p_real_vout = vout_Create( p_vout, &fmt );
405 case VLC_FOURCC('I','4','2','2'):
406 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
407 p_real_vout = vout_Create( p_vout, &fmt );
417 /*****************************************************************************
418 * End: terminate Deinterlace video thread output method
419 *****************************************************************************/
420 static void End( vout_thread_t *p_vout )
424 /* Free the fake output buffers we allocated */
425 for( i_index = I_OUTPUTPICTURES ; i_index ; )
428 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
431 if( p_vout->p_sys->p_vout )
433 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
434 vlc_object_detach( p_vout->p_sys->p_vout );
435 vout_Destroy( p_vout->p_sys->p_vout );
438 DEL_PARENT_CALLBACKS( SendEventsToChild );
441 /*****************************************************************************
442 * Destroy: destroy Deinterlace video thread output method
443 *****************************************************************************
444 * Terminate an output method created by DeinterlaceCreateOutputMethod
445 *****************************************************************************/
446 static void Destroy( vlc_object_t *p_this )
448 vout_thread_t *p_vout = (vout_thread_t *)p_this;
449 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
450 free( p_vout->p_sys );
453 /*****************************************************************************
454 * Render: displays previously rendered output
455 *****************************************************************************
456 * This function send the currently rendered image to Deinterlace image,
457 * waits until it is displayed and switch the two rendering buffers, preparing
459 *****************************************************************************/
460 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
462 vout_sys_t *p_sys = p_vout->p_sys;
463 picture_t *pp_outpic[2];
465 p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
466 p_vout->fmt_in.i_x_offset;
467 p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
468 p_vout->fmt_in.i_y_offset;
469 p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
470 p_vout->fmt_in.i_visible_width;
471 p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
472 p_vout->fmt_in.i_visible_height;
473 if( p_vout->p_sys->i_mode == DEINTERLACE_MEAN ||
474 p_vout->p_sys->i_mode == DEINTERLACE_DISCARD )
476 p_vout->fmt_out.i_y_offset /= 2; p_sys->p_vout->fmt_in.i_y_offset /= 2;
477 p_vout->fmt_out.i_visible_height /= 2;
478 p_sys->p_vout->fmt_in.i_visible_height /= 2;
481 pp_outpic[0] = pp_outpic[1] = NULL;
483 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
485 /* Get a new picture */
486 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
490 if( p_vout->b_die || p_vout->b_error )
492 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
495 msleep( VOUT_OUTMEM_SLEEP );
498 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
500 /* If we are using double rate, get an additional new picture */
501 if( p_vout->p_sys->b_double_rate )
503 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
507 if( p_vout->b_die || p_vout->b_error )
509 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
510 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
513 msleep( VOUT_OUTMEM_SLEEP );
516 /* 20ms is a bit arbitrary, but it's only for the first image we get */
517 if( !p_vout->p_sys->last_date )
519 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
520 p_pic->date + 20000 );
524 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
525 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
527 p_vout->p_sys->last_date = p_pic->date;
530 switch( p_vout->p_sys->i_mode )
532 case DEINTERLACE_DISCARD:
533 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
534 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
537 case DEINTERLACE_BOB:
538 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
539 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
540 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
541 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
544 case DEINTERLACE_LINEAR:
545 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
546 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
547 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
548 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
551 case DEINTERLACE_MEAN:
552 RenderMean( p_vout, pp_outpic[0], p_pic );
553 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
556 case DEINTERLACE_BLEND:
557 RenderBlend( p_vout, pp_outpic[0], p_pic );
558 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
562 RenderX( pp_outpic[0], p_pic );
563 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
566 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
569 /*****************************************************************************
570 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
571 *****************************************************************************/
572 static void RenderDiscard( vout_thread_t *p_vout,
573 picture_t *p_outpic, picture_t *p_pic, int i_field )
577 /* Copy image and skip lines */
578 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
580 uint8_t *p_in, *p_out_end, *p_out;
583 p_in = p_pic->p[i_plane].p_pixels
584 + i_field * p_pic->p[i_plane].i_pitch;
586 p_out = p_outpic->p[i_plane].p_pixels;
587 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
588 * p_outpic->p[i_plane].i_visible_lines;
590 switch( p_vout->render.i_chroma )
592 case VLC_FOURCC('I','4','2','0'):
593 case VLC_FOURCC('I','Y','U','V'):
594 case VLC_FOURCC('Y','V','1','2'):
596 for( ; p_out < p_out_end ; )
598 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
600 p_out += p_outpic->p[i_plane].i_pitch;
601 p_in += 2 * p_pic->p[i_plane].i_pitch;
605 case VLC_FOURCC('I','4','2','2'):
607 i_increment = 2 * p_pic->p[i_plane].i_pitch;
609 if( i_plane == Y_PLANE )
611 for( ; p_out < p_out_end ; )
613 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
614 p_out += p_outpic->p[i_plane].i_pitch;
615 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
616 p_out += p_outpic->p[i_plane].i_pitch;
622 for( ; p_out < p_out_end ; )
624 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
625 p_out += p_outpic->p[i_plane].i_pitch;
637 /*****************************************************************************
638 * RenderBob: renders a BOB picture - simple copy
639 *****************************************************************************/
640 static void RenderBob( vout_thread_t *p_vout,
641 picture_t *p_outpic, picture_t *p_pic, int i_field )
645 /* Copy image and skip lines */
646 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
648 uint8_t *p_in, *p_out_end, *p_out;
650 p_in = p_pic->p[i_plane].p_pixels;
651 p_out = p_outpic->p[i_plane].p_pixels;
652 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
653 * p_outpic->p[i_plane].i_visible_lines;
655 switch( p_vout->render.i_chroma )
657 case VLC_FOURCC('I','4','2','0'):
658 case VLC_FOURCC('I','Y','U','V'):
659 case VLC_FOURCC('Y','V','1','2'):
660 /* For BOTTOM field we need to add the first line */
663 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
664 p_in += p_pic->p[i_plane].i_pitch;
665 p_out += p_outpic->p[i_plane].i_pitch;
668 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
670 for( ; p_out < p_out_end ; )
672 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
674 p_out += p_outpic->p[i_plane].i_pitch;
676 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
678 p_in += 2 * p_pic->p[i_plane].i_pitch;
679 p_out += p_outpic->p[i_plane].i_pitch;
682 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
684 /* For TOP field we need to add the last line */
687 p_in += p_pic->p[i_plane].i_pitch;
688 p_out += p_outpic->p[i_plane].i_pitch;
689 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
693 case VLC_FOURCC('I','4','2','2'):
694 /* For BOTTOM field we need to add the first line */
697 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
698 p_in += p_pic->p[i_plane].i_pitch;
699 p_out += p_outpic->p[i_plane].i_pitch;
702 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
704 if( i_plane == Y_PLANE )
706 for( ; p_out < p_out_end ; )
708 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
710 p_out += p_outpic->p[i_plane].i_pitch;
712 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
714 p_in += 2 * p_pic->p[i_plane].i_pitch;
715 p_out += p_outpic->p[i_plane].i_pitch;
720 for( ; p_out < p_out_end ; )
722 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
724 p_out += p_outpic->p[i_plane].i_pitch;
725 p_in += 2 * p_pic->p[i_plane].i_pitch;
729 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
731 /* For TOP field we need to add the last line */
734 p_in += p_pic->p[i_plane].i_pitch;
735 p_out += p_outpic->p[i_plane].i_pitch;
736 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
743 #define Merge p_vout->p_sys->pf_merge
744 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
746 /*****************************************************************************
747 * RenderLinear: BOB with linear interpolation
748 *****************************************************************************/
749 static void RenderLinear( vout_thread_t *p_vout,
750 picture_t *p_outpic, picture_t *p_pic, int i_field )
754 /* Copy image and skip lines */
755 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
757 uint8_t *p_in, *p_out_end, *p_out;
759 p_in = p_pic->p[i_plane].p_pixels;
760 p_out = p_outpic->p[i_plane].p_pixels;
761 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
762 * p_outpic->p[i_plane].i_visible_lines;
764 /* For BOTTOM field we need to add the first line */
767 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
768 p_in += p_pic->p[i_plane].i_pitch;
769 p_out += p_outpic->p[i_plane].i_pitch;
772 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
774 for( ; p_out < p_out_end ; )
776 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
778 p_out += p_outpic->p[i_plane].i_pitch;
780 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
781 p_pic->p[i_plane].i_pitch );
783 p_in += 2 * p_pic->p[i_plane].i_pitch;
784 p_out += p_outpic->p[i_plane].i_pitch;
787 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
789 /* For TOP field we need to add the last line */
792 p_in += p_pic->p[i_plane].i_pitch;
793 p_out += p_outpic->p[i_plane].i_pitch;
794 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
800 static void RenderMean( vout_thread_t *p_vout,
801 picture_t *p_outpic, picture_t *p_pic )
805 /* Copy image and skip lines */
806 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
808 uint8_t *p_in, *p_out_end, *p_out;
810 p_in = p_pic->p[i_plane].p_pixels;
812 p_out = p_outpic->p[i_plane].p_pixels;
813 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
814 * p_outpic->p[i_plane].i_visible_lines;
816 /* All lines: mean value */
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_outpic->p[i_plane].i_pitch;
823 p_in += 2 * p_pic->p[i_plane].i_pitch;
829 static void RenderBlend( vout_thread_t *p_vout,
830 picture_t *p_outpic, picture_t *p_pic )
834 /* Copy image and skip lines */
835 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
837 uint8_t *p_in, *p_out_end, *p_out;
839 p_in = p_pic->p[i_plane].p_pixels;
841 p_out = p_outpic->p[i_plane].p_pixels;
842 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
843 * p_outpic->p[i_plane].i_visible_lines;
845 switch( p_vout->render.i_chroma )
847 case VLC_FOURCC('I','4','2','0'):
848 case VLC_FOURCC('I','Y','U','V'):
849 case VLC_FOURCC('Y','V','1','2'):
850 /* First line: simple copy */
851 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
852 p_out += p_outpic->p[i_plane].i_pitch;
854 /* Remaining lines: mean value */
855 for( ; p_out < p_out_end ; )
857 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
858 p_pic->p[i_plane].i_pitch );
860 p_out += p_outpic->p[i_plane].i_pitch;
861 p_in += p_pic->p[i_plane].i_pitch;
865 case VLC_FOURCC('I','4','2','2'):
866 /* First line: simple copy */
867 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
868 p_out += p_outpic->p[i_plane].i_pitch;
870 /* Remaining lines: mean value */
871 if( i_plane == Y_PLANE )
873 for( ; p_out < p_out_end ; )
875 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
876 p_pic->p[i_plane].i_pitch );
878 p_out += p_outpic->p[i_plane].i_pitch;
879 p_in += p_pic->p[i_plane].i_pitch;
885 for( ; p_out < p_out_end ; )
887 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
888 p_pic->p[i_plane].i_pitch );
890 p_out += p_outpic->p[i_plane].i_pitch;
891 p_in += 2*p_pic->p[i_plane].i_pitch;
902 static void MergeGeneric( void *_p_dest, const void *_p_s1,
903 const void *_p_s2, size_t i_bytes )
905 uint8_t* p_dest = (uint8_t*)_p_dest;
906 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
907 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
908 uint8_t* p_end = p_dest + i_bytes - 8;
910 while( p_dest < p_end )
912 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
913 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
914 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
915 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
916 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
917 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
918 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
919 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
924 while( p_dest < p_end )
926 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
930 #if defined(CAN_COMPILE_MMXEXT)
931 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
934 uint8_t* p_dest = (uint8_t*)_p_dest;
935 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
936 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
937 uint8_t* p_end = p_dest + i_bytes - 8;
938 while( p_dest < p_end )
940 __asm__ __volatile__( "movq %2,%%mm1;"
942 "movq %%mm1, %0" :"=m" (*p_dest):
952 while( p_dest < p_end )
954 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
959 #if defined(CAN_COMPILE_3DNOW)
960 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
963 uint8_t* p_dest = (uint8_t*)_p_dest;
964 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
965 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
966 uint8_t* p_end = p_dest + i_bytes - 8;
967 while( p_dest < p_end )
969 __asm__ __volatile__( "movq %2,%%mm1;"
971 "movq %%mm1, %0" :"=m" (*p_dest):
981 while( p_dest < p_end )
983 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
988 #if defined(CAN_COMPILE_SSE)
989 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
992 uint8_t* p_dest = (uint8_t*)_p_dest;
993 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
994 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
996 while( (uintptr_t)p_s1 % 16 )
998 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1000 p_end = p_dest + i_bytes - 16;
1001 while( p_dest < p_end )
1003 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1005 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1015 while( p_dest < p_end )
1017 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1022 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1023 static void EndMMX( void )
1025 __asm__ __volatile__( "emms" :: );
1029 #if defined(CAN_COMPILE_3DNOW)
1030 static void End3DNow( void )
1032 __asm__ __volatile__( "femms" :: );
1036 #ifdef CAN_COMPILE_C_ALTIVEC
1037 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1038 const void *_p_s2, size_t i_bytes )
1040 uint8_t *p_dest = (uint8_t *)_p_dest;
1041 uint8_t *p_s1 = (uint8_t *)_p_s1;
1042 uint8_t *p_s2 = (uint8_t *)_p_s2;
1043 uint8_t *p_end = p_dest + i_bytes - 15;
1045 /* Use C until the first 16-bytes aligned destination pixel */
1046 while( (int)p_dest & 0xF )
1048 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1051 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1053 /* Unaligned source */
1054 vector unsigned char s1v, s2v, destv;
1055 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1056 vector unsigned char perm1v, perm2v;
1058 perm1v = vec_lvsl( 0, p_s1 );
1059 perm2v = vec_lvsl( 0, p_s2 );
1060 s1oldv = vec_ld( 0, p_s1 );
1061 s2oldv = vec_ld( 0, p_s2 );
1063 while( p_dest < p_end )
1065 s1newv = vec_ld( 16, p_s1 );
1066 s2newv = vec_ld( 16, p_s2 );
1067 s1v = vec_perm( s1oldv, s1newv, perm1v );
1068 s2v = vec_perm( s2oldv, s2newv, perm2v );
1071 destv = vec_avg( s1v, s2v );
1072 vec_st( destv, 0, p_dest );
1081 /* Aligned source */
1082 vector unsigned char s1v, s2v, destv;
1084 while( p_dest < p_end )
1086 s1v = vec_ld( 0, p_s1 );
1087 s2v = vec_ld( 0, p_s2 );
1088 destv = vec_avg( s1v, s2v );
1089 vec_st( destv, 0, p_dest );
1099 while( p_dest < p_end )
1101 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1106 /*****************************************************************************
1107 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1108 * and apply a process to recreate the bottom field :
1109 * If a 8x8 block is classified as :
1110 * - progressive: it applies a small blend (1,6,1)
1112 * * in the MMX version: we do a ME between the 2 fields, if there is a
1113 * good match we use MC to recreate the bottom field (with a small
1115 * * otherwise: it recreates the bottom field by an edge oriented
1117 *****************************************************************************/
1119 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1120 * XXX: It need to access to 8x10
1121 * We use more than 8 lines to help with scrolling (text)
1122 * (and because XDeint8x8Frame use line 9)
1123 * XXX: smooth/uniform area with noise detection doesn't works well
1124 * but it's not really a problem because they don't have much details anyway
1126 static inline int ssd( int a ) { return a*a; }
1127 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1133 /* Detect interlacing */
1135 for( y = 0; y < 7; y += 2 )
1138 for( x = 0; x < 8; x++ )
1140 fr += ssd(src[ x] - src[1*i_src+x]) +
1141 ssd(src[i_src+x] - src[2*i_src+x]);
1142 ff += ssd(src[ x] - src[2*i_src+x]) +
1143 ssd(src[i_src+x] - src[3*i_src+x]);
1145 if( ff < 6*fr/8 && fr > 32 )
1151 return fc < 1 ? false : true;
1153 #ifdef CAN_COMPILE_MMXEXT
1154 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1161 /* Detect interlacing */
1163 pxor_r2r( mm7, mm7 );
1164 for( y = 0; y < 9; y += 2 )
1167 pxor_r2r( mm5, mm5 );
1168 pxor_r2r( mm6, mm6 );
1169 for( x = 0; x < 8; x+=4 )
1171 movd_m2r( src[ x], mm0 );
1172 movd_m2r( src[1*i_src+x], mm1 );
1173 movd_m2r( src[2*i_src+x], mm2 );
1174 movd_m2r( src[3*i_src+x], mm3 );
1176 punpcklbw_r2r( mm7, mm0 );
1177 punpcklbw_r2r( mm7, mm1 );
1178 punpcklbw_r2r( mm7, mm2 );
1179 punpcklbw_r2r( mm7, mm3 );
1181 movq_r2r( mm0, mm4 );
1183 psubw_r2r( mm1, mm0 );
1184 psubw_r2r( mm2, mm4 );
1186 psubw_r2r( mm1, mm2 );
1187 psubw_r2r( mm1, mm3 );
1189 pmaddwd_r2r( mm0, mm0 );
1190 pmaddwd_r2r( mm4, mm4 );
1191 pmaddwd_r2r( mm2, mm2 );
1192 pmaddwd_r2r( mm3, mm3 );
1193 paddd_r2r( mm0, mm2 );
1194 paddd_r2r( mm4, mm3 );
1195 paddd_r2r( mm2, mm5 );
1196 paddd_r2r( mm3, mm6 );
1199 movq_r2r( mm5, mm0 );
1200 psrlq_i2r( 32, mm0 );
1201 paddd_r2r( mm0, mm5 );
1202 movd_r2m( mm5, fr );
1204 movq_r2r( mm6, mm0 );
1205 psrlq_i2r( 32, mm0 );
1206 paddd_r2r( mm0, mm6 );
1207 movd_r2m( mm6, ff );
1209 if( ff < 6*fr/8 && fr > 32 )
1218 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1219 * This won't destroy details, and help if there is a bit of interlacing.
1220 * (It helps with paning to avoid flickers)
1224 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1225 uint8_t *src, int i_src )
1230 for( y = 0; y < 8; y += 2 )
1232 memcpy( dst, src, 8 );
1235 for( x = 0; x < 8; x++ )
1236 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1242 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1243 uint8_t *src1, int i_src1,
1244 uint8_t *src2, int i_src2 )
1249 for( y = 0; y < 8; y += 2 )
1251 memcpy( dst, src1, 8 );
1254 for( x = 0; x < 8; x++ )
1255 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1263 #ifdef CAN_COMPILE_MMXEXT
1264 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1265 uint8_t *src1, int i_src1,
1266 uint8_t *src2, int i_src2 )
1268 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1272 pxor_r2r( mm7, mm7 );
1273 for( y = 0; y < 8; y += 2 )
1275 for( x = 0; x < 8; x +=4 )
1277 movd_m2r( src1[x], mm0 );
1278 movd_r2m( mm0, dst[x] );
1280 movd_m2r( src2[x], mm1 );
1281 movd_m2r( src1[i_src1+x], mm2 );
1283 punpcklbw_r2r( mm7, mm0 );
1284 punpcklbw_r2r( mm7, mm1 );
1285 punpcklbw_r2r( mm7, mm2 );
1286 paddw_r2r( mm1, mm1 );
1287 movq_r2r( mm1, mm3 );
1288 paddw_r2r( mm3, mm3 );
1289 paddw_r2r( mm2, mm0 );
1290 paddw_r2r( mm3, mm1 );
1291 paddw_m2r( m_4, mm1 );
1292 paddw_r2r( mm1, mm0 );
1293 psraw_i2r( 3, mm0 );
1294 packuswb_r2r( mm7, mm0 );
1295 movd_r2m( mm0, dst[i_dst+x] );
1306 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1309 for( y = 0; y < 8; y++ )
1310 memset( &dst[y*i_dst], v, 8 );
1313 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1316 * TODO: a better one for the inner part.
1318 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1319 uint8_t *src, int i_src )
1324 for( y = 0; y < 8; y += 2 )
1326 memcpy( dst, src, 8 );
1329 for( x = 0; x < 8; x++ )
1330 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1335 #ifdef CAN_COMPILE_MMXEXT
1336 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1337 uint8_t *src, int i_src )
1342 for( y = 0; y < 8; y += 2 )
1344 movq_m2r( src[0], mm0 );
1345 movq_r2m( mm0, dst[0] );
1348 movq_m2r( src[2*i_src], mm1 );
1349 pavgb_r2r( mm1, mm0 );
1351 movq_r2m( mm0, dst[0] );
1359 /* XDeint8x8Field: Edge oriented interpolation
1360 * (Need -4 and +5 pixels H, +1 line)
1362 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1363 uint8_t *src, int i_src )
1368 for( y = 0; y < 8; y += 2 )
1370 memcpy( dst, src, 8 );
1373 for( x = 0; x < 8; x++ )
1375 uint8_t *src2 = &src[2*i_src];
1376 /* I use 8 pixels just to match the MMX version, but it's overkill
1377 * 5 would be enough (less isn't good) */
1378 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1379 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1380 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1381 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1383 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1384 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1385 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1386 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1388 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1389 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1390 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1391 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1393 if( c0 < c1 && c1 <= c2 )
1394 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1395 else if( c2 < c1 && c1 <= c0 )
1396 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1398 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1405 #ifdef CAN_COMPILE_MMXEXT
1406 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1407 uint8_t *src, int i_src )
1412 for( y = 0; y < 8; y += 2 )
1414 memcpy( dst, src, 8 );
1417 for( x = 0; x < 8; x++ )
1419 uint8_t *src2 = &src[2*i_src];
1422 movq_m2r( src[x-2], mm0 );
1423 movq_m2r( src[x-3], mm1 );
1424 movq_m2r( src[x-4], mm2 );
1426 psadbw_m2r( src2[x-4], mm0 );
1427 psadbw_m2r( src2[x-3], mm1 );
1428 psadbw_m2r( src2[x-2], mm2 );
1430 movd_r2m( mm0, c2 );
1431 movd_r2m( mm1, c1 );
1432 movd_r2m( mm2, c0 );
1434 if( c0 < c1 && c1 <= c2 )
1435 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1436 else if( c2 < c1 && c1 <= c0 )
1437 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1439 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1449 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1450 uint8_t *pix2, int i_pix2 )
1455 for( y = 0; y < 8; y++ )
1456 for( x = 0; x < 8; x++ )
1457 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1461 #ifdef CAN_COMPILE_MMXEXT
1462 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1463 uint8_t *pix2, int i_pix2 )
1468 pxor_r2r( mm7, mm7 );
1469 pxor_r2r( mm6, mm6 );
1471 for( y = 0; y < 8; y++ )
1473 movq_m2r( pix1[0], mm0 );
1474 movq_m2r( pix2[0], mm1 );
1476 movq_r2r( mm0, mm2 );
1477 movq_r2r( mm1, mm3 );
1479 punpcklbw_r2r( mm7, mm0 );
1480 punpckhbw_r2r( mm7, mm2 );
1481 punpcklbw_r2r( mm7, mm1 );
1482 punpckhbw_r2r( mm7, mm3 );
1484 psubw_r2r( mm1, mm0 );
1485 psubw_r2r( mm3, mm2 );
1487 pmaddwd_r2r( mm0, mm0 );
1488 pmaddwd_r2r( mm2, mm2 );
1490 paddd_r2r( mm2, mm0 );
1491 paddd_r2r( mm0, mm6 );
1497 movq_r2r( mm6, mm7 );
1498 psrlq_i2r( 32, mm7 );
1499 paddd_r2r( mm6, mm7 );
1508 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1509 #ifdef CAN_COMPILE_MMXEXT
1512 * TODO: mmx version (easier in sse2)
1514 static inline void XDeintMC( uint8_t *dst, int i_dst,
1515 uint8_t *src, int i_src,
1517 int i_width, int i_height )
1519 const int d4x = mvx&0x03;
1520 const int d4y = mvy&0x03;
1522 const int cA = (4-d4x)*(4-d4y);
1523 const int cB = d4x *(4-d4y);
1524 const int cC = (4-d4x)*d4y;
1525 const int cD = d4x *d4y;
1531 src += (mvy >> 2) * i_src + (mvx >> 2);
1534 for( y = 0; y < i_height; y++ )
1536 for( x = 0; x < i_width; x++ )
1538 dst[x] = ( cA*src[x] + cB*src[x+1] +
1539 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1547 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1548 uint8_t *pix2, int i_pix2 )
1552 movq_m2r( pix1[0*i_pix1], mm0 );
1553 movq_m2r( pix1[1*i_pix1], mm1 );
1555 psadbw_m2r( pix2[0*i_pix2], mm0 );
1556 psadbw_m2r( pix2[1*i_pix2], mm1 );
1558 movq_m2r( pix1[2*i_pix1], mm2 );
1559 movq_m2r( pix1[3*i_pix1], mm3 );
1560 psadbw_m2r( pix2[2*i_pix2], mm2 );
1561 psadbw_m2r( pix2[3*i_pix2], mm3 );
1563 paddd_r2r( mm1, mm0 );
1564 paddd_r2r( mm3, mm2 );
1565 paddd_r2r( mm2, mm0 );
1571 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1572 uint8_t *ref, int i_stride,
1574 int xmax, int ymax )
1576 uint8_t buffer[8*4];
1578 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1581 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1582 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1584 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1585 uint8_t *ref, int i_stride,
1587 int xmax, int ymax )
1589 if( abs(mx) >= xmax || abs(my) >= ymax )
1592 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1595 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1596 uint8_t *src, int i_src,
1598 int xmax, int ymax )
1600 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1601 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1602 uint8_t *next = &src[i_src];
1603 const int i_src2 = 2*i_src;
1608 uint8_t *rec = &dst[i_dst];
1610 /* We construct with intra method the missing field */
1611 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1613 /* Now we will try to find a match with ME with the other field */
1615 /* ME: A small/partial EPZS
1616 * We search only for small MV (with high motion intra will be perfect */
1617 if( xmax > 4 ) xmax = 4;
1618 if( ymax > 4 ) ymax = 4;
1620 /* Init with NULL Mv */
1622 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1624 /* Try predicted Mv */
1625 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1631 /* Search interger pel (small mv) */
1632 for( i_step = 0; i_step < 4; i_step++ )
1638 for( i = 0; i < 4; i++ )
1640 s = XDeint8x4TestInt( rec, i_src2,
1641 next, i_src2, mvx+dx[i], mvy+dy[i],
1661 if( mvs > 4 && mvs < 256 )
1664 /* XXX: for now only HPEL (too slow) */
1665 for( i_step = 0; i_step < 4; i_step++ )
1671 for( i = 0; i < 8; i++ )
1673 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1674 mvx+dx[i], mvy+dy[i],
1692 uint8_t buffer[8*4];
1693 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1694 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1696 //XDeint8x8Set( dst, i_dst, 0 );
1703 /* Kernel interpolation (1,-5,20,20,-5,1)
1704 * Lose a bit more details+add aliasing than edge interpol but avoid
1707 static inline uint8_t clip1( int a )
1716 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1717 uint8_t *src, int i_src )
1722 for( y = 0; y < 8; y += 2 )
1724 const int i_src2 = i_src*2;
1726 memcpy( dst, src, 8 );
1729 for( x = 0; x < 8; x++ )
1733 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1734 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1735 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1737 dst[x] = clip1( ( pix + 16 ) >> 5 );
1747 /* NxN arbitray size (and then only use pixel in the NxN block)
1749 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1750 int i_height, int i_width )
1757 /* Detect interlacing */
1758 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1761 for( y = 0; y < i_height - 2; y += 2 )
1763 const uint8_t *s = &src[y*i_src];
1764 for( x = 0; x < i_width; x++ )
1766 fr += ssd(s[ x] - s[1*i_src+x]);
1767 ff += ssd(s[ x] - s[2*i_src+x]);
1769 if( ff < fr && fr > i_width / 2 )
1773 return fc < 2 ? false : true;
1776 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1777 uint8_t *src, int i_src,
1778 int i_width, int i_height )
1783 for( y = 0; y < i_height; y += 2 )
1785 memcpy( dst, src, i_width );
1788 if( y < i_height - 2 )
1790 for( x = 0; x < i_width; x++ )
1791 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1795 /* Blend last line */
1796 for( x = 0; x < i_width; x++ )
1797 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1804 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1805 uint8_t *src, int i_src,
1806 int i_width, int i_height )
1811 for( y = 0; y < i_height; y += 2 )
1813 memcpy( dst, src, i_width );
1816 if( y < i_height - 2 )
1818 for( x = 0; x < i_width; x++ )
1819 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1823 /* Blend last line */
1824 for( x = 0; x < i_width; x++ )
1825 dst[x] = (src[x] + src[i_src+x]) >> 1;
1832 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1833 int i_width, int i_height )
1835 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1836 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1838 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1842 static inline int median( int a, int b, int c )
1844 int min = a, max =a;
1855 return a + b + c - min - max;
1861 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1862 uint8_t *src, int i_src,
1863 const int i_mbx, int i_modx )
1867 for( x = 0; x < i_mbx; x++ )
1870 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1872 if( x == 0 || x == i_mbx - 1 )
1873 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1875 XDeint8x8FieldC( dst, i_dst, src, i_src );
1879 XDeint8x8MergeC( dst, i_dst,
1880 &src[0*i_src], 2*i_src,
1881 &src[1*i_src], 2*i_src );
1889 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1891 #ifdef CAN_COMPILE_MMXEXT
1892 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1893 uint8_t *src, int i_src,
1894 const int i_mbx, int i_modx )
1898 /* Reset current line */
1899 for( x = 0; x < i_mbx; x++ )
1902 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1904 if( x == 0 || x == i_mbx - 1 )
1905 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1907 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1911 XDeint8x8MergeMMXEXT( dst, i_dst,
1912 &src[0*i_src], 2*i_src,
1913 &src[1*i_src], 2*i_src );
1921 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1925 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1929 /* Copy image and skip lines */
1930 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1932 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1933 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1935 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1936 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1938 const int i_dst = p_outpic->p[i_plane].i_pitch;
1939 const int i_src = p_pic->p[i_plane].i_pitch;
1943 for( y = 0; y < i_mby; y++ )
1945 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1946 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1948 #ifdef CAN_COMPILE_MMXEXT
1949 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1950 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1953 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1956 /* Last line (C only)*/
1959 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1960 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1962 for( x = 0; x < i_mbx; x++ )
1964 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1971 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1975 #ifdef CAN_COMPILE_MMXEXT
1976 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1981 /*****************************************************************************
1982 * SendEvents: forward mouse and keyboard events to the parent p_vout
1983 *****************************************************************************/
1984 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1985 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1987 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
1988 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1989 vlc_value_t sentval = newval;
1991 if( !strcmp( psz_var, "mouse-y" ) )
1993 switch( p_vout->p_sys->i_mode )
1995 case DEINTERLACE_MEAN:
1996 case DEINTERLACE_DISCARD:
2002 var_Set( p_vout, psz_var, sentval );
2007 /*****************************************************************************
2008 * FilterCallback: called when changing the deinterlace method on the fly.
2009 *****************************************************************************/
2010 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
2011 vlc_value_t oldval, vlc_value_t newval,
2014 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2015 vout_thread_t * p_vout = (vout_thread_t *)p_this;
2016 int i_old_mode = p_vout->p_sys->i_mode;
2018 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
2020 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
2022 SetFilterMethod( p_vout, newval.psz_string );
2024 switch( p_vout->render.i_chroma )
2026 case VLC_FOURCC('I','4','2','2'):
2027 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2031 case VLC_FOURCC('I','4','2','0'):
2032 case VLC_FOURCC('I','Y','U','V'):
2033 case VLC_FOURCC('Y','V','1','2'):
2034 switch( p_vout->p_sys->i_mode )
2036 case DEINTERLACE_MEAN:
2037 case DEINTERLACE_DISCARD:
2038 if( ( i_old_mode == DEINTERLACE_MEAN )
2039 || ( i_old_mode == DEINTERLACE_DISCARD ) )
2041 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2046 case DEINTERLACE_BOB:
2047 case DEINTERLACE_BLEND:
2048 case DEINTERLACE_LINEAR:
2049 if( ( i_old_mode == DEINTERLACE_BOB )
2050 || ( i_old_mode == DEINTERLACE_BLEND )
2051 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2053 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2064 /* We need to kill the old vout */
2066 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2068 vlc_object_detach( p_vout->p_sys->p_vout );
2069 vout_Destroy( p_vout->p_sys->p_vout );
2071 /* Try to open a new video output */
2072 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2074 if( p_vout->p_sys->p_vout == NULL )
2076 /* Everything failed */
2077 msg_Err( p_vout, "cannot open vout, aborting" );
2079 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2080 return VLC_EGENERIC;
2083 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2085 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2089 /*****************************************************************************
2090 * SendEventsToChild: forward events to the child/children vout
2091 *****************************************************************************/
2092 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2093 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2095 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2096 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2097 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2102 /*****************************************************************************
2103 * video filter2 functions
2104 *****************************************************************************/
2105 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2107 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2108 picture_t *p_pic_dst;
2110 /* Request output picture */
2111 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
2112 if( p_pic_dst == NULL )
2114 msg_Warn( p_filter, "can't get output picture" );
2118 switch( p_vout->p_sys->i_mode )
2120 case DEINTERLACE_DISCARD:
2122 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2124 msg_Err( p_vout, "discarding lines is not supported yet" );
2125 p_pic_dst->pf_release( p_pic_dst );
2129 case DEINTERLACE_BOB:
2131 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2132 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2136 case DEINTERLACE_LINEAR:
2138 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2139 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2141 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2142 p_pic_dst->pf_release( p_pic_dst );
2146 case DEINTERLACE_MEAN:
2147 RenderMean( p_vout, p_pic_dst, p_pic );
2150 case DEINTERLACE_BLEND:
2151 RenderBlend( p_vout, p_pic_dst, p_pic );
2155 RenderX( p_pic_dst, p_pic );
2159 p_pic_dst->date = p_pic->date;
2160 p_pic_dst->b_force = p_pic->b_force;
2161 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2162 p_pic_dst->b_progressive = true;
2163 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2165 p_pic->pf_release( p_pic );
2169 /*****************************************************************************
2171 *****************************************************************************/
2172 static int OpenFilter( vlc_object_t *p_this )
2174 filter_t *p_filter = (filter_t*)p_this;
2175 vout_thread_t *p_vout;
2178 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2179 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2180 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2181 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2183 return VLC_EGENERIC;
2186 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2188 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2189 vlc_object_attach( p_vout, p_filter );
2190 p_filter->p_sys = (filter_sys_t *)p_vout;
2191 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2193 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2195 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2196 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2197 var_Set( p_filter, "deinterlace-mode", val );
2199 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2201 vlc_object_detach( p_vout );
2202 vlc_object_release( p_vout );
2203 return VLC_EGENERIC;
2206 p_filter->pf_video_filter = Deinterlace;
2208 msg_Dbg( p_filter, "deinterlacing" );
2213 /*****************************************************************************
2214 * CloseFilter: clean up the filter
2215 *****************************************************************************/
2216 static void CloseFilter( vlc_object_t *p_this )
2218 filter_t *p_filter = (filter_t*)p_this;
2219 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2221 Destroy( VLC_OBJECT(p_vout) );
2222 vlc_object_detach( p_vout );
2223 vlc_object_release( p_vout );