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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
34 #include "vlc_filter.h"
40 #ifdef CAN_COMPILE_MMXEXT
44 #include "filter_common.h"
46 #define DEINTERLACE_DISCARD 1
47 #define DEINTERLACE_MEAN 2
48 #define DEINTERLACE_BLEND 3
49 #define DEINTERLACE_BOB 4
50 #define DEINTERLACE_LINEAR 5
51 #define DEINTERLACE_X 6
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Create ( vlc_object_t * );
57 static void Destroy ( vlc_object_t * );
59 static int Init ( vout_thread_t * );
60 static void End ( vout_thread_t * );
61 static void Render ( vout_thread_t *, picture_t * );
63 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
64 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
65 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
66 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
67 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
68 static void RenderX ( vout_thread_t *, picture_t *, picture_t * );
70 static void MergeGeneric ( void *, const void *, const void *, size_t );
71 #if defined(CAN_COMPILE_C_ALTIVEC)
72 static void MergeAltivec ( void *, const void *, const void *, size_t );
74 #if defined(CAN_COMPILE_MMXEXT)
75 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
77 #if defined(CAN_COMPILE_3DNOW)
78 static void Merge3DNow ( void *, const void *, const void *, size_t );
80 #if defined(CAN_COMPILE_SSE)
81 static void MergeSSE2 ( void *, const void *, const void *, size_t );
83 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
84 static void EndMMX ( void );
86 #if defined(CAN_COMPILE_3DNOW)
87 static void End3DNow ( void );
90 static int SendEvents ( vlc_object_t *, char const *,
91 vlc_value_t, vlc_value_t, void * );
93 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
94 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
96 static int OpenFilter( vlc_object_t *p_this );
97 static void CloseFilter( vlc_object_t *p_this );
99 /*****************************************************************************
100 * Callback prototypes
101 *****************************************************************************/
102 static int FilterCallback ( vlc_object_t *, char const *,
103 vlc_value_t, vlc_value_t, void * );
105 /*****************************************************************************
107 *****************************************************************************/
108 #define MODE_TEXT N_("Deinterlace mode")
109 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
111 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
112 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
114 #define FILTER_CFG_PREFIX "sout-deinterlace-"
116 static const char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", "x" };
117 static const char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
118 N_("Bob"), N_("Linear"), "X" };
121 set_description( _("Deinterlacing video filter") );
122 set_shortname( _("Deinterlace" ));
123 set_capability( "video filter", 0 );
124 set_category( CAT_VIDEO );
125 set_subcategory( SUBCAT_VIDEO_VFILTER );
127 set_section( N_("Display"),NULL);
128 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
129 MODE_LONGTEXT, VLC_FALSE );
130 change_string_list( mode_list, mode_list_text, 0 );
132 add_shortcut( "deinterlace" );
133 set_callbacks( Create, Destroy );
136 set_capability( "video filter2", 0 );
137 set_section( N_("Streaming"),NULL);
138 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
139 SOUT_MODE_LONGTEXT, VLC_FALSE );
140 change_string_list( mode_list, mode_list_text, 0 );
141 set_callbacks( OpenFilter, CloseFilter );
144 static const char *ppsz_filter_options[] = {
148 /*****************************************************************************
149 * vout_sys_t: Deinterlace video output method descriptor
150 *****************************************************************************
151 * This structure is part of the video output thread descriptor.
152 * It describes the Deinterlace specific properties of an output thread.
153 *****************************************************************************/
156 int i_mode; /* Deinterlace mode */
157 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
162 vout_thread_t *p_vout;
164 vlc_mutex_t filter_lock;
166 void (*pf_merge) ( void *, const void *, const void *, size_t );
167 void (*pf_end_merge) ( void );
170 /*****************************************************************************
171 * Control: control facility for the vout (forwards to child vout)
172 *****************************************************************************/
173 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
175 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
178 /*****************************************************************************
179 * Create: allocates Deinterlace video thread output method
180 *****************************************************************************
181 * This function allocates and initializes a Deinterlace vout method.
182 *****************************************************************************/
183 static int Create( vlc_object_t *p_this )
185 vout_thread_t *p_vout = (vout_thread_t *)p_this;
188 /* Allocate structure */
189 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
190 if( p_vout->p_sys == NULL )
192 msg_Err( p_vout, "out of memory" );
196 p_vout->pf_init = Init;
197 p_vout->pf_end = End;
198 p_vout->pf_manage = NULL;
199 p_vout->pf_render = Render;
200 p_vout->pf_display = NULL;
201 p_vout->pf_control = Control;
203 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
204 p_vout->p_sys->b_double_rate = VLC_FALSE;
205 p_vout->p_sys->last_date = 0;
206 p_vout->p_sys->p_vout = 0;
207 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
209 #if defined(CAN_COMPILE_C_ALTIVEC)
210 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
212 p_vout->p_sys->pf_merge = MergeAltivec;
213 p_vout->p_sys->pf_end_merge = NULL;
217 #if defined(CAN_COMPILE_SSE)
218 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
220 p_vout->p_sys->pf_merge = MergeSSE2;
221 p_vout->p_sys->pf_end_merge = EndMMX;
225 #if defined(CAN_COMPILE_MMXEXT)
226 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
228 p_vout->p_sys->pf_merge = MergeMMXEXT;
229 p_vout->p_sys->pf_end_merge = EndMMX;
233 #if defined(CAN_COMPILE_3DNOW)
234 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
236 p_vout->p_sys->pf_merge = Merge3DNow;
237 p_vout->p_sys->pf_end_merge = End3DNow;
242 p_vout->p_sys->pf_merge = MergeGeneric;
243 p_vout->p_sys->pf_end_merge = NULL;
246 /* Look what method was requested */
247 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
248 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
250 if( val.psz_string == NULL )
252 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
253 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
255 val.psz_string = strdup( "discard" );
258 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
260 SetFilterMethod( p_vout, val.psz_string );
262 free( val.psz_string );
267 /*****************************************************************************
268 * SetFilterMethod: setup the deinterlace method to use.
269 *****************************************************************************/
270 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
272 if( !strcmp( psz_method, "discard" ) )
274 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
275 p_vout->p_sys->b_double_rate = VLC_FALSE;
277 else if( !strcmp( psz_method, "mean" ) )
279 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
280 p_vout->p_sys->b_double_rate = VLC_FALSE;
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 = VLC_FALSE;
289 else if( !strcmp( psz_method, "bob" )
290 || !strcmp( psz_method, "progressive-scan" ) )
292 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
293 p_vout->p_sys->b_double_rate = VLC_TRUE;
295 else if( !strcmp( psz_method, "linear" ) )
297 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
298 p_vout->p_sys->b_double_rate = VLC_TRUE;
300 else if( !strcmp( psz_method, "x" ) )
302 p_vout->p_sys->i_mode = DEINTERLACE_X;
303 p_vout->p_sys->b_double_rate = VLC_FALSE;
307 msg_Err( p_vout, "no valid deinterlace mode provided, "
308 "using \"discard\"" );
311 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
314 /*****************************************************************************
315 * Init: initialize Deinterlace video thread output method
316 *****************************************************************************/
317 static int Init( vout_thread_t *p_vout )
322 I_OUTPUTPICTURES = 0;
324 /* Initialize the output structure, full of directbuffers since we want
325 * the decoder to output directly to our structures. */
326 switch( p_vout->render.i_chroma )
328 case VLC_FOURCC('I','4','2','0'):
329 case VLC_FOURCC('I','Y','U','V'):
330 case VLC_FOURCC('Y','V','1','2'):
331 case VLC_FOURCC('I','4','2','2'):
332 p_vout->output.i_chroma = p_vout->render.i_chroma;
333 p_vout->output.i_width = p_vout->render.i_width;
334 p_vout->output.i_height = p_vout->render.i_height;
335 p_vout->output.i_aspect = p_vout->render.i_aspect;
336 p_vout->fmt_out = p_vout->fmt_in;
340 return VLC_EGENERIC; /* unknown chroma */
344 /* Try to open the real video output */
345 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
347 if( p_vout->p_sys->p_vout == NULL )
349 /* Everything failed */
350 msg_Err( p_vout, "cannot open vout, aborting" );
355 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
357 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
359 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
361 ADD_PARENT_CALLBACKS( SendEventsToChild );
366 /*****************************************************************************
367 * SpawnRealVout: spawn the real video output.
368 *****************************************************************************/
369 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
371 vout_thread_t *p_real_vout = NULL;
373 memset( &fmt, 0, sizeof( video_format_t ) );
375 msg_Dbg( p_vout, "spawning the real video output" );
377 fmt = p_vout->fmt_out;
379 switch( p_vout->render.i_chroma )
381 case VLC_FOURCC('I','4','2','0'):
382 case VLC_FOURCC('I','Y','U','V'):
383 case VLC_FOURCC('Y','V','1','2'):
384 switch( p_vout->p_sys->i_mode )
386 case DEINTERLACE_MEAN:
387 case DEINTERLACE_DISCARD:
388 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
390 p_real_vout = vout_Create( p_vout, &fmt );
393 case DEINTERLACE_BOB:
394 case DEINTERLACE_BLEND:
395 case DEINTERLACE_LINEAR:
397 p_real_vout = vout_Create( p_vout, &fmt );
402 case VLC_FOURCC('I','4','2','2'):
403 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
404 p_real_vout = vout_Create( p_vout, &fmt );
414 /*****************************************************************************
415 * End: terminate Deinterlace video thread output method
416 *****************************************************************************/
417 static void End( vout_thread_t *p_vout )
421 /* Free the fake output buffers we allocated */
422 for( i_index = I_OUTPUTPICTURES ; i_index ; )
425 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
428 if( p_vout->p_sys->p_vout )
430 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
431 vlc_object_detach( p_vout->p_sys->p_vout );
432 vout_Destroy( p_vout->p_sys->p_vout );
435 DEL_PARENT_CALLBACKS( SendEventsToChild );
438 /*****************************************************************************
439 * Destroy: destroy Deinterlace video thread output method
440 *****************************************************************************
441 * Terminate an output method created by DeinterlaceCreateOutputMethod
442 *****************************************************************************/
443 static void Destroy( vlc_object_t *p_this )
445 vout_thread_t *p_vout = (vout_thread_t *)p_this;
446 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
447 free( p_vout->p_sys );
450 /*****************************************************************************
451 * Render: displays previously rendered output
452 *****************************************************************************
453 * This function send the currently rendered image to Deinterlace image,
454 * waits until it is displayed and switch the two rendering buffers, preparing
456 *****************************************************************************/
457 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
459 vout_sys_t *p_sys = p_vout->p_sys;
460 picture_t *pp_outpic[2];
462 p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
463 p_vout->fmt_in.i_x_offset;
464 p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
465 p_vout->fmt_in.i_y_offset;
466 p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
467 p_vout->fmt_in.i_visible_width;
468 p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
469 p_vout->fmt_in.i_visible_height;
470 if( p_vout->p_sys->i_mode == DEINTERLACE_MEAN ||
471 p_vout->p_sys->i_mode == DEINTERLACE_DISCARD )
473 p_vout->fmt_out.i_y_offset /= 2; p_sys->p_vout->fmt_in.i_y_offset /= 2;
474 p_vout->fmt_out.i_visible_height /= 2;
475 p_sys->p_vout->fmt_in.i_visible_height /= 2;
478 pp_outpic[0] = pp_outpic[1] = NULL;
480 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
482 /* Get a new picture */
483 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
487 if( p_vout->b_die || p_vout->b_error )
489 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
492 msleep( VOUT_OUTMEM_SLEEP );
495 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
497 /* If we are using double rate, get an additional new picture */
498 if( p_vout->p_sys->b_double_rate )
500 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
504 if( p_vout->b_die || p_vout->b_error )
506 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
507 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
510 msleep( VOUT_OUTMEM_SLEEP );
513 /* 20ms is a bit arbitrary, but it's only for the first image we get */
514 if( !p_vout->p_sys->last_date )
516 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
517 p_pic->date + 20000 );
521 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
522 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
524 p_vout->p_sys->last_date = p_pic->date;
527 switch( p_vout->p_sys->i_mode )
529 case DEINTERLACE_DISCARD:
530 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
531 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
534 case DEINTERLACE_BOB:
535 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
536 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
537 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
538 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
541 case DEINTERLACE_LINEAR:
542 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
543 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
544 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
545 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
548 case DEINTERLACE_MEAN:
549 RenderMean( p_vout, pp_outpic[0], p_pic );
550 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
553 case DEINTERLACE_BLEND:
554 RenderBlend( p_vout, pp_outpic[0], p_pic );
555 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
559 RenderX( p_vout, pp_outpic[0], p_pic );
560 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
563 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
566 /*****************************************************************************
567 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
568 *****************************************************************************/
569 static void RenderDiscard( vout_thread_t *p_vout,
570 picture_t *p_outpic, picture_t *p_pic, int i_field )
574 /* Copy image and skip lines */
575 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
577 uint8_t *p_in, *p_out_end, *p_out;
580 p_in = p_pic->p[i_plane].p_pixels
581 + i_field * p_pic->p[i_plane].i_pitch;
583 p_out = p_outpic->p[i_plane].p_pixels;
584 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
585 * p_outpic->p[i_plane].i_visible_lines;
587 switch( p_vout->render.i_chroma )
589 case VLC_FOURCC('I','4','2','0'):
590 case VLC_FOURCC('I','Y','U','V'):
591 case VLC_FOURCC('Y','V','1','2'):
593 for( ; p_out < p_out_end ; )
595 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
596 p_pic->p[i_plane].i_pitch );
598 p_out += p_outpic->p[i_plane].i_pitch;
599 p_in += 2 * p_pic->p[i_plane].i_pitch;
603 case VLC_FOURCC('I','4','2','2'):
605 i_increment = 2 * p_pic->p[i_plane].i_pitch;
607 if( i_plane == Y_PLANE )
609 for( ; p_out < p_out_end ; )
611 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
612 p_pic->p[i_plane].i_pitch );
613 p_out += p_outpic->p[i_plane].i_pitch;
614 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
615 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 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
625 p_pic->p[i_plane].i_pitch );
626 p_out += p_outpic->p[i_plane].i_pitch;
638 /*****************************************************************************
639 * RenderBob: renders a BOB picture - simple copy
640 *****************************************************************************/
641 static void RenderBob( vout_thread_t *p_vout,
642 picture_t *p_outpic, picture_t *p_pic, int i_field )
646 /* Copy image and skip lines */
647 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
649 uint8_t *p_in, *p_out_end, *p_out;
651 p_in = p_pic->p[i_plane].p_pixels;
652 p_out = p_outpic->p[i_plane].p_pixels;
653 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
654 * p_outpic->p[i_plane].i_visible_lines;
656 switch( p_vout->render.i_chroma )
658 case VLC_FOURCC('I','4','2','0'):
659 case VLC_FOURCC('I','Y','U','V'):
660 case VLC_FOURCC('Y','V','1','2'):
661 /* For BOTTOM field we need to add the first line */
664 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
665 p_pic->p[i_plane].i_pitch );
666 p_in += p_pic->p[i_plane].i_pitch;
667 p_out += p_outpic->p[i_plane].i_pitch;
670 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
672 for( ; p_out < p_out_end ; )
674 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
675 p_pic->p[i_plane].i_pitch );
677 p_out += p_outpic->p[i_plane].i_pitch;
679 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
680 p_pic->p[i_plane].i_pitch );
682 p_in += 2 * p_pic->p[i_plane].i_pitch;
683 p_out += p_outpic->p[i_plane].i_pitch;
686 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
687 p_pic->p[i_plane].i_pitch );
689 /* For TOP field we need to add the last line */
692 p_in += p_pic->p[i_plane].i_pitch;
693 p_out += p_outpic->p[i_plane].i_pitch;
694 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
695 p_pic->p[i_plane].i_pitch );
699 case VLC_FOURCC('I','4','2','2'):
700 /* For BOTTOM field we need to add the first line */
703 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
704 p_pic->p[i_plane].i_pitch );
705 p_in += p_pic->p[i_plane].i_pitch;
706 p_out += p_outpic->p[i_plane].i_pitch;
709 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
711 if( i_plane == Y_PLANE )
713 for( ; p_out < p_out_end ; )
715 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
716 p_pic->p[i_plane].i_pitch );
718 p_out += p_outpic->p[i_plane].i_pitch;
720 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
721 p_pic->p[i_plane].i_pitch );
723 p_in += 2 * p_pic->p[i_plane].i_pitch;
724 p_out += p_outpic->p[i_plane].i_pitch;
729 for( ; p_out < p_out_end ; )
731 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
732 p_pic->p[i_plane].i_pitch );
734 p_out += p_outpic->p[i_plane].i_pitch;
735 p_in += 2 * p_pic->p[i_plane].i_pitch;
739 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
740 p_pic->p[i_plane].i_pitch );
742 /* For TOP field we need to add the last line */
745 p_in += p_pic->p[i_plane].i_pitch;
746 p_out += p_outpic->p[i_plane].i_pitch;
747 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
748 p_pic->p[i_plane].i_pitch );
755 #define Merge p_vout->p_sys->pf_merge
756 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
758 /*****************************************************************************
759 * RenderLinear: BOB with linear interpolation
760 *****************************************************************************/
761 static void RenderLinear( vout_thread_t *p_vout,
762 picture_t *p_outpic, picture_t *p_pic, int i_field )
766 /* Copy image and skip lines */
767 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
769 uint8_t *p_in, *p_out_end, *p_out;
771 p_in = p_pic->p[i_plane].p_pixels;
772 p_out = p_outpic->p[i_plane].p_pixels;
773 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
774 * p_outpic->p[i_plane].i_visible_lines;
776 /* For BOTTOM field we need to add the first line */
779 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
780 p_pic->p[i_plane].i_pitch );
781 p_in += p_pic->p[i_plane].i_pitch;
782 p_out += p_outpic->p[i_plane].i_pitch;
785 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
787 for( ; p_out < p_out_end ; )
789 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
790 p_pic->p[i_plane].i_pitch );
792 p_out += p_outpic->p[i_plane].i_pitch;
794 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
795 p_pic->p[i_plane].i_pitch );
797 p_in += 2 * p_pic->p[i_plane].i_pitch;
798 p_out += p_outpic->p[i_plane].i_pitch;
801 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
802 p_pic->p[i_plane].i_pitch );
804 /* For TOP field we need to add the last line */
807 p_in += p_pic->p[i_plane].i_pitch;
808 p_out += p_outpic->p[i_plane].i_pitch;
809 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
810 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 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
868 p_pic->p[i_plane].i_pitch );
869 p_out += p_outpic->p[i_plane].i_pitch;
871 /* Remaining lines: mean value */
872 for( ; p_out < p_out_end ; )
874 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
875 p_pic->p[i_plane].i_pitch );
877 p_out += p_outpic->p[i_plane].i_pitch;
878 p_in += p_pic->p[i_plane].i_pitch;
882 case VLC_FOURCC('I','4','2','2'):
883 /* First line: simple copy */
884 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
885 p_pic->p[i_plane].i_pitch );
886 p_out += p_outpic->p[i_plane].i_pitch;
888 /* Remaining lines: mean value */
889 if( i_plane == Y_PLANE )
891 for( ; p_out < p_out_end ; )
893 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
894 p_pic->p[i_plane].i_pitch );
896 p_out += p_outpic->p[i_plane].i_pitch;
897 p_in += p_pic->p[i_plane].i_pitch;
903 for( ; p_out < p_out_end ; )
905 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
906 p_pic->p[i_plane].i_pitch );
908 p_out += p_outpic->p[i_plane].i_pitch;
909 p_in += 2*p_pic->p[i_plane].i_pitch;
920 static void MergeGeneric( void *_p_dest, const void *_p_s1,
921 const void *_p_s2, size_t i_bytes )
923 uint8_t* p_dest = (uint8_t*)_p_dest;
924 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
925 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
926 uint8_t* p_end = p_dest + i_bytes - 8;
928 while( p_dest < p_end )
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;
936 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
937 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
942 while( p_dest < p_end )
944 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
948 #if defined(CAN_COMPILE_MMXEXT)
949 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
952 uint8_t* p_dest = (uint8_t*)_p_dest;
953 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
954 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
955 uint8_t* p_end = p_dest + i_bytes - 8;
956 while( p_dest < p_end )
958 __asm__ __volatile__( "movq %2,%%mm1;"
960 "movq %%mm1, %0" :"=m" (*p_dest):
970 while( p_dest < p_end )
972 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
977 #if defined(CAN_COMPILE_3DNOW)
978 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
981 uint8_t* p_dest = (uint8_t*)_p_dest;
982 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
983 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
984 uint8_t* p_end = p_dest + i_bytes - 8;
985 while( p_dest < p_end )
987 __asm__ __volatile__( "movq %2,%%mm1;"
989 "movq %%mm1, %0" :"=m" (*p_dest):
999 while( p_dest < p_end )
1001 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1006 #if defined(CAN_COMPILE_SSE)
1007 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1010 uint8_t* p_dest = (uint8_t*)_p_dest;
1011 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1012 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1014 while( (ptrdiff_t)p_s1 % 16 )
1016 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1018 p_end = p_dest + i_bytes - 16;
1019 while( p_dest < p_end )
1021 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1023 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1033 while( p_dest < p_end )
1035 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1040 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1041 static void EndMMX( void )
1043 __asm__ __volatile__( "emms" :: );
1047 #if defined(CAN_COMPILE_3DNOW)
1048 static void End3DNow( void )
1050 __asm__ __volatile__( "femms" :: );
1054 #ifdef CAN_COMPILE_C_ALTIVEC
1055 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1056 const void *_p_s2, size_t i_bytes )
1058 uint8_t *p_dest = (uint8_t *)_p_dest;
1059 uint8_t *p_s1 = (uint8_t *)_p_s1;
1060 uint8_t *p_s2 = (uint8_t *)_p_s2;
1061 uint8_t *p_end = p_dest + i_bytes - 15;
1063 /* Use C until the first 16-bytes aligned destination pixel */
1064 while( (int)p_dest & 0xF )
1066 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1069 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1071 /* Unaligned source */
1072 vector unsigned char s1v, s2v, destv;
1073 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1074 vector unsigned char perm1v, perm2v;
1076 perm1v = vec_lvsl( 0, p_s1 );
1077 perm2v = vec_lvsl( 0, p_s2 );
1078 s1oldv = vec_ld( 0, p_s1 );
1079 s2oldv = vec_ld( 0, p_s2 );
1081 while( p_dest < p_end )
1083 s1newv = vec_ld( 16, p_s1 );
1084 s2newv = vec_ld( 16, p_s2 );
1085 s1v = vec_perm( s1oldv, s1newv, perm1v );
1086 s2v = vec_perm( s2oldv, s2newv, perm2v );
1089 destv = vec_avg( s1v, s2v );
1090 vec_st( destv, 0, p_dest );
1099 /* Aligned source */
1100 vector unsigned char s1v, s2v, destv;
1102 while( p_dest < p_end )
1104 s1v = vec_ld( 0, p_s1 );
1105 s2v = vec_ld( 0, p_s2 );
1106 destv = vec_avg( s1v, s2v );
1107 vec_st( destv, 0, p_dest );
1117 while( p_dest < p_end )
1119 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1124 /*****************************************************************************
1125 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1126 * and apply a process to recreate the bottom field :
1127 * If a 8x8 block is classified as :
1128 * - progressive: it applies a small blend (1,6,1)
1130 * * in the MMX version: we do a ME between the 2 fields, if there is a
1131 * good match we use MC to recreate the bottom field (with a small
1133 * * otherwise: it recreates the bottom field by an edge oriented
1135 *****************************************************************************/
1137 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1138 * XXX: It need to access to 8x10
1139 * We use more than 8 lines to help with scrolling (text)
1140 * (and because XDeint8x8Frame use line 9)
1141 * XXX: smooth/uniform area with noise detection doesn't works well
1142 * but it's not really a problem because they don't have much details anyway
1144 static inline int ssd( int a ) { return a*a; }
1145 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1151 /* Detect interlacing */
1153 for( y = 0; y < 7; y += 2 )
1156 for( x = 0; x < 8; x++ )
1158 fr += ssd(src[ x] - src[1*i_src+x]) +
1159 ssd(src[i_src+x] - src[2*i_src+x]);
1160 ff += ssd(src[ x] - src[2*i_src+x]) +
1161 ssd(src[i_src+x] - src[3*i_src+x]);
1163 if( ff < 6*fr/8 && fr > 32 )
1169 return fc < 1 ? VLC_FALSE : VLC_TRUE;
1171 #ifdef CAN_COMPILE_MMXEXT
1172 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1179 /* Detect interlacing */
1181 pxor_r2r( mm7, mm7 );
1182 for( y = 0; y < 9; y += 2 )
1185 pxor_r2r( mm5, mm5 );
1186 pxor_r2r( mm6, mm6 );
1187 for( x = 0; x < 8; x+=4 )
1189 movd_m2r( src[ x], mm0 );
1190 movd_m2r( src[1*i_src+x], mm1 );
1191 movd_m2r( src[2*i_src+x], mm2 );
1192 movd_m2r( src[3*i_src+x], mm3 );
1194 punpcklbw_r2r( mm7, mm0 );
1195 punpcklbw_r2r( mm7, mm1 );
1196 punpcklbw_r2r( mm7, mm2 );
1197 punpcklbw_r2r( mm7, mm3 );
1199 movq_r2r( mm0, mm4 );
1201 psubw_r2r( mm1, mm0 );
1202 psubw_r2r( mm2, mm4 );
1204 psubw_r2r( mm1, mm2 );
1205 psubw_r2r( mm1, mm3 );
1207 pmaddwd_r2r( mm0, mm0 );
1208 pmaddwd_r2r( mm4, mm4 );
1209 pmaddwd_r2r( mm2, mm2 );
1210 pmaddwd_r2r( mm3, mm3 );
1211 paddd_r2r( mm0, mm2 );
1212 paddd_r2r( mm4, mm3 );
1213 paddd_r2r( mm2, mm5 );
1214 paddd_r2r( mm3, mm6 );
1217 movq_r2r( mm5, mm0 );
1218 psrlq_i2r( 32, mm0 );
1219 paddd_r2r( mm0, mm5 );
1220 movd_r2m( mm5, fr );
1222 movq_r2r( mm6, mm0 );
1223 psrlq_i2r( 32, mm0 );
1224 paddd_r2r( mm0, mm6 );
1225 movd_r2m( mm6, ff );
1227 if( ff < 6*fr/8 && fr > 32 )
1236 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1237 * This won't destroy details, and help if there is a bit of interlacing.
1238 * (It helps with paning to avoid flickers)
1242 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1243 uint8_t *src, int i_src )
1248 for( y = 0; y < 8; y += 2 )
1250 memcpy( dst, src, 8 );
1253 for( x = 0; x < 8; x++ )
1254 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1260 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1261 uint8_t *src1, int i_src1,
1262 uint8_t *src2, int i_src2 )
1267 for( y = 0; y < 8; y += 2 )
1269 memcpy( dst, src1, 8 );
1272 for( x = 0; x < 8; x++ )
1273 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1281 #ifdef CAN_COMPILE_MMXEXT
1282 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1283 uint8_t *src1, int i_src1,
1284 uint8_t *src2, int i_src2 )
1286 static const uint64_t m_4 = I64C(0x0004000400040004);
1290 pxor_r2r( mm7, mm7 );
1291 for( y = 0; y < 8; y += 2 )
1293 for( x = 0; x < 8; x +=4 )
1295 movd_m2r( src1[x], mm0 );
1296 movd_r2m( mm0, dst[x] );
1298 movd_m2r( src2[x], mm1 );
1299 movd_m2r( src1[i_src1+x], mm2 );
1301 punpcklbw_r2r( mm7, mm0 );
1302 punpcklbw_r2r( mm7, mm1 );
1303 punpcklbw_r2r( mm7, mm2 );
1304 paddw_r2r( mm1, mm1 );
1305 movq_r2r( mm1, mm3 );
1306 paddw_r2r( mm3, mm3 );
1307 paddw_r2r( mm2, mm0 );
1308 paddw_r2r( mm3, mm1 );
1309 paddw_m2r( m_4, mm1 );
1310 paddw_r2r( mm1, mm0 );
1311 psraw_i2r( 3, mm0 );
1312 packuswb_r2r( mm7, mm0 );
1313 movd_r2m( mm0, dst[i_dst+x] );
1324 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1327 for( y = 0; y < 8; y++ )
1328 memset( &dst[y*i_dst], v, 8 );
1331 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1334 * TODO: a better one for the inner part.
1336 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1337 uint8_t *src, int i_src )
1342 for( y = 0; y < 8; y += 2 )
1344 memcpy( dst, src, 8 );
1347 for( x = 0; x < 8; x++ )
1348 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1353 #ifdef CAN_COMPILE_MMXEXT
1354 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1355 uint8_t *src, int i_src )
1360 for( y = 0; y < 8; y += 2 )
1362 movq_m2r( src[0], mm0 );
1363 movq_r2m( mm0, dst[0] );
1366 movq_m2r( src[2*i_src], mm1 );
1367 pavgb_r2r( mm1, mm0 );
1369 movq_r2m( mm0, dst[0] );
1377 /* XDeint8x8Field: Edge oriented interpolation
1378 * (Need -4 and +5 pixels H, +1 line)
1380 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1381 uint8_t *src, int i_src )
1386 for( y = 0; y < 8; y += 2 )
1388 memcpy( dst, src, 8 );
1391 for( x = 0; x < 8; x++ )
1393 uint8_t *src2 = &src[2*i_src];
1394 /* I use 8 pixels just to match the MMX version, but it's overkill
1395 * 5 would be enough (less isn't good) */
1396 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1397 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1398 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1399 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1401 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1402 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1403 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1404 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1406 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1407 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1408 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1409 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1411 if( c0 < c1 && c1 <= c2 )
1412 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1413 else if( c2 < c1 && c1 <= c0 )
1414 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1416 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1423 #ifdef CAN_COMPILE_MMXEXT
1424 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1425 uint8_t *src, int i_src )
1430 for( y = 0; y < 8; y += 2 )
1432 memcpy( dst, src, 8 );
1435 for( x = 0; x < 8; x++ )
1437 uint8_t *src2 = &src[2*i_src];
1440 movq_m2r( src[x-2], mm0 );
1441 movq_m2r( src[x-3], mm1 );
1442 movq_m2r( src[x-4], mm2 );
1444 psadbw_m2r( src2[x-4], mm0 );
1445 psadbw_m2r( src2[x-3], mm1 );
1446 psadbw_m2r( src2[x-2], mm2 );
1448 movd_r2m( mm0, c2 );
1449 movd_r2m( mm1, c1 );
1450 movd_r2m( mm2, c0 );
1452 if( c0 < c1 && c1 <= c2 )
1453 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1454 else if( c2 < c1 && c1 <= c0 )
1455 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1457 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1467 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1468 uint8_t *pix2, int i_pix2 )
1473 for( y = 0; y < 8; y++ )
1474 for( x = 0; x < 8; x++ )
1475 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1479 #ifdef CAN_COMPILE_MMXEXT
1480 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1481 uint8_t *pix2, int i_pix2 )
1486 pxor_r2r( mm7, mm7 );
1487 pxor_r2r( mm6, mm6 );
1489 for( y = 0; y < 8; y++ )
1491 movq_m2r( pix1[0], mm0 );
1492 movq_m2r( pix2[0], mm1 );
1494 movq_r2r( mm0, mm2 );
1495 movq_r2r( mm1, mm3 );
1497 punpcklbw_r2r( mm7, mm0 );
1498 punpckhbw_r2r( mm7, mm2 );
1499 punpcklbw_r2r( mm7, mm1 );
1500 punpckhbw_r2r( mm7, mm3 );
1502 psubw_r2r( mm1, mm0 );
1503 psubw_r2r( mm3, mm2 );
1505 pmaddwd_r2r( mm0, mm0 );
1506 pmaddwd_r2r( mm2, mm2 );
1508 paddd_r2r( mm2, mm0 );
1509 paddd_r2r( mm0, mm6 );
1515 movq_r2r( mm6, mm7 );
1516 psrlq_i2r( 32, mm7 );
1517 paddd_r2r( mm6, mm7 );
1526 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1527 #ifdef CAN_COMPILE_MMXEXT
1530 * TODO: mmx version (easier in sse2)
1532 static inline void XDeintMC( uint8_t *dst, int i_dst,
1533 uint8_t *src, int i_src,
1535 int i_width, int i_height )
1537 const int d4x = mvx&0x03;
1538 const int d4y = mvy&0x03;
1540 const int cA = (4-d4x)*(4-d4y);
1541 const int cB = d4x *(4-d4y);
1542 const int cC = (4-d4x)*d4y;
1543 const int cD = d4x *d4y;
1549 src += (mvy >> 2) * i_src + (mvx >> 2);
1552 for( y = 0; y < i_height; y++ )
1554 for( x = 0; x < i_width; x++ )
1556 dst[x] = ( cA*src[x] + cB*src[x+1] +
1557 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1565 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1566 uint8_t *pix2, int i_pix2 )
1570 movq_m2r( pix1[0*i_pix1], mm0 );
1571 movq_m2r( pix1[1*i_pix1], mm1 );
1573 psadbw_m2r( pix2[0*i_pix2], mm0 );
1574 psadbw_m2r( pix2[1*i_pix2], mm1 );
1576 movq_m2r( pix1[2*i_pix1], mm2 );
1577 movq_m2r( pix1[3*i_pix1], mm3 );
1578 psadbw_m2r( pix2[2*i_pix2], mm2 );
1579 psadbw_m2r( pix2[3*i_pix2], mm3 );
1581 paddd_r2r( mm1, mm0 );
1582 paddd_r2r( mm3, mm2 );
1583 paddd_r2r( mm2, mm0 );
1589 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1590 uint8_t *ref, int i_stride,
1592 int xmax, int ymax )
1594 uint8_t buffer[8*4];
1596 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1599 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1600 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1602 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1603 uint8_t *ref, int i_stride,
1605 int xmax, int ymax )
1607 if( abs(mx) >= xmax || abs(my) >= ymax )
1610 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1613 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1614 uint8_t *src, int i_src,
1616 int xmax, int ymax )
1618 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1619 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1620 uint8_t *next = &src[i_src];
1621 const int i_src2 = 2*i_src;
1626 uint8_t *rec = &dst[i_dst];
1628 /* We construct with intra method the missing field */
1629 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1631 /* Now we will try to find a match with ME with the other field */
1633 /* ME: A small/partial EPZS
1634 * We search only for small MV (with high motion intra will be perfect */
1635 if( xmax > 4 ) xmax = 4;
1636 if( ymax > 4 ) ymax = 4;
1638 /* Init with NULL Mv */
1640 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1642 /* Try predicted Mv */
1643 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1649 /* Search interger pel (small mv) */
1650 for( i_step = 0; i_step < 4; i_step++ )
1656 for( i = 0; i < 4; i++ )
1658 s = XDeint8x4TestInt( rec, i_src2,
1659 next, i_src2, mvx+dx[i], mvy+dy[i],
1679 if( mvs > 4 && mvs < 256 )
1682 /* XXX: for now only HPEL (too slow) */
1683 for( i_step = 0; i_step < 4; i_step++ )
1689 for( i = 0; i < 8; i++ )
1691 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1692 mvx+dx[i], mvy+dy[i],
1710 uint8_t buffer[8*4];
1711 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1712 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1714 //XDeint8x8Set( dst, i_dst, 0 );
1721 /* Kernel interpolation (1,-5,20,20,-5,1)
1722 * Lose a bit more details+add aliasing than edge interpol but avoid
1725 static inline uint8_t clip1( int a )
1734 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1735 uint8_t *src, int i_src )
1740 for( y = 0; y < 8; y += 2 )
1742 const int i_src2 = i_src*2;
1744 memcpy( dst, src, 8 );
1747 for( x = 0; x < 8; x++ )
1751 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1752 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1753 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1755 dst[x] = clip1( ( pix + 16 ) >> 5 );
1765 /* NxN arbitray size (and then only use pixel in the NxN block)
1767 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1768 int i_height, int i_width )
1775 /* Detect interlacing */
1776 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1779 for( y = 0; y < i_height - 2; y += 2 )
1781 const uint8_t *s = &src[y*i_src];
1782 for( x = 0; x < i_width; x++ )
1784 fr += ssd(s[ x] - s[1*i_src+x]);
1785 ff += ssd(s[ x] - s[2*i_src+x]);
1787 if( ff < fr && fr > i_width / 2 )
1791 return fc < 2 ? VLC_FALSE : VLC_TRUE;
1794 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1795 uint8_t *src, int i_src,
1796 int i_width, int i_height )
1801 for( y = 0; y < i_height; y += 2 )
1803 memcpy( dst, src, i_width );
1806 if( y < i_height - 2 )
1808 for( x = 0; x < i_width; x++ )
1809 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1813 /* Blend last line */
1814 for( x = 0; x < i_width; x++ )
1815 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1822 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1823 uint8_t *src, int i_src,
1824 int i_width, int i_height )
1829 for( y = 0; y < i_height; y += 2 )
1831 memcpy( dst, src, i_width );
1834 if( y < i_height - 2 )
1836 for( x = 0; x < i_width; x++ )
1837 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1841 /* Blend last line */
1842 for( x = 0; x < i_width; x++ )
1843 dst[x] = (src[x] + src[i_src+x]) >> 1;
1850 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1851 int i_width, int i_height )
1853 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1854 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1856 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1860 static inline int median( int a, int b, int c )
1862 int min = a, max =a;
1873 return a + b + c - min - max;
1879 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1880 uint8_t *src, int i_src,
1881 const int i_mbx, int i_modx )
1885 for( x = 0; x < i_mbx; x++ )
1888 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1890 if( x == 0 || x == i_mbx - 1 )
1891 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1893 XDeint8x8FieldC( dst, i_dst, src, i_src );
1897 XDeint8x8MergeC( dst, i_dst,
1898 &src[0*i_src], 2*i_src,
1899 &src[1*i_src], 2*i_src );
1907 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1909 #ifdef CAN_COMPILE_MMXEXT
1910 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1911 uint8_t *src, int i_src,
1912 const int i_mbx, int i_modx )
1916 /* Reset current line */
1917 for( x = 0; x < i_mbx; x++ )
1920 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1922 if( x == 0 || x == i_mbx - 1 )
1923 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1925 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1929 XDeint8x8MergeMMXEXT( dst, i_dst,
1930 &src[0*i_src], 2*i_src,
1931 &src[1*i_src], 2*i_src );
1939 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1943 static void RenderX( vout_thread_t *p_vout,
1944 picture_t *p_outpic, picture_t *p_pic )
1948 /* Copy image and skip lines */
1949 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1951 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1952 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1954 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1955 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1957 const int i_dst = p_outpic->p[i_plane].i_pitch;
1958 const int i_src = p_pic->p[i_plane].i_pitch;
1962 for( y = 0; y < i_mby; y++ )
1964 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1965 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1967 #ifdef CAN_COMPILE_MMXEXT
1968 if( vlc_CPU & CPU_CAPABILITY_MMXEXT )
1969 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1972 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1975 /* Last line (C only)*/
1978 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1979 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1981 for( x = 0; x < i_mbx; x++ )
1983 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1990 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1994 #ifdef CAN_COMPILE_MMXEXT
1995 if( vlc_CPU & CPU_CAPABILITY_MMXEXT )
2000 /*****************************************************************************
2001 * SendEvents: forward mouse and keyboard events to the parent p_vout
2002 *****************************************************************************/
2003 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
2004 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
2006 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
2007 vlc_value_t sentval = newval;
2009 if( !strcmp( psz_var, "mouse-y" ) )
2011 switch( p_vout->p_sys->i_mode )
2013 case DEINTERLACE_MEAN:
2014 case DEINTERLACE_DISCARD:
2020 var_Set( p_vout, psz_var, sentval );
2025 /*****************************************************************************
2026 * FilterCallback: called when changing the deinterlace method on the fly.
2027 *****************************************************************************/
2028 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
2029 vlc_value_t oldval, vlc_value_t newval,
2032 vout_thread_t * p_vout = (vout_thread_t *)p_this;
2033 int i_old_mode = p_vout->p_sys->i_mode;
2035 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
2037 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
2039 SetFilterMethod( p_vout, newval.psz_string );
2041 switch( p_vout->render.i_chroma )
2043 case VLC_FOURCC('I','4','2','2'):
2044 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2048 case VLC_FOURCC('I','4','2','0'):
2049 case VLC_FOURCC('I','Y','U','V'):
2050 case VLC_FOURCC('Y','V','1','2'):
2051 switch( p_vout->p_sys->i_mode )
2053 case DEINTERLACE_MEAN:
2054 case DEINTERLACE_DISCARD:
2055 if( ( i_old_mode == DEINTERLACE_MEAN )
2056 || ( i_old_mode == DEINTERLACE_DISCARD ) )
2058 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2063 case DEINTERLACE_BOB:
2064 case DEINTERLACE_BLEND:
2065 case DEINTERLACE_LINEAR:
2066 if( ( i_old_mode == DEINTERLACE_BOB )
2067 || ( i_old_mode == DEINTERLACE_BLEND )
2068 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2070 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2081 /* We need to kill the old vout */
2083 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2085 vlc_object_detach( p_vout->p_sys->p_vout );
2086 vout_Destroy( p_vout->p_sys->p_vout );
2088 /* Try to open a new video output */
2089 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2091 if( p_vout->p_sys->p_vout == NULL )
2093 /* Everything failed */
2094 msg_Err( p_vout, "cannot open vout, aborting" );
2096 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2097 return VLC_EGENERIC;
2100 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2102 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2106 /*****************************************************************************
2107 * SendEventsToChild: forward events to the child/children vout
2108 *****************************************************************************/
2109 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2110 vlc_value_t oldval, vlc_value_t newval, void *p_data )
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 = p_filter->pf_vout_buffer_new( p_filter );
2128 if( p_pic_dst == NULL )
2130 msg_Warn( p_filter, "can't get output picture" );
2134 switch( p_vout->p_sys->i_mode )
2136 case DEINTERLACE_DISCARD:
2138 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2140 msg_Err( p_vout, "discarding lines is not supported yet" );
2141 p_pic_dst->pf_release( p_pic_dst );
2145 case DEINTERLACE_BOB:
2147 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2148 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2152 case DEINTERLACE_LINEAR:
2154 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2155 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2157 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2158 p_pic_dst->pf_release( p_pic_dst );
2162 case DEINTERLACE_MEAN:
2163 RenderMean( p_vout, p_pic_dst, p_pic );
2166 case DEINTERLACE_BLEND:
2167 RenderBlend( p_vout, p_pic_dst, p_pic );
2171 RenderX( p_vout, p_pic_dst, p_pic );
2175 p_pic_dst->date = p_pic->date;
2176 p_pic_dst->b_force = p_pic->b_force;
2177 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2178 p_pic_dst->b_progressive = VLC_TRUE;
2179 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2181 p_pic->pf_release( p_pic );
2185 /*****************************************************************************
2187 *****************************************************************************/
2188 static int OpenFilter( vlc_object_t *p_this )
2190 filter_t *p_filter = (filter_t*)p_this;
2191 vout_thread_t *p_vout;
2194 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2195 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2196 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2197 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2199 return VLC_EGENERIC;
2202 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2204 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2205 vlc_object_attach( p_vout, p_filter );
2206 p_filter->p_sys = (filter_sys_t *)p_vout;
2207 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2209 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2211 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2212 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2213 var_Set( p_filter, "deinterlace-mode", val );
2215 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2217 vlc_object_detach( p_vout );
2218 vlc_object_release( p_vout );
2219 return VLC_EGENERIC;
2222 p_filter->pf_video_filter = Deinterlace;
2224 msg_Dbg( p_filter, "deinterlacing" );
2229 /*****************************************************************************
2230 * CloseFilter: clean up the filter
2231 *****************************************************************************/
2232 static void CloseFilter( vlc_object_t *p_this )
2234 filter_t *p_filter = (filter_t*)p_this;
2235 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2237 Destroy( VLC_OBJECT(p_vout) );
2238 vlc_object_detach( p_vout );
2239 vlc_object_release( p_vout );