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 *****************************************************************************/
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
42 #include "vlc_filter.h"
44 #ifdef CAN_COMPILE_MMXEXT
48 #include "filter_common.h"
50 #define DEINTERLACE_DISCARD 1
51 #define DEINTERLACE_MEAN 2
52 #define DEINTERLACE_BLEND 3
53 #define DEINTERLACE_BOB 4
54 #define DEINTERLACE_LINEAR 5
55 #define DEINTERLACE_X 6
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Create ( vlc_object_t * );
61 static void Destroy ( vlc_object_t * );
63 static int Init ( vout_thread_t * );
64 static void End ( vout_thread_t * );
65 static void Render ( vout_thread_t *, picture_t * );
67 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
68 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
69 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
70 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
71 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
72 static void RenderX ( picture_t *, picture_t * );
74 static void MergeGeneric ( void *, const void *, const void *, size_t );
75 #if defined(CAN_COMPILE_C_ALTIVEC)
76 static void MergeAltivec ( void *, const void *, const void *, size_t );
78 #if defined(CAN_COMPILE_MMXEXT)
79 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
81 #if defined(CAN_COMPILE_3DNOW)
82 static void Merge3DNow ( void *, const void *, const void *, size_t );
84 #if defined(CAN_COMPILE_SSE)
85 static void MergeSSE2 ( void *, const void *, const void *, size_t );
87 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
88 static void EndMMX ( void );
90 #if defined(CAN_COMPILE_3DNOW)
91 static void End3DNow ( void );
94 static int SendEvents ( vlc_object_t *, char const *,
95 vlc_value_t, vlc_value_t, void * );
97 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
98 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
100 static int OpenFilter( vlc_object_t *p_this );
101 static void CloseFilter( vlc_object_t *p_this );
103 /*****************************************************************************
104 * Callback prototypes
105 *****************************************************************************/
106 static int FilterCallback ( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 /*****************************************************************************
111 *****************************************************************************/
112 #define MODE_TEXT N_("Deinterlace mode")
113 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
115 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
116 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
118 #define FILTER_CFG_PREFIX "sout-deinterlace-"
120 static const char *const mode_list[] = {
121 "discard", "blend", "mean", "bob", "linear", "x" };
122 static const char *const mode_list_text[] = {
123 N_("Discard"), N_("Blend"), N_("Mean"), N_("Bob"), N_("Linear"), "X" };
126 set_description( N_("Deinterlacing video filter") );
127 set_shortname( N_("Deinterlace" ));
128 set_capability( "video filter", 0 );
129 set_category( CAT_VIDEO );
130 set_subcategory( SUBCAT_VIDEO_VFILTER );
132 set_section( N_("Display"),NULL);
133 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
134 MODE_LONGTEXT, false );
135 change_string_list( mode_list, mode_list_text, 0 );
137 add_shortcut( "deinterlace" );
138 set_callbacks( Create, Destroy );
141 set_capability( "video filter2", 0 );
142 set_section( N_("Streaming"),NULL);
143 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
144 SOUT_MODE_LONGTEXT, false );
145 change_string_list( mode_list, mode_list_text, 0 );
146 set_callbacks( OpenFilter, CloseFilter );
149 static const char *const ppsz_filter_options[] = {
153 /*****************************************************************************
154 * vout_sys_t: Deinterlace video output method descriptor
155 *****************************************************************************
156 * This structure is part of the video output thread descriptor.
157 * It describes the Deinterlace specific properties of an output thread.
158 *****************************************************************************/
161 int i_mode; /* Deinterlace mode */
162 bool b_double_rate; /* Shall we double the framerate? */
163 bool b_half_height; /* Shall be devide the height by 2 */
168 vout_thread_t *p_vout;
170 vlc_mutex_t filter_lock;
172 void (*pf_merge) ( void *, const void *, const void *, size_t );
173 void (*pf_end_merge) ( void );
176 /*****************************************************************************
177 * Control: control facility for the vout (forwards to child vout)
178 *****************************************************************************/
179 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
181 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
184 /*****************************************************************************
185 * Create: allocates Deinterlace video thread output method
186 *****************************************************************************
187 * This function allocates and initializes a Deinterlace vout method.
188 *****************************************************************************/
189 static int Create( vlc_object_t *p_this )
191 vout_thread_t *p_vout = (vout_thread_t *)p_this;
194 /* Allocate structure */
195 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
196 if( p_vout->p_sys == NULL )
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->b_half_height = true;
209 p_vout->p_sys->last_date = 0;
210 p_vout->p_sys->p_vout = 0;
211 vlc_mutex_init( &p_vout->p_sys->filter_lock );
213 #if defined(CAN_COMPILE_C_ALTIVEC)
214 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
216 p_vout->p_sys->pf_merge = MergeAltivec;
217 p_vout->p_sys->pf_end_merge = NULL;
221 #if defined(CAN_COMPILE_SSE)
222 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
224 p_vout->p_sys->pf_merge = MergeSSE2;
225 p_vout->p_sys->pf_end_merge = EndMMX;
229 #if defined(CAN_COMPILE_MMXEXT)
230 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
232 p_vout->p_sys->pf_merge = MergeMMXEXT;
233 p_vout->p_sys->pf_end_merge = EndMMX;
237 #if defined(CAN_COMPILE_3DNOW)
238 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
240 p_vout->p_sys->pf_merge = Merge3DNow;
241 p_vout->p_sys->pf_end_merge = End3DNow;
246 p_vout->p_sys->pf_merge = MergeGeneric;
247 p_vout->p_sys->pf_end_merge = NULL;
250 /* Look what method was requested */
251 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
252 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
254 if( val.psz_string == NULL )
256 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
257 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
259 val.psz_string = strdup( "discard" );
262 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
264 SetFilterMethod( p_vout, val.psz_string );
266 free( val.psz_string );
271 /*****************************************************************************
272 * SetFilterMethod: setup the deinterlace method to use.
273 *****************************************************************************/
274 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
276 if( !strcmp( psz_method, "mean" ) )
278 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
279 p_vout->p_sys->b_double_rate = false;
280 p_vout->p_sys->b_half_height = true;
282 else if( !strcmp( psz_method, "blend" )
283 || !strcmp( psz_method, "average" )
284 || !strcmp( psz_method, "combine-fields" ) )
286 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
287 p_vout->p_sys->b_double_rate = false;
288 p_vout->p_sys->b_half_height = false;
290 else if( !strcmp( psz_method, "bob" )
291 || !strcmp( psz_method, "progressive-scan" ) )
293 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
294 p_vout->p_sys->b_double_rate = true;
295 p_vout->p_sys->b_half_height = false;
297 else if( !strcmp( psz_method, "linear" ) )
299 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
300 p_vout->p_sys->b_double_rate = true;
301 p_vout->p_sys->b_half_height = false;
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;
307 p_vout->p_sys->b_half_height = false;
311 if( strcmp( psz_method, "discard" ) )
312 msg_Err( p_vout, "no valid deinterlace mode provided, "
313 "using \"discard\"" );
315 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
316 p_vout->p_sys->b_double_rate = false;
317 p_vout->p_sys->b_half_height = true;
320 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
323 /*****************************************************************************
324 * Init: initialize Deinterlace video thread output method
325 *****************************************************************************/
326 static int Init( vout_thread_t *p_vout )
331 I_OUTPUTPICTURES = 0;
333 /* Initialize the output structure, full of directbuffers since we want
334 * the decoder to output directly to our structures. */
335 switch( p_vout->render.i_chroma )
337 case VLC_FOURCC('I','4','2','0'):
338 case VLC_FOURCC('I','Y','U','V'):
339 case VLC_FOURCC('Y','V','1','2'):
340 case VLC_FOURCC('I','4','2','2'):
341 p_vout->output.i_chroma = p_vout->render.i_chroma;
342 p_vout->output.i_width = p_vout->render.i_width;
343 p_vout->output.i_height = p_vout->render.i_height;
344 p_vout->output.i_aspect = p_vout->render.i_aspect;
345 p_vout->fmt_out = p_vout->fmt_in;
349 return VLC_EGENERIC; /* unknown chroma */
353 /* Try to open the real video output */
354 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
356 if( p_vout->p_sys->p_vout == NULL )
358 /* Everything failed */
359 msg_Err( p_vout, "cannot open vout, aborting" );
364 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
366 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
368 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
370 ADD_PARENT_CALLBACKS( SendEventsToChild );
375 /*****************************************************************************
376 * SpawnRealVout: spawn the real video output.
377 *****************************************************************************/
378 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
380 vout_thread_t *p_real_vout = NULL;
382 memset( &fmt, 0, sizeof( video_format_t ) );
384 msg_Dbg( p_vout, "spawning the real video output" );
386 fmt = p_vout->fmt_out;
388 switch( p_vout->render.i_chroma )
390 case VLC_FOURCC('I','4','2','0'):
391 case VLC_FOURCC('I','Y','U','V'):
392 case VLC_FOURCC('Y','V','1','2'):
393 if( p_vout->p_sys->b_half_height )
395 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
398 p_real_vout = vout_Create( p_vout, &fmt );
401 case VLC_FOURCC('I','4','2','2'):
402 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
403 p_real_vout = vout_Create( p_vout, &fmt );
413 /*****************************************************************************
414 * End: terminate Deinterlace video thread output method
415 *****************************************************************************/
416 static void End( vout_thread_t *p_vout )
420 DEL_PARENT_CALLBACKS( SendEventsToChild );
422 if( p_vout->p_sys->p_vout )
423 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
425 /* Free the fake output buffers we allocated */
426 for( i_index = I_OUTPUTPICTURES ; i_index ; )
429 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
432 if( p_vout->p_sys->p_vout )
433 vout_CloseAndRelease( p_vout->p_sys->p_vout );
436 /*****************************************************************************
437 * Destroy: destroy Deinterlace video thread output method
438 *****************************************************************************
439 * Terminate an output method created by DeinterlaceCreateOutputMethod
440 *****************************************************************************/
441 static void Destroy( vlc_object_t *p_this )
443 vout_thread_t *p_vout = (vout_thread_t *)p_this;
444 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
445 free( p_vout->p_sys );
448 /*****************************************************************************
449 * Render: displays previously rendered output
450 *****************************************************************************
451 * This function send the currently rendered image to Deinterlace image,
452 * waits until it is displayed and switch the two rendering buffers, preparing
454 *****************************************************************************/
455 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
457 vout_sys_t *p_sys = p_vout->p_sys;
458 picture_t *pp_outpic[2];
460 /* FIXME are they needed ? */
461 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
462 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
463 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
464 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
466 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
467 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
468 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
469 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
470 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
471 if( p_vout->p_sys->b_half_height )
473 p_sys->p_vout->fmt_in.i_y_offset /= 2;
474 p_sys->p_vout->fmt_in.i_visible_height /= 2;
477 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
479 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
481 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
482 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
483 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
485 video_format_t fmt = p_vout->fmt_out;
486 if( p_vout->p_sys->b_half_height )
488 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
492 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
497 pp_outpic[0] = pp_outpic[1] = NULL;
499 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
501 /* Get a new picture */
502 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
506 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
508 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
511 msleep( VOUT_OUTMEM_SLEEP );
514 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
516 /* If we are using double rate, get an additional new picture */
517 if( p_vout->p_sys->b_double_rate )
519 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
523 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
525 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
526 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
529 msleep( VOUT_OUTMEM_SLEEP );
532 /* 20ms is a bit arbitrary, but it's only for the first image we get */
533 if( !p_vout->p_sys->last_date )
535 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
536 p_pic->date + 20000 );
540 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
541 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
543 p_vout->p_sys->last_date = p_pic->date;
546 switch( p_vout->p_sys->i_mode )
548 case DEINTERLACE_DISCARD:
549 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
550 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
553 case DEINTERLACE_BOB:
554 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
555 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
556 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
557 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
560 case DEINTERLACE_LINEAR:
561 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
562 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
563 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
564 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
567 case DEINTERLACE_MEAN:
568 RenderMean( p_vout, pp_outpic[0], p_pic );
569 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
572 case DEINTERLACE_BLEND:
573 RenderBlend( p_vout, pp_outpic[0], p_pic );
574 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
578 RenderX( pp_outpic[0], p_pic );
579 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
582 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
585 /*****************************************************************************
586 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
587 *****************************************************************************/
588 static void RenderDiscard( vout_thread_t *p_vout,
589 picture_t *p_outpic, picture_t *p_pic, int i_field )
593 /* Copy image and skip lines */
594 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
596 uint8_t *p_in, *p_out_end, *p_out;
599 p_in = p_pic->p[i_plane].p_pixels
600 + i_field * p_pic->p[i_plane].i_pitch;
602 p_out = p_outpic->p[i_plane].p_pixels;
603 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
604 * p_outpic->p[i_plane].i_visible_lines;
606 switch( p_vout->render.i_chroma )
608 case VLC_FOURCC('I','4','2','0'):
609 case VLC_FOURCC('I','Y','U','V'):
610 case VLC_FOURCC('Y','V','1','2'):
612 for( ; p_out < p_out_end ; )
614 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
616 p_out += p_outpic->p[i_plane].i_pitch;
617 p_in += 2 * p_pic->p[i_plane].i_pitch;
621 case VLC_FOURCC('I','4','2','2'):
623 i_increment = 2 * p_pic->p[i_plane].i_pitch;
625 if( i_plane == Y_PLANE )
627 for( ; p_out < p_out_end ; )
629 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
630 p_out += p_outpic->p[i_plane].i_pitch;
631 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
632 p_out += p_outpic->p[i_plane].i_pitch;
638 for( ; p_out < p_out_end ; )
640 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
641 p_out += p_outpic->p[i_plane].i_pitch;
653 /*****************************************************************************
654 * RenderBob: renders a BOB picture - simple copy
655 *****************************************************************************/
656 static void RenderBob( vout_thread_t *p_vout,
657 picture_t *p_outpic, picture_t *p_pic, int i_field )
661 /* Copy image and skip lines */
662 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
664 uint8_t *p_in, *p_out_end, *p_out;
666 p_in = p_pic->p[i_plane].p_pixels;
667 p_out = p_outpic->p[i_plane].p_pixels;
668 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
669 * p_outpic->p[i_plane].i_visible_lines;
671 switch( p_vout->render.i_chroma )
673 case VLC_FOURCC('I','4','2','0'):
674 case VLC_FOURCC('I','Y','U','V'):
675 case VLC_FOURCC('Y','V','1','2'):
676 /* For BOTTOM field we need to add the first line */
679 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
680 p_in += p_pic->p[i_plane].i_pitch;
681 p_out += p_outpic->p[i_plane].i_pitch;
684 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
686 for( ; p_out < p_out_end ; )
688 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
690 p_out += p_outpic->p[i_plane].i_pitch;
692 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
694 p_in += 2 * p_pic->p[i_plane].i_pitch;
695 p_out += p_outpic->p[i_plane].i_pitch;
698 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
700 /* For TOP field we need to add the last line */
703 p_in += p_pic->p[i_plane].i_pitch;
704 p_out += p_outpic->p[i_plane].i_pitch;
705 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
709 case VLC_FOURCC('I','4','2','2'):
710 /* For BOTTOM field we need to add the first line */
713 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
714 p_in += p_pic->p[i_plane].i_pitch;
715 p_out += p_outpic->p[i_plane].i_pitch;
718 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
720 if( i_plane == Y_PLANE )
722 for( ; p_out < p_out_end ; )
724 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
726 p_out += p_outpic->p[i_plane].i_pitch;
728 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
730 p_in += 2 * p_pic->p[i_plane].i_pitch;
731 p_out += p_outpic->p[i_plane].i_pitch;
736 for( ; p_out < p_out_end ; )
738 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
740 p_out += p_outpic->p[i_plane].i_pitch;
741 p_in += 2 * p_pic->p[i_plane].i_pitch;
745 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
747 /* For TOP field we need to add the last line */
750 p_in += p_pic->p[i_plane].i_pitch;
751 p_out += p_outpic->p[i_plane].i_pitch;
752 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
759 #define Merge p_vout->p_sys->pf_merge
760 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
762 /*****************************************************************************
763 * RenderLinear: BOB with linear interpolation
764 *****************************************************************************/
765 static void RenderLinear( vout_thread_t *p_vout,
766 picture_t *p_outpic, picture_t *p_pic, int i_field )
770 /* Copy image and skip lines */
771 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
773 uint8_t *p_in, *p_out_end, *p_out;
775 p_in = p_pic->p[i_plane].p_pixels;
776 p_out = p_outpic->p[i_plane].p_pixels;
777 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
778 * p_outpic->p[i_plane].i_visible_lines;
780 /* For BOTTOM field we need to add the first line */
783 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
784 p_in += p_pic->p[i_plane].i_pitch;
785 p_out += p_outpic->p[i_plane].i_pitch;
788 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
790 for( ; p_out < p_out_end ; )
792 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
794 p_out += p_outpic->p[i_plane].i_pitch;
796 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
797 p_pic->p[i_plane].i_pitch );
799 p_in += 2 * p_pic->p[i_plane].i_pitch;
800 p_out += p_outpic->p[i_plane].i_pitch;
803 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
805 /* For TOP field we need to add the last line */
808 p_in += p_pic->p[i_plane].i_pitch;
809 p_out += p_outpic->p[i_plane].i_pitch;
810 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
816 static void RenderMean( vout_thread_t *p_vout,
817 picture_t *p_outpic, picture_t *p_pic )
821 /* Copy image and skip lines */
822 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
824 uint8_t *p_in, *p_out_end, *p_out;
826 p_in = p_pic->p[i_plane].p_pixels;
828 p_out = p_outpic->p[i_plane].p_pixels;
829 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
830 * p_outpic->p[i_plane].i_visible_lines;
832 /* All lines: mean value */
833 for( ; p_out < p_out_end ; )
835 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
836 p_pic->p[i_plane].i_pitch );
838 p_out += p_outpic->p[i_plane].i_pitch;
839 p_in += 2 * p_pic->p[i_plane].i_pitch;
845 static void RenderBlend( vout_thread_t *p_vout,
846 picture_t *p_outpic, picture_t *p_pic )
850 /* Copy image and skip lines */
851 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
853 uint8_t *p_in, *p_out_end, *p_out;
855 p_in = p_pic->p[i_plane].p_pixels;
857 p_out = p_outpic->p[i_plane].p_pixels;
858 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
859 * p_outpic->p[i_plane].i_visible_lines;
861 switch( p_vout->render.i_chroma )
863 case VLC_FOURCC('I','4','2','0'):
864 case VLC_FOURCC('I','Y','U','V'):
865 case VLC_FOURCC('Y','V','1','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 for( ; p_out < p_out_end ; )
873 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
874 p_pic->p[i_plane].i_pitch );
876 p_out += p_outpic->p[i_plane].i_pitch;
877 p_in += p_pic->p[i_plane].i_pitch;
881 case VLC_FOURCC('I','4','2','2'):
882 /* First line: simple copy */
883 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
884 p_out += p_outpic->p[i_plane].i_pitch;
886 /* Remaining lines: mean value */
887 if( i_plane == Y_PLANE )
889 for( ; p_out < p_out_end ; )
891 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
892 p_pic->p[i_plane].i_pitch );
894 p_out += p_outpic->p[i_plane].i_pitch;
895 p_in += p_pic->p[i_plane].i_pitch;
901 for( ; p_out < p_out_end ; )
903 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
904 p_pic->p[i_plane].i_pitch );
906 p_out += p_outpic->p[i_plane].i_pitch;
907 p_in += 2*p_pic->p[i_plane].i_pitch;
918 static void MergeGeneric( void *_p_dest, const void *_p_s1,
919 const void *_p_s2, size_t i_bytes )
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;
926 while( p_dest < p_end )
928 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
929 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
930 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
931 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
932 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
933 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
934 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
935 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
940 while( p_dest < p_end )
942 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
946 #if defined(CAN_COMPILE_MMXEXT)
947 static void MergeMMXEXT( 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;
953 uint8_t* p_end = p_dest + i_bytes - 8;
954 while( p_dest < p_end )
956 __asm__ __volatile__( "movq %2,%%mm1;"
958 "movq %%mm1, %0" :"=m" (*p_dest):
968 while( p_dest < p_end )
970 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
975 #if defined(CAN_COMPILE_3DNOW)
976 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
979 uint8_t* p_dest = (uint8_t*)_p_dest;
980 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
981 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
982 uint8_t* p_end = p_dest + i_bytes - 8;
983 while( p_dest < p_end )
985 __asm__ __volatile__( "movq %2,%%mm1;"
987 "movq %%mm1, %0" :"=m" (*p_dest):
997 while( p_dest < p_end )
999 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1004 #if defined(CAN_COMPILE_SSE)
1005 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1008 uint8_t* p_dest = (uint8_t*)_p_dest;
1009 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1010 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1012 while( (uintptr_t)p_s1 % 16 )
1014 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1016 p_end = p_dest + i_bytes - 16;
1017 while( p_dest < p_end )
1019 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1021 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1031 while( p_dest < p_end )
1033 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1038 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1039 static void EndMMX( void )
1041 __asm__ __volatile__( "emms" :: );
1045 #if defined(CAN_COMPILE_3DNOW)
1046 static void End3DNow( void )
1048 __asm__ __volatile__( "femms" :: );
1052 #ifdef CAN_COMPILE_C_ALTIVEC
1053 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1054 const void *_p_s2, size_t i_bytes )
1056 uint8_t *p_dest = (uint8_t *)_p_dest;
1057 uint8_t *p_s1 = (uint8_t *)_p_s1;
1058 uint8_t *p_s2 = (uint8_t *)_p_s2;
1059 uint8_t *p_end = p_dest + i_bytes - 15;
1061 /* Use C until the first 16-bytes aligned destination pixel */
1062 while( (int)p_dest & 0xF )
1064 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1067 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1069 /* Unaligned source */
1070 vector unsigned char s1v, s2v, destv;
1071 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1072 vector unsigned char perm1v, perm2v;
1074 perm1v = vec_lvsl( 0, p_s1 );
1075 perm2v = vec_lvsl( 0, p_s2 );
1076 s1oldv = vec_ld( 0, p_s1 );
1077 s2oldv = vec_ld( 0, p_s2 );
1079 while( p_dest < p_end )
1081 s1newv = vec_ld( 16, p_s1 );
1082 s2newv = vec_ld( 16, p_s2 );
1083 s1v = vec_perm( s1oldv, s1newv, perm1v );
1084 s2v = vec_perm( s2oldv, s2newv, perm2v );
1087 destv = vec_avg( s1v, s2v );
1088 vec_st( destv, 0, p_dest );
1097 /* Aligned source */
1098 vector unsigned char s1v, s2v, destv;
1100 while( p_dest < p_end )
1102 s1v = vec_ld( 0, p_s1 );
1103 s2v = vec_ld( 0, p_s2 );
1104 destv = vec_avg( s1v, s2v );
1105 vec_st( destv, 0, p_dest );
1115 while( p_dest < p_end )
1117 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1122 /*****************************************************************************
1123 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1124 * and apply a process to recreate the bottom field :
1125 * If a 8x8 block is classified as :
1126 * - progressive: it applies a small blend (1,6,1)
1128 * * in the MMX version: we do a ME between the 2 fields, if there is a
1129 * good match we use MC to recreate the bottom field (with a small
1131 * * otherwise: it recreates the bottom field by an edge oriented
1133 *****************************************************************************/
1135 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1136 * XXX: It need to access to 8x10
1137 * We use more than 8 lines to help with scrolling (text)
1138 * (and because XDeint8x8Frame use line 9)
1139 * XXX: smooth/uniform area with noise detection doesn't works well
1140 * but it's not really a problem because they don't have much details anyway
1142 static inline int ssd( int a ) { return a*a; }
1143 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1149 /* Detect interlacing */
1151 for( y = 0; y < 7; y += 2 )
1154 for( x = 0; x < 8; x++ )
1156 fr += ssd(src[ x] - src[1*i_src+x]) +
1157 ssd(src[i_src+x] - src[2*i_src+x]);
1158 ff += ssd(src[ x] - src[2*i_src+x]) +
1159 ssd(src[i_src+x] - src[3*i_src+x]);
1161 if( ff < 6*fr/8 && fr > 32 )
1167 return fc < 1 ? false : true;
1169 #ifdef CAN_COMPILE_MMXEXT
1170 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1177 /* Detect interlacing */
1179 pxor_r2r( mm7, mm7 );
1180 for( y = 0; y < 9; y += 2 )
1183 pxor_r2r( mm5, mm5 );
1184 pxor_r2r( mm6, mm6 );
1185 for( x = 0; x < 8; x+=4 )
1187 movd_m2r( src[ x], mm0 );
1188 movd_m2r( src[1*i_src+x], mm1 );
1189 movd_m2r( src[2*i_src+x], mm2 );
1190 movd_m2r( src[3*i_src+x], mm3 );
1192 punpcklbw_r2r( mm7, mm0 );
1193 punpcklbw_r2r( mm7, mm1 );
1194 punpcklbw_r2r( mm7, mm2 );
1195 punpcklbw_r2r( mm7, mm3 );
1197 movq_r2r( mm0, mm4 );
1199 psubw_r2r( mm1, mm0 );
1200 psubw_r2r( mm2, mm4 );
1202 psubw_r2r( mm1, mm2 );
1203 psubw_r2r( mm1, mm3 );
1205 pmaddwd_r2r( mm0, mm0 );
1206 pmaddwd_r2r( mm4, mm4 );
1207 pmaddwd_r2r( mm2, mm2 );
1208 pmaddwd_r2r( mm3, mm3 );
1209 paddd_r2r( mm0, mm2 );
1210 paddd_r2r( mm4, mm3 );
1211 paddd_r2r( mm2, mm5 );
1212 paddd_r2r( mm3, mm6 );
1215 movq_r2r( mm5, mm0 );
1216 psrlq_i2r( 32, mm0 );
1217 paddd_r2r( mm0, mm5 );
1218 movd_r2m( mm5, fr );
1220 movq_r2r( mm6, mm0 );
1221 psrlq_i2r( 32, mm0 );
1222 paddd_r2r( mm0, mm6 );
1223 movd_r2m( mm6, ff );
1225 if( ff < 6*fr/8 && fr > 32 )
1234 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1235 * This won't destroy details, and help if there is a bit of interlacing.
1236 * (It helps with paning to avoid flickers)
1240 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1241 uint8_t *src, int i_src )
1246 for( y = 0; y < 8; y += 2 )
1248 memcpy( dst, src, 8 );
1251 for( x = 0; x < 8; x++ )
1252 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1258 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1259 uint8_t *src1, int i_src1,
1260 uint8_t *src2, int i_src2 )
1265 for( y = 0; y < 8; y += 2 )
1267 memcpy( dst, src1, 8 );
1270 for( x = 0; x < 8; x++ )
1271 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1279 #ifdef CAN_COMPILE_MMXEXT
1280 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1281 uint8_t *src1, int i_src1,
1282 uint8_t *src2, int i_src2 )
1284 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1288 pxor_r2r( mm7, mm7 );
1289 for( y = 0; y < 8; y += 2 )
1291 for( x = 0; x < 8; x +=4 )
1293 movd_m2r( src1[x], mm0 );
1294 movd_r2m( mm0, dst[x] );
1296 movd_m2r( src2[x], mm1 );
1297 movd_m2r( src1[i_src1+x], mm2 );
1299 punpcklbw_r2r( mm7, mm0 );
1300 punpcklbw_r2r( mm7, mm1 );
1301 punpcklbw_r2r( mm7, mm2 );
1302 paddw_r2r( mm1, mm1 );
1303 movq_r2r( mm1, mm3 );
1304 paddw_r2r( mm3, mm3 );
1305 paddw_r2r( mm2, mm0 );
1306 paddw_r2r( mm3, mm1 );
1307 paddw_m2r( m_4, mm1 );
1308 paddw_r2r( mm1, mm0 );
1309 psraw_i2r( 3, mm0 );
1310 packuswb_r2r( mm7, mm0 );
1311 movd_r2m( mm0, dst[i_dst+x] );
1322 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1325 for( y = 0; y < 8; y++ )
1326 memset( &dst[y*i_dst], v, 8 );
1329 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1332 * TODO: a better one for the inner part.
1334 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1335 uint8_t *src, int i_src )
1340 for( y = 0; y < 8; y += 2 )
1342 memcpy( dst, src, 8 );
1345 for( x = 0; x < 8; x++ )
1346 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1351 #ifdef CAN_COMPILE_MMXEXT
1352 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1353 uint8_t *src, int i_src )
1358 for( y = 0; y < 8; y += 2 )
1360 movq_m2r( src[0], mm0 );
1361 movq_r2m( mm0, dst[0] );
1364 movq_m2r( src[2*i_src], mm1 );
1365 pavgb_r2r( mm1, mm0 );
1367 movq_r2m( mm0, dst[0] );
1375 /* XDeint8x8Field: Edge oriented interpolation
1376 * (Need -4 and +5 pixels H, +1 line)
1378 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1379 uint8_t *src, int i_src )
1384 for( y = 0; y < 8; y += 2 )
1386 memcpy( dst, src, 8 );
1389 for( x = 0; x < 8; x++ )
1391 uint8_t *src2 = &src[2*i_src];
1392 /* I use 8 pixels just to match the MMX version, but it's overkill
1393 * 5 would be enough (less isn't good) */
1394 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1395 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1396 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1397 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1399 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1400 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1401 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1402 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1404 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1405 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1406 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1407 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1409 if( c0 < c1 && c1 <= c2 )
1410 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1411 else if( c2 < c1 && c1 <= c0 )
1412 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1414 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1421 #ifdef CAN_COMPILE_MMXEXT
1422 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1423 uint8_t *src, int i_src )
1428 for( y = 0; y < 8; y += 2 )
1430 memcpy( dst, src, 8 );
1433 for( x = 0; x < 8; x++ )
1435 uint8_t *src2 = &src[2*i_src];
1438 movq_m2r( src[x-2], mm0 );
1439 movq_m2r( src[x-3], mm1 );
1440 movq_m2r( src[x-4], mm2 );
1442 psadbw_m2r( src2[x-4], mm0 );
1443 psadbw_m2r( src2[x-3], mm1 );
1444 psadbw_m2r( src2[x-2], mm2 );
1446 movd_r2m( mm0, c2 );
1447 movd_r2m( mm1, c1 );
1448 movd_r2m( mm2, c0 );
1450 if( c0 < c1 && c1 <= c2 )
1451 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1452 else if( c2 < c1 && c1 <= c0 )
1453 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1455 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1465 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1466 uint8_t *pix2, int i_pix2 )
1471 for( y = 0; y < 8; y++ )
1472 for( x = 0; x < 8; x++ )
1473 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1477 #ifdef CAN_COMPILE_MMXEXT
1478 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1479 uint8_t *pix2, int i_pix2 )
1484 pxor_r2r( mm7, mm7 );
1485 pxor_r2r( mm6, mm6 );
1487 for( y = 0; y < 8; y++ )
1489 movq_m2r( pix1[0], mm0 );
1490 movq_m2r( pix2[0], mm1 );
1492 movq_r2r( mm0, mm2 );
1493 movq_r2r( mm1, mm3 );
1495 punpcklbw_r2r( mm7, mm0 );
1496 punpckhbw_r2r( mm7, mm2 );
1497 punpcklbw_r2r( mm7, mm1 );
1498 punpckhbw_r2r( mm7, mm3 );
1500 psubw_r2r( mm1, mm0 );
1501 psubw_r2r( mm3, mm2 );
1503 pmaddwd_r2r( mm0, mm0 );
1504 pmaddwd_r2r( mm2, mm2 );
1506 paddd_r2r( mm2, mm0 );
1507 paddd_r2r( mm0, mm6 );
1513 movq_r2r( mm6, mm7 );
1514 psrlq_i2r( 32, mm7 );
1515 paddd_r2r( mm6, mm7 );
1524 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1525 #ifdef CAN_COMPILE_MMXEXT
1528 * TODO: mmx version (easier in sse2)
1530 static inline void XDeintMC( uint8_t *dst, int i_dst,
1531 uint8_t *src, int i_src,
1533 int i_width, int i_height )
1535 const int d4x = mvx&0x03;
1536 const int d4y = mvy&0x03;
1538 const int cA = (4-d4x)*(4-d4y);
1539 const int cB = d4x *(4-d4y);
1540 const int cC = (4-d4x)*d4y;
1541 const int cD = d4x *d4y;
1547 src += (mvy >> 2) * i_src + (mvx >> 2);
1550 for( y = 0; y < i_height; y++ )
1552 for( x = 0; x < i_width; x++ )
1554 dst[x] = ( cA*src[x] + cB*src[x+1] +
1555 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1563 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1564 uint8_t *pix2, int i_pix2 )
1568 movq_m2r( pix1[0*i_pix1], mm0 );
1569 movq_m2r( pix1[1*i_pix1], mm1 );
1571 psadbw_m2r( pix2[0*i_pix2], mm0 );
1572 psadbw_m2r( pix2[1*i_pix2], mm1 );
1574 movq_m2r( pix1[2*i_pix1], mm2 );
1575 movq_m2r( pix1[3*i_pix1], mm3 );
1576 psadbw_m2r( pix2[2*i_pix2], mm2 );
1577 psadbw_m2r( pix2[3*i_pix2], mm3 );
1579 paddd_r2r( mm1, mm0 );
1580 paddd_r2r( mm3, mm2 );
1581 paddd_r2r( mm2, mm0 );
1587 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1588 uint8_t *ref, int i_stride,
1590 int xmax, int ymax )
1592 uint8_t buffer[8*4];
1594 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1597 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1598 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1600 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1601 uint8_t *ref, int i_stride,
1603 int xmax, int ymax )
1605 if( abs(mx) >= xmax || abs(my) >= ymax )
1608 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1611 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1612 uint8_t *src, int i_src,
1614 int xmax, int ymax )
1616 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1617 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1618 uint8_t *next = &src[i_src];
1619 const int i_src2 = 2*i_src;
1624 uint8_t *rec = &dst[i_dst];
1626 /* We construct with intra method the missing field */
1627 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1629 /* Now we will try to find a match with ME with the other field */
1631 /* ME: A small/partial EPZS
1632 * We search only for small MV (with high motion intra will be perfect */
1633 if( xmax > 4 ) xmax = 4;
1634 if( ymax > 4 ) ymax = 4;
1636 /* Init with NULL Mv */
1638 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1640 /* Try predicted Mv */
1641 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1647 /* Search interger pel (small mv) */
1648 for( i_step = 0; i_step < 4; i_step++ )
1654 for( i = 0; i < 4; i++ )
1656 s = XDeint8x4TestInt( rec, i_src2,
1657 next, i_src2, mvx+dx[i], mvy+dy[i],
1677 if( mvs > 4 && mvs < 256 )
1680 /* XXX: for now only HPEL (too slow) */
1681 for( i_step = 0; i_step < 4; i_step++ )
1687 for( i = 0; i < 8; i++ )
1689 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1690 mvx+dx[i], mvy+dy[i],
1708 uint8_t buffer[8*4];
1709 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1710 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1712 //XDeint8x8Set( dst, i_dst, 0 );
1719 /* Kernel interpolation (1,-5,20,20,-5,1)
1720 * Lose a bit more details+add aliasing than edge interpol but avoid
1723 static inline uint8_t clip1( int a )
1732 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1733 uint8_t *src, int i_src )
1738 for( y = 0; y < 8; y += 2 )
1740 const int i_src2 = i_src*2;
1742 memcpy( dst, src, 8 );
1745 for( x = 0; x < 8; x++ )
1749 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1750 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1751 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1753 dst[x] = clip1( ( pix + 16 ) >> 5 );
1763 /* NxN arbitray size (and then only use pixel in the NxN block)
1765 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1766 int i_height, int i_width )
1773 /* Detect interlacing */
1774 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1777 for( y = 0; y < i_height - 2; y += 2 )
1779 const uint8_t *s = &src[y*i_src];
1780 for( x = 0; x < i_width; x++ )
1782 fr += ssd(s[ x] - s[1*i_src+x]);
1783 ff += ssd(s[ x] - s[2*i_src+x]);
1785 if( ff < fr && fr > i_width / 2 )
1789 return fc < 2 ? false : true;
1792 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1793 uint8_t *src, int i_src,
1794 int i_width, int i_height )
1799 for( y = 0; y < i_height; y += 2 )
1801 memcpy( dst, src, i_width );
1804 if( y < i_height - 2 )
1806 for( x = 0; x < i_width; x++ )
1807 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1811 /* Blend last line */
1812 for( x = 0; x < i_width; x++ )
1813 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1820 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1821 uint8_t *src, int i_src,
1822 int i_width, int i_height )
1827 for( y = 0; y < i_height; y += 2 )
1829 memcpy( dst, src, i_width );
1832 if( y < i_height - 2 )
1834 for( x = 0; x < i_width; x++ )
1835 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1839 /* Blend last line */
1840 for( x = 0; x < i_width; x++ )
1841 dst[x] = (src[x] + src[i_src+x]) >> 1;
1848 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1849 int i_width, int i_height )
1851 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1852 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1854 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1858 static inline int median( int a, int b, int c )
1860 int min = a, max =a;
1871 return a + b + c - min - max;
1877 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1878 uint8_t *src, int i_src,
1879 const int i_mbx, int i_modx )
1883 for( x = 0; x < i_mbx; x++ )
1886 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1888 if( x == 0 || x == i_mbx - 1 )
1889 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1891 XDeint8x8FieldC( dst, i_dst, src, i_src );
1895 XDeint8x8MergeC( dst, i_dst,
1896 &src[0*i_src], 2*i_src,
1897 &src[1*i_src], 2*i_src );
1905 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1907 #ifdef CAN_COMPILE_MMXEXT
1908 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1909 uint8_t *src, int i_src,
1910 const int i_mbx, int i_modx )
1914 /* Reset current line */
1915 for( x = 0; x < i_mbx; x++ )
1918 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1920 if( x == 0 || x == i_mbx - 1 )
1921 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1923 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1927 XDeint8x8MergeMMXEXT( dst, i_dst,
1928 &src[0*i_src], 2*i_src,
1929 &src[1*i_src], 2*i_src );
1937 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1941 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1945 /* Copy image and skip lines */
1946 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1948 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1949 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1951 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1952 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1954 const int i_dst = p_outpic->p[i_plane].i_pitch;
1955 const int i_src = p_pic->p[i_plane].i_pitch;
1959 for( y = 0; y < i_mby; y++ )
1961 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1962 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1964 #ifdef CAN_COMPILE_MMXEXT
1965 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1966 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1969 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1972 /* Last line (C only)*/
1975 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1976 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1978 for( x = 0; x < i_mbx; x++ )
1980 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1987 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1991 #ifdef CAN_COMPILE_MMXEXT
1992 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1997 /*****************************************************************************
1998 * SendEvents: forward mouse and keyboard events to the parent p_vout
1999 *****************************************************************************/
2000 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
2001 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
2003 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
2004 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
2005 vlc_value_t sentval = newval;
2007 if( !strcmp( psz_var, "mouse-y" ) )
2009 switch( p_vout->p_sys->i_mode )
2011 case DEINTERLACE_MEAN:
2012 case DEINTERLACE_DISCARD:
2018 var_Set( p_vout, psz_var, sentval );
2023 /*****************************************************************************
2024 * FilterCallback: called when changing the deinterlace method on the fly.
2025 *****************************************************************************/
2026 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
2027 vlc_value_t oldval, vlc_value_t newval,
2030 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2031 vout_thread_t * p_vout = (vout_thread_t *)p_this;
2032 int i_old_mode = p_vout->p_sys->i_mode;
2034 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
2036 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
2038 SetFilterMethod( p_vout, newval.psz_string );
2040 switch( p_vout->render.i_chroma )
2042 case VLC_FOURCC('I','4','2','2'):
2043 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2047 case VLC_FOURCC('I','4','2','0'):
2048 case VLC_FOURCC('I','Y','U','V'):
2049 case VLC_FOURCC('Y','V','1','2'):
2050 switch( p_vout->p_sys->i_mode )
2052 case DEINTERLACE_MEAN:
2053 case DEINTERLACE_DISCARD:
2054 if( ( i_old_mode == DEINTERLACE_MEAN )
2055 || ( i_old_mode == DEINTERLACE_DISCARD ) )
2057 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2062 case DEINTERLACE_BOB:
2063 case DEINTERLACE_BLEND:
2064 case DEINTERLACE_LINEAR:
2065 if( ( i_old_mode == DEINTERLACE_BOB )
2066 || ( i_old_mode == DEINTERLACE_BLEND )
2067 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2069 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2080 /* We need to kill the old vout */
2081 if( p_vout->p_sys->p_vout )
2083 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2084 vout_CloseAndRelease( p_vout->p_sys->p_vout );
2087 /* Try to open a new video output */
2088 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2090 if( p_vout->p_sys->p_vout == NULL )
2092 /* Everything failed */
2093 msg_Err( p_vout, "cannot open vout, aborting" );
2095 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2096 return VLC_EGENERIC;
2099 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2101 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2105 /*****************************************************************************
2106 * SendEventsToChild: forward events to the child/children vout
2107 *****************************************************************************/
2108 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2109 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2111 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2112 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2113 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2118 /*****************************************************************************
2119 * video filter2 functions
2120 *****************************************************************************/
2121 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2123 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2124 picture_t *p_pic_dst;
2126 /* Request output picture */
2127 p_pic_dst = filter_NewPicture( p_filter );
2128 if( p_pic_dst == NULL )
2131 switch( p_vout->p_sys->i_mode )
2133 case DEINTERLACE_DISCARD:
2135 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2137 msg_Err( p_vout, "discarding lines is not supported yet" );
2138 picture_Release( p_pic_dst );
2142 case DEINTERLACE_BOB:
2144 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2145 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2149 case DEINTERLACE_LINEAR:
2151 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2152 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2154 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2155 picture_Release( p_pic_dst );
2159 case DEINTERLACE_MEAN:
2160 RenderMean( p_vout, p_pic_dst, p_pic );
2163 case DEINTERLACE_BLEND:
2164 RenderBlend( p_vout, p_pic_dst, p_pic );
2168 RenderX( p_pic_dst, p_pic );
2172 picture_CopyProperties( p_pic_dst, p_pic );
2173 p_pic_dst->b_progressive = true;
2175 picture_Release( p_pic );
2179 /*****************************************************************************
2181 *****************************************************************************/
2182 static int OpenFilter( vlc_object_t *p_this )
2184 filter_t *p_filter = (filter_t*)p_this;
2185 vout_thread_t *p_vout;
2188 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2189 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2190 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2191 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2193 return VLC_EGENERIC;
2196 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2198 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2199 vlc_object_attach( p_vout, p_filter );
2200 p_filter->p_sys = (filter_sys_t *)p_vout;
2201 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2203 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2205 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2206 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2207 var_Set( p_filter, "deinterlace-mode", val );
2208 free( val.psz_string );
2210 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2212 vlc_object_detach( p_vout );
2213 vlc_object_release( p_vout );
2214 return VLC_EGENERIC;
2217 p_filter->pf_video_filter = Deinterlace;
2219 msg_Dbg( p_filter, "deinterlacing" );
2224 /*****************************************************************************
2225 * CloseFilter: clean up the filter
2226 *****************************************************************************/
2227 static void CloseFilter( vlc_object_t *p_this )
2229 filter_t *p_filter = (filter_t*)p_this;
2230 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2232 Destroy( VLC_OBJECT(p_vout) );
2233 vlc_object_detach( p_vout );
2234 vlc_object_release( p_vout );