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 *****************************************************************************/
36 #include "vlc_filter.h"
42 #ifdef CAN_COMPILE_MMXEXT
46 #include "filter_common.h"
48 #define DEINTERLACE_DISCARD 1
49 #define DEINTERLACE_MEAN 2
50 #define DEINTERLACE_BLEND 3
51 #define DEINTERLACE_BOB 4
52 #define DEINTERLACE_LINEAR 5
53 #define DEINTERLACE_X 6
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Create ( vlc_object_t * );
59 static void Destroy ( vlc_object_t * );
61 static int Init ( vout_thread_t * );
62 static void End ( vout_thread_t * );
63 static void Render ( vout_thread_t *, picture_t * );
65 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
66 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
67 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
68 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
69 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
70 static void RenderX ( picture_t *, picture_t * );
72 static void MergeGeneric ( void *, const void *, const void *, size_t );
73 #if defined(CAN_COMPILE_C_ALTIVEC)
74 static void MergeAltivec ( void *, const void *, const void *, size_t );
76 #if defined(CAN_COMPILE_MMXEXT)
77 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
79 #if defined(CAN_COMPILE_3DNOW)
80 static void Merge3DNow ( void *, const void *, const void *, size_t );
82 #if defined(CAN_COMPILE_SSE)
83 static void MergeSSE2 ( void *, const void *, const void *, size_t );
85 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
86 static void EndMMX ( void );
88 #if defined(CAN_COMPILE_3DNOW)
89 static void End3DNow ( void );
92 static int SendEvents ( vlc_object_t *, char const *,
93 vlc_value_t, vlc_value_t, void * );
95 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
96 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
98 static int OpenFilter( vlc_object_t *p_this );
99 static void CloseFilter( vlc_object_t *p_this );
101 /*****************************************************************************
102 * Callback prototypes
103 *****************************************************************************/
104 static int FilterCallback ( vlc_object_t *, char const *,
105 vlc_value_t, vlc_value_t, void * );
107 /*****************************************************************************
109 *****************************************************************************/
110 #define MODE_TEXT N_("Deinterlace mode")
111 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
113 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
114 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
116 #define FILTER_CFG_PREFIX "sout-deinterlace-"
118 static const char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", "x" };
119 static const char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
120 N_("Bob"), N_("Linear"), "X" };
123 set_description( _("Deinterlacing video filter") );
124 set_shortname( _("Deinterlace" ));
125 set_capability( "video filter", 0 );
126 set_category( CAT_VIDEO );
127 set_subcategory( SUBCAT_VIDEO_VFILTER );
129 set_section( N_("Display"),NULL);
130 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
131 MODE_LONGTEXT, false );
132 change_string_list( mode_list, mode_list_text, 0 );
134 add_shortcut( "deinterlace" );
135 set_callbacks( Create, Destroy );
138 set_capability( "video filter2", 0 );
139 set_section( N_("Streaming"),NULL);
140 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
141 SOUT_MODE_LONGTEXT, false );
142 change_string_list( mode_list, mode_list_text, 0 );
143 set_callbacks( OpenFilter, CloseFilter );
146 static const char *ppsz_filter_options[] = {
150 /*****************************************************************************
151 * vout_sys_t: Deinterlace video output method descriptor
152 *****************************************************************************
153 * This structure is part of the video output thread descriptor.
154 * It describes the Deinterlace specific properties of an output thread.
155 *****************************************************************************/
158 int i_mode; /* Deinterlace mode */
159 bool b_double_rate; /* Shall we double the framerate? */
164 vout_thread_t *p_vout;
166 vlc_mutex_t filter_lock;
168 void (*pf_merge) ( void *, const void *, const void *, size_t );
169 void (*pf_end_merge) ( void );
172 /*****************************************************************************
173 * Control: control facility for the vout (forwards to child vout)
174 *****************************************************************************/
175 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
177 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
180 /*****************************************************************************
181 * Create: allocates Deinterlace video thread output method
182 *****************************************************************************
183 * This function allocates and initializes a Deinterlace vout method.
184 *****************************************************************************/
185 static int Create( vlc_object_t *p_this )
187 vout_thread_t *p_vout = (vout_thread_t *)p_this;
190 /* Allocate structure */
191 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
192 if( p_vout->p_sys == NULL )
194 msg_Err( p_vout, "out of memory" );
198 p_vout->pf_init = Init;
199 p_vout->pf_end = End;
200 p_vout->pf_manage = NULL;
201 p_vout->pf_render = Render;
202 p_vout->pf_display = NULL;
203 p_vout->pf_control = Control;
205 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
206 p_vout->p_sys->b_double_rate = false;
207 p_vout->p_sys->last_date = 0;
208 p_vout->p_sys->p_vout = 0;
209 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
211 #if defined(CAN_COMPILE_C_ALTIVEC)
212 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
214 p_vout->p_sys->pf_merge = MergeAltivec;
215 p_vout->p_sys->pf_end_merge = NULL;
219 #if defined(CAN_COMPILE_SSE)
220 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
222 p_vout->p_sys->pf_merge = MergeSSE2;
223 p_vout->p_sys->pf_end_merge = EndMMX;
227 #if defined(CAN_COMPILE_MMXEXT)
228 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
230 p_vout->p_sys->pf_merge = MergeMMXEXT;
231 p_vout->p_sys->pf_end_merge = EndMMX;
235 #if defined(CAN_COMPILE_3DNOW)
236 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
238 p_vout->p_sys->pf_merge = Merge3DNow;
239 p_vout->p_sys->pf_end_merge = End3DNow;
244 p_vout->p_sys->pf_merge = MergeGeneric;
245 p_vout->p_sys->pf_end_merge = NULL;
248 /* Look what method was requested */
249 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
250 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
252 if( val.psz_string == NULL )
254 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
255 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
257 val.psz_string = strdup( "discard" );
260 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
262 SetFilterMethod( p_vout, val.psz_string );
264 free( val.psz_string );
269 /*****************************************************************************
270 * SetFilterMethod: setup the deinterlace method to use.
271 *****************************************************************************/
272 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
274 if( !strcmp( psz_method, "discard" ) )
276 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
277 p_vout->p_sys->b_double_rate = false;
279 else if( !strcmp( psz_method, "mean" ) )
281 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
282 p_vout->p_sys->b_double_rate = false;
284 else if( !strcmp( psz_method, "blend" )
285 || !strcmp( psz_method, "average" )
286 || !strcmp( psz_method, "combine-fields" ) )
288 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
289 p_vout->p_sys->b_double_rate = false;
291 else if( !strcmp( psz_method, "bob" )
292 || !strcmp( psz_method, "progressive-scan" ) )
294 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
295 p_vout->p_sys->b_double_rate = true;
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;
302 else if( !strcmp( psz_method, "x" ) )
304 p_vout->p_sys->i_mode = DEINTERLACE_X;
305 p_vout->p_sys->b_double_rate = false;
309 msg_Err( p_vout, "no valid deinterlace mode provided, "
310 "using \"discard\"" );
313 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
316 /*****************************************************************************
317 * Init: initialize Deinterlace video thread output method
318 *****************************************************************************/
319 static int Init( vout_thread_t *p_vout )
324 I_OUTPUTPICTURES = 0;
326 /* Initialize the output structure, full of directbuffers since we want
327 * the decoder to output directly to our structures. */
328 switch( p_vout->render.i_chroma )
330 case VLC_FOURCC('I','4','2','0'):
331 case VLC_FOURCC('I','Y','U','V'):
332 case VLC_FOURCC('Y','V','1','2'):
333 case VLC_FOURCC('I','4','2','2'):
334 p_vout->output.i_chroma = p_vout->render.i_chroma;
335 p_vout->output.i_width = p_vout->render.i_width;
336 p_vout->output.i_height = p_vout->render.i_height;
337 p_vout->output.i_aspect = p_vout->render.i_aspect;
338 p_vout->fmt_out = p_vout->fmt_in;
342 return VLC_EGENERIC; /* unknown chroma */
346 /* Try to open the real video output */
347 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
349 if( p_vout->p_sys->p_vout == NULL )
351 /* Everything failed */
352 msg_Err( p_vout, "cannot open vout, aborting" );
357 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
359 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
361 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
363 ADD_PARENT_CALLBACKS( SendEventsToChild );
368 /*****************************************************************************
369 * SpawnRealVout: spawn the real video output.
370 *****************************************************************************/
371 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
373 vout_thread_t *p_real_vout = NULL;
375 memset( &fmt, 0, sizeof( video_format_t ) );
377 msg_Dbg( p_vout, "spawning the real video output" );
379 fmt = p_vout->fmt_out;
381 switch( p_vout->render.i_chroma )
383 case VLC_FOURCC('I','4','2','0'):
384 case VLC_FOURCC('I','Y','U','V'):
385 case VLC_FOURCC('Y','V','1','2'):
386 switch( p_vout->p_sys->i_mode )
388 case DEINTERLACE_MEAN:
389 case DEINTERLACE_DISCARD:
390 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
392 p_real_vout = vout_Create( p_vout, &fmt );
395 case DEINTERLACE_BOB:
396 case DEINTERLACE_BLEND:
397 case DEINTERLACE_LINEAR:
399 p_real_vout = vout_Create( p_vout, &fmt );
404 case VLC_FOURCC('I','4','2','2'):
405 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
406 p_real_vout = vout_Create( p_vout, &fmt );
416 /*****************************************************************************
417 * End: terminate Deinterlace video thread output method
418 *****************************************************************************/
419 static void End( vout_thread_t *p_vout )
423 /* Free the fake output buffers we allocated */
424 for( i_index = I_OUTPUTPICTURES ; i_index ; )
427 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
430 if( p_vout->p_sys->p_vout )
432 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
433 vlc_object_detach( p_vout->p_sys->p_vout );
434 vout_Destroy( p_vout->p_sys->p_vout );
437 DEL_PARENT_CALLBACKS( SendEventsToChild );
440 /*****************************************************************************
441 * Destroy: destroy Deinterlace video thread output method
442 *****************************************************************************
443 * Terminate an output method created by DeinterlaceCreateOutputMethod
444 *****************************************************************************/
445 static void Destroy( vlc_object_t *p_this )
447 vout_thread_t *p_vout = (vout_thread_t *)p_this;
448 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
449 free( p_vout->p_sys );
452 /*****************************************************************************
453 * Render: displays previously rendered output
454 *****************************************************************************
455 * This function send the currently rendered image to Deinterlace image,
456 * waits until it is displayed and switch the two rendering buffers, preparing
458 *****************************************************************************/
459 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
461 vout_sys_t *p_sys = p_vout->p_sys;
462 picture_t *pp_outpic[2];
464 p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
465 p_vout->fmt_in.i_x_offset;
466 p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
467 p_vout->fmt_in.i_y_offset;
468 p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
469 p_vout->fmt_in.i_visible_width;
470 p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
471 p_vout->fmt_in.i_visible_height;
472 if( p_vout->p_sys->i_mode == DEINTERLACE_MEAN ||
473 p_vout->p_sys->i_mode == DEINTERLACE_DISCARD )
475 p_vout->fmt_out.i_y_offset /= 2; p_sys->p_vout->fmt_in.i_y_offset /= 2;
476 p_vout->fmt_out.i_visible_height /= 2;
477 p_sys->p_vout->fmt_in.i_visible_height /= 2;
480 pp_outpic[0] = pp_outpic[1] = NULL;
482 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
484 /* Get a new picture */
485 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
489 if( p_vout->b_die || p_vout->b_error )
491 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
494 msleep( VOUT_OUTMEM_SLEEP );
497 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
499 /* If we are using double rate, get an additional new picture */
500 if( p_vout->p_sys->b_double_rate )
502 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
506 if( p_vout->b_die || p_vout->b_error )
508 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
509 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
512 msleep( VOUT_OUTMEM_SLEEP );
515 /* 20ms is a bit arbitrary, but it's only for the first image we get */
516 if( !p_vout->p_sys->last_date )
518 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
519 p_pic->date + 20000 );
523 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
524 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
526 p_vout->p_sys->last_date = p_pic->date;
529 switch( p_vout->p_sys->i_mode )
531 case DEINTERLACE_DISCARD:
532 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
533 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
536 case DEINTERLACE_BOB:
537 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
538 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
539 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
540 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
543 case DEINTERLACE_LINEAR:
544 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
545 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
546 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
547 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
550 case DEINTERLACE_MEAN:
551 RenderMean( p_vout, pp_outpic[0], p_pic );
552 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
555 case DEINTERLACE_BLEND:
556 RenderBlend( p_vout, pp_outpic[0], p_pic );
557 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
561 RenderX( pp_outpic[0], p_pic );
562 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
565 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
568 /*****************************************************************************
569 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
570 *****************************************************************************/
571 static void RenderDiscard( vout_thread_t *p_vout,
572 picture_t *p_outpic, picture_t *p_pic, int i_field )
576 /* Copy image and skip lines */
577 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
579 uint8_t *p_in, *p_out_end, *p_out;
582 p_in = p_pic->p[i_plane].p_pixels
583 + i_field * p_pic->p[i_plane].i_pitch;
585 p_out = p_outpic->p[i_plane].p_pixels;
586 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
587 * p_outpic->p[i_plane].i_visible_lines;
589 switch( p_vout->render.i_chroma )
591 case VLC_FOURCC('I','4','2','0'):
592 case VLC_FOURCC('I','Y','U','V'):
593 case VLC_FOURCC('Y','V','1','2'):
595 for( ; p_out < p_out_end ; )
597 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
598 p_pic->p[i_plane].i_pitch );
600 p_out += p_outpic->p[i_plane].i_pitch;
601 p_in += 2 * p_pic->p[i_plane].i_pitch;
605 case VLC_FOURCC('I','4','2','2'):
607 i_increment = 2 * p_pic->p[i_plane].i_pitch;
609 if( i_plane == Y_PLANE )
611 for( ; p_out < p_out_end ; )
613 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
614 p_pic->p[i_plane].i_pitch );
615 p_out += p_outpic->p[i_plane].i_pitch;
616 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
617 p_pic->p[i_plane].i_pitch );
618 p_out += p_outpic->p[i_plane].i_pitch;
624 for( ; p_out < p_out_end ; )
626 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
627 p_pic->p[i_plane].i_pitch );
628 p_out += p_outpic->p[i_plane].i_pitch;
640 /*****************************************************************************
641 * RenderBob: renders a BOB picture - simple copy
642 *****************************************************************************/
643 static void RenderBob( vout_thread_t *p_vout,
644 picture_t *p_outpic, picture_t *p_pic, int i_field )
648 /* Copy image and skip lines */
649 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
651 uint8_t *p_in, *p_out_end, *p_out;
653 p_in = p_pic->p[i_plane].p_pixels;
654 p_out = p_outpic->p[i_plane].p_pixels;
655 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
656 * p_outpic->p[i_plane].i_visible_lines;
658 switch( p_vout->render.i_chroma )
660 case VLC_FOURCC('I','4','2','0'):
661 case VLC_FOURCC('I','Y','U','V'):
662 case VLC_FOURCC('Y','V','1','2'):
663 /* For BOTTOM field we need to add the first line */
666 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
667 p_pic->p[i_plane].i_pitch );
668 p_in += p_pic->p[i_plane].i_pitch;
669 p_out += p_outpic->p[i_plane].i_pitch;
672 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
674 for( ; p_out < p_out_end ; )
676 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
677 p_pic->p[i_plane].i_pitch );
679 p_out += p_outpic->p[i_plane].i_pitch;
681 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
682 p_pic->p[i_plane].i_pitch );
684 p_in += 2 * p_pic->p[i_plane].i_pitch;
685 p_out += p_outpic->p[i_plane].i_pitch;
688 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
689 p_pic->p[i_plane].i_pitch );
691 /* For TOP field we need to add the last line */
694 p_in += p_pic->p[i_plane].i_pitch;
695 p_out += p_outpic->p[i_plane].i_pitch;
696 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
697 p_pic->p[i_plane].i_pitch );
701 case VLC_FOURCC('I','4','2','2'):
702 /* For BOTTOM field we need to add the first line */
705 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
706 p_pic->p[i_plane].i_pitch );
707 p_in += p_pic->p[i_plane].i_pitch;
708 p_out += p_outpic->p[i_plane].i_pitch;
711 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
713 if( i_plane == Y_PLANE )
715 for( ; p_out < p_out_end ; )
717 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
718 p_pic->p[i_plane].i_pitch );
720 p_out += p_outpic->p[i_plane].i_pitch;
722 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
723 p_pic->p[i_plane].i_pitch );
725 p_in += 2 * p_pic->p[i_plane].i_pitch;
726 p_out += p_outpic->p[i_plane].i_pitch;
731 for( ; p_out < p_out_end ; )
733 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
734 p_pic->p[i_plane].i_pitch );
736 p_out += p_outpic->p[i_plane].i_pitch;
737 p_in += 2 * p_pic->p[i_plane].i_pitch;
741 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
742 p_pic->p[i_plane].i_pitch );
744 /* For TOP field we need to add the last line */
747 p_in += p_pic->p[i_plane].i_pitch;
748 p_out += p_outpic->p[i_plane].i_pitch;
749 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
750 p_pic->p[i_plane].i_pitch );
757 #define Merge p_vout->p_sys->pf_merge
758 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
760 /*****************************************************************************
761 * RenderLinear: BOB with linear interpolation
762 *****************************************************************************/
763 static void RenderLinear( vout_thread_t *p_vout,
764 picture_t *p_outpic, picture_t *p_pic, int i_field )
768 /* Copy image and skip lines */
769 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
771 uint8_t *p_in, *p_out_end, *p_out;
773 p_in = p_pic->p[i_plane].p_pixels;
774 p_out = p_outpic->p[i_plane].p_pixels;
775 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
776 * p_outpic->p[i_plane].i_visible_lines;
778 /* For BOTTOM field we need to add the first line */
781 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
782 p_pic->p[i_plane].i_pitch );
783 p_in += p_pic->p[i_plane].i_pitch;
784 p_out += p_outpic->p[i_plane].i_pitch;
787 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
789 for( ; p_out < p_out_end ; )
791 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
792 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 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
804 p_pic->p[i_plane].i_pitch );
806 /* For TOP field we need to add the last line */
809 p_in += p_pic->p[i_plane].i_pitch;
810 p_out += p_outpic->p[i_plane].i_pitch;
811 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
812 p_pic->p[i_plane].i_pitch );
818 static void RenderMean( vout_thread_t *p_vout,
819 picture_t *p_outpic, picture_t *p_pic )
823 /* Copy image and skip lines */
824 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
826 uint8_t *p_in, *p_out_end, *p_out;
828 p_in = p_pic->p[i_plane].p_pixels;
830 p_out = p_outpic->p[i_plane].p_pixels;
831 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
832 * p_outpic->p[i_plane].i_visible_lines;
834 /* All lines: mean value */
835 for( ; p_out < p_out_end ; )
837 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
838 p_pic->p[i_plane].i_pitch );
840 p_out += p_outpic->p[i_plane].i_pitch;
841 p_in += 2 * p_pic->p[i_plane].i_pitch;
847 static void RenderBlend( vout_thread_t *p_vout,
848 picture_t *p_outpic, picture_t *p_pic )
852 /* Copy image and skip lines */
853 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
855 uint8_t *p_in, *p_out_end, *p_out;
857 p_in = p_pic->p[i_plane].p_pixels;
859 p_out = p_outpic->p[i_plane].p_pixels;
860 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
861 * p_outpic->p[i_plane].i_visible_lines;
863 switch( p_vout->render.i_chroma )
865 case VLC_FOURCC('I','4','2','0'):
866 case VLC_FOURCC('I','Y','U','V'):
867 case VLC_FOURCC('Y','V','1','2'):
868 /* First line: simple copy */
869 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
870 p_pic->p[i_plane].i_pitch );
871 p_out += p_outpic->p[i_plane].i_pitch;
873 /* Remaining lines: mean value */
874 for( ; p_out < p_out_end ; )
876 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
877 p_pic->p[i_plane].i_pitch );
879 p_out += p_outpic->p[i_plane].i_pitch;
880 p_in += p_pic->p[i_plane].i_pitch;
884 case VLC_FOURCC('I','4','2','2'):
885 /* First line: simple copy */
886 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
887 p_pic->p[i_plane].i_pitch );
888 p_out += p_outpic->p[i_plane].i_pitch;
890 /* Remaining lines: mean value */
891 if( i_plane == Y_PLANE )
893 for( ; p_out < p_out_end ; )
895 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
896 p_pic->p[i_plane].i_pitch );
898 p_out += p_outpic->p[i_plane].i_pitch;
899 p_in += p_pic->p[i_plane].i_pitch;
905 for( ; p_out < p_out_end ; )
907 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
908 p_pic->p[i_plane].i_pitch );
910 p_out += p_outpic->p[i_plane].i_pitch;
911 p_in += 2*p_pic->p[i_plane].i_pitch;
922 static void MergeGeneric( void *_p_dest, const void *_p_s1,
923 const void *_p_s2, size_t i_bytes )
925 uint8_t* p_dest = (uint8_t*)_p_dest;
926 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
927 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
928 uint8_t* p_end = p_dest + i_bytes - 8;
930 while( p_dest < p_end )
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;
938 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
939 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
944 while( p_dest < p_end )
946 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
950 #if defined(CAN_COMPILE_MMXEXT)
951 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
954 uint8_t* p_dest = (uint8_t*)_p_dest;
955 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
956 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
957 uint8_t* p_end = p_dest + i_bytes - 8;
958 while( p_dest < p_end )
960 __asm__ __volatile__( "movq %2,%%mm1;"
962 "movq %%mm1, %0" :"=m" (*p_dest):
972 while( p_dest < p_end )
974 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
979 #if defined(CAN_COMPILE_3DNOW)
980 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
983 uint8_t* p_dest = (uint8_t*)_p_dest;
984 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
985 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
986 uint8_t* p_end = p_dest + i_bytes - 8;
987 while( p_dest < p_end )
989 __asm__ __volatile__( "movq %2,%%mm1;"
991 "movq %%mm1, %0" :"=m" (*p_dest):
1001 while( p_dest < p_end )
1003 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1008 #if defined(CAN_COMPILE_SSE)
1009 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1012 uint8_t* p_dest = (uint8_t*)_p_dest;
1013 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1014 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1016 while( (uintptr_t)p_s1 % 16 )
1018 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1020 p_end = p_dest + i_bytes - 16;
1021 while( p_dest < p_end )
1023 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1025 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1035 while( p_dest < p_end )
1037 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1042 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1043 static void EndMMX( void )
1045 __asm__ __volatile__( "emms" :: );
1049 #if defined(CAN_COMPILE_3DNOW)
1050 static void End3DNow( void )
1052 __asm__ __volatile__( "femms" :: );
1056 #ifdef CAN_COMPILE_C_ALTIVEC
1057 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1058 const void *_p_s2, size_t i_bytes )
1060 uint8_t *p_dest = (uint8_t *)_p_dest;
1061 uint8_t *p_s1 = (uint8_t *)_p_s1;
1062 uint8_t *p_s2 = (uint8_t *)_p_s2;
1063 uint8_t *p_end = p_dest + i_bytes - 15;
1065 /* Use C until the first 16-bytes aligned destination pixel */
1066 while( (int)p_dest & 0xF )
1068 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1071 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1073 /* Unaligned source */
1074 vector unsigned char s1v, s2v, destv;
1075 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1076 vector unsigned char perm1v, perm2v;
1078 perm1v = vec_lvsl( 0, p_s1 );
1079 perm2v = vec_lvsl( 0, p_s2 );
1080 s1oldv = vec_ld( 0, p_s1 );
1081 s2oldv = vec_ld( 0, p_s2 );
1083 while( p_dest < p_end )
1085 s1newv = vec_ld( 16, p_s1 );
1086 s2newv = vec_ld( 16, p_s2 );
1087 s1v = vec_perm( s1oldv, s1newv, perm1v );
1088 s2v = vec_perm( s2oldv, s2newv, perm2v );
1091 destv = vec_avg( s1v, s2v );
1092 vec_st( destv, 0, p_dest );
1101 /* Aligned source */
1102 vector unsigned char s1v, s2v, destv;
1104 while( p_dest < p_end )
1106 s1v = vec_ld( 0, p_s1 );
1107 s2v = vec_ld( 0, p_s2 );
1108 destv = vec_avg( s1v, s2v );
1109 vec_st( destv, 0, p_dest );
1119 while( p_dest < p_end )
1121 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1126 /*****************************************************************************
1127 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1128 * and apply a process to recreate the bottom field :
1129 * If a 8x8 block is classified as :
1130 * - progressive: it applies a small blend (1,6,1)
1132 * * in the MMX version: we do a ME between the 2 fields, if there is a
1133 * good match we use MC to recreate the bottom field (with a small
1135 * * otherwise: it recreates the bottom field by an edge oriented
1137 *****************************************************************************/
1139 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1140 * XXX: It need to access to 8x10
1141 * We use more than 8 lines to help with scrolling (text)
1142 * (and because XDeint8x8Frame use line 9)
1143 * XXX: smooth/uniform area with noise detection doesn't works well
1144 * but it's not really a problem because they don't have much details anyway
1146 static inline int ssd( int a ) { return a*a; }
1147 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1153 /* Detect interlacing */
1155 for( y = 0; y < 7; y += 2 )
1158 for( x = 0; x < 8; x++ )
1160 fr += ssd(src[ x] - src[1*i_src+x]) +
1161 ssd(src[i_src+x] - src[2*i_src+x]);
1162 ff += ssd(src[ x] - src[2*i_src+x]) +
1163 ssd(src[i_src+x] - src[3*i_src+x]);
1165 if( ff < 6*fr/8 && fr > 32 )
1171 return fc < 1 ? false : true;
1173 #ifdef CAN_COMPILE_MMXEXT
1174 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1181 /* Detect interlacing */
1183 pxor_r2r( mm7, mm7 );
1184 for( y = 0; y < 9; y += 2 )
1187 pxor_r2r( mm5, mm5 );
1188 pxor_r2r( mm6, mm6 );
1189 for( x = 0; x < 8; x+=4 )
1191 movd_m2r( src[ x], mm0 );
1192 movd_m2r( src[1*i_src+x], mm1 );
1193 movd_m2r( src[2*i_src+x], mm2 );
1194 movd_m2r( src[3*i_src+x], mm3 );
1196 punpcklbw_r2r( mm7, mm0 );
1197 punpcklbw_r2r( mm7, mm1 );
1198 punpcklbw_r2r( mm7, mm2 );
1199 punpcklbw_r2r( mm7, mm3 );
1201 movq_r2r( mm0, mm4 );
1203 psubw_r2r( mm1, mm0 );
1204 psubw_r2r( mm2, mm4 );
1206 psubw_r2r( mm1, mm2 );
1207 psubw_r2r( mm1, mm3 );
1209 pmaddwd_r2r( mm0, mm0 );
1210 pmaddwd_r2r( mm4, mm4 );
1211 pmaddwd_r2r( mm2, mm2 );
1212 pmaddwd_r2r( mm3, mm3 );
1213 paddd_r2r( mm0, mm2 );
1214 paddd_r2r( mm4, mm3 );
1215 paddd_r2r( mm2, mm5 );
1216 paddd_r2r( mm3, mm6 );
1219 movq_r2r( mm5, mm0 );
1220 psrlq_i2r( 32, mm0 );
1221 paddd_r2r( mm0, mm5 );
1222 movd_r2m( mm5, fr );
1224 movq_r2r( mm6, mm0 );
1225 psrlq_i2r( 32, mm0 );
1226 paddd_r2r( mm0, mm6 );
1227 movd_r2m( mm6, ff );
1229 if( ff < 6*fr/8 && fr > 32 )
1238 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1239 * This won't destroy details, and help if there is a bit of interlacing.
1240 * (It helps with paning to avoid flickers)
1244 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1245 uint8_t *src, int i_src )
1250 for( y = 0; y < 8; y += 2 )
1252 memcpy( dst, src, 8 );
1255 for( x = 0; x < 8; x++ )
1256 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1262 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1263 uint8_t *src1, int i_src1,
1264 uint8_t *src2, int i_src2 )
1269 for( y = 0; y < 8; y += 2 )
1271 memcpy( dst, src1, 8 );
1274 for( x = 0; x < 8; x++ )
1275 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1283 #ifdef CAN_COMPILE_MMXEXT
1284 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1285 uint8_t *src1, int i_src1,
1286 uint8_t *src2, int i_src2 )
1288 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1292 pxor_r2r( mm7, mm7 );
1293 for( y = 0; y < 8; y += 2 )
1295 for( x = 0; x < 8; x +=4 )
1297 movd_m2r( src1[x], mm0 );
1298 movd_r2m( mm0, dst[x] );
1300 movd_m2r( src2[x], mm1 );
1301 movd_m2r( src1[i_src1+x], mm2 );
1303 punpcklbw_r2r( mm7, mm0 );
1304 punpcklbw_r2r( mm7, mm1 );
1305 punpcklbw_r2r( mm7, mm2 );
1306 paddw_r2r( mm1, mm1 );
1307 movq_r2r( mm1, mm3 );
1308 paddw_r2r( mm3, mm3 );
1309 paddw_r2r( mm2, mm0 );
1310 paddw_r2r( mm3, mm1 );
1311 paddw_m2r( m_4, mm1 );
1312 paddw_r2r( mm1, mm0 );
1313 psraw_i2r( 3, mm0 );
1314 packuswb_r2r( mm7, mm0 );
1315 movd_r2m( mm0, dst[i_dst+x] );
1326 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1329 for( y = 0; y < 8; y++ )
1330 memset( &dst[y*i_dst], v, 8 );
1333 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1336 * TODO: a better one for the inner part.
1338 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1339 uint8_t *src, int i_src )
1344 for( y = 0; y < 8; y += 2 )
1346 memcpy( dst, src, 8 );
1349 for( x = 0; x < 8; x++ )
1350 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1355 #ifdef CAN_COMPILE_MMXEXT
1356 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1357 uint8_t *src, int i_src )
1362 for( y = 0; y < 8; y += 2 )
1364 movq_m2r( src[0], mm0 );
1365 movq_r2m( mm0, dst[0] );
1368 movq_m2r( src[2*i_src], mm1 );
1369 pavgb_r2r( mm1, mm0 );
1371 movq_r2m( mm0, dst[0] );
1379 /* XDeint8x8Field: Edge oriented interpolation
1380 * (Need -4 and +5 pixels H, +1 line)
1382 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1383 uint8_t *src, int i_src )
1388 for( y = 0; y < 8; y += 2 )
1390 memcpy( dst, src, 8 );
1393 for( x = 0; x < 8; x++ )
1395 uint8_t *src2 = &src[2*i_src];
1396 /* I use 8 pixels just to match the MMX version, but it's overkill
1397 * 5 would be enough (less isn't good) */
1398 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1399 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1400 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1401 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1403 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1404 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1405 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1406 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1408 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1409 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1410 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1411 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1413 if( c0 < c1 && c1 <= c2 )
1414 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1415 else if( c2 < c1 && c1 <= c0 )
1416 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1418 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1425 #ifdef CAN_COMPILE_MMXEXT
1426 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1427 uint8_t *src, int i_src )
1432 for( y = 0; y < 8; y += 2 )
1434 memcpy( dst, src, 8 );
1437 for( x = 0; x < 8; x++ )
1439 uint8_t *src2 = &src[2*i_src];
1442 movq_m2r( src[x-2], mm0 );
1443 movq_m2r( src[x-3], mm1 );
1444 movq_m2r( src[x-4], mm2 );
1446 psadbw_m2r( src2[x-4], mm0 );
1447 psadbw_m2r( src2[x-3], mm1 );
1448 psadbw_m2r( src2[x-2], mm2 );
1450 movd_r2m( mm0, c2 );
1451 movd_r2m( mm1, c1 );
1452 movd_r2m( mm2, c0 );
1454 if( c0 < c1 && c1 <= c2 )
1455 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1456 else if( c2 < c1 && c1 <= c0 )
1457 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1459 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1469 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1470 uint8_t *pix2, int i_pix2 )
1475 for( y = 0; y < 8; y++ )
1476 for( x = 0; x < 8; x++ )
1477 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1481 #ifdef CAN_COMPILE_MMXEXT
1482 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1483 uint8_t *pix2, int i_pix2 )
1488 pxor_r2r( mm7, mm7 );
1489 pxor_r2r( mm6, mm6 );
1491 for( y = 0; y < 8; y++ )
1493 movq_m2r( pix1[0], mm0 );
1494 movq_m2r( pix2[0], mm1 );
1496 movq_r2r( mm0, mm2 );
1497 movq_r2r( mm1, mm3 );
1499 punpcklbw_r2r( mm7, mm0 );
1500 punpckhbw_r2r( mm7, mm2 );
1501 punpcklbw_r2r( mm7, mm1 );
1502 punpckhbw_r2r( mm7, mm3 );
1504 psubw_r2r( mm1, mm0 );
1505 psubw_r2r( mm3, mm2 );
1507 pmaddwd_r2r( mm0, mm0 );
1508 pmaddwd_r2r( mm2, mm2 );
1510 paddd_r2r( mm2, mm0 );
1511 paddd_r2r( mm0, mm6 );
1517 movq_r2r( mm6, mm7 );
1518 psrlq_i2r( 32, mm7 );
1519 paddd_r2r( mm6, mm7 );
1528 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1529 #ifdef CAN_COMPILE_MMXEXT
1532 * TODO: mmx version (easier in sse2)
1534 static inline void XDeintMC( uint8_t *dst, int i_dst,
1535 uint8_t *src, int i_src,
1537 int i_width, int i_height )
1539 const int d4x = mvx&0x03;
1540 const int d4y = mvy&0x03;
1542 const int cA = (4-d4x)*(4-d4y);
1543 const int cB = d4x *(4-d4y);
1544 const int cC = (4-d4x)*d4y;
1545 const int cD = d4x *d4y;
1551 src += (mvy >> 2) * i_src + (mvx >> 2);
1554 for( y = 0; y < i_height; y++ )
1556 for( x = 0; x < i_width; x++ )
1558 dst[x] = ( cA*src[x] + cB*src[x+1] +
1559 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1567 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1568 uint8_t *pix2, int i_pix2 )
1572 movq_m2r( pix1[0*i_pix1], mm0 );
1573 movq_m2r( pix1[1*i_pix1], mm1 );
1575 psadbw_m2r( pix2[0*i_pix2], mm0 );
1576 psadbw_m2r( pix2[1*i_pix2], mm1 );
1578 movq_m2r( pix1[2*i_pix1], mm2 );
1579 movq_m2r( pix1[3*i_pix1], mm3 );
1580 psadbw_m2r( pix2[2*i_pix2], mm2 );
1581 psadbw_m2r( pix2[3*i_pix2], mm3 );
1583 paddd_r2r( mm1, mm0 );
1584 paddd_r2r( mm3, mm2 );
1585 paddd_r2r( mm2, mm0 );
1591 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1592 uint8_t *ref, int i_stride,
1594 int xmax, int ymax )
1596 uint8_t buffer[8*4];
1598 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1601 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1602 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1604 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1605 uint8_t *ref, int i_stride,
1607 int xmax, int ymax )
1609 if( abs(mx) >= xmax || abs(my) >= ymax )
1612 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1615 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1616 uint8_t *src, int i_src,
1618 int xmax, int ymax )
1620 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1621 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1622 uint8_t *next = &src[i_src];
1623 const int i_src2 = 2*i_src;
1628 uint8_t *rec = &dst[i_dst];
1630 /* We construct with intra method the missing field */
1631 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1633 /* Now we will try to find a match with ME with the other field */
1635 /* ME: A small/partial EPZS
1636 * We search only for small MV (with high motion intra will be perfect */
1637 if( xmax > 4 ) xmax = 4;
1638 if( ymax > 4 ) ymax = 4;
1640 /* Init with NULL Mv */
1642 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1644 /* Try predicted Mv */
1645 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1651 /* Search interger pel (small mv) */
1652 for( i_step = 0; i_step < 4; i_step++ )
1658 for( i = 0; i < 4; i++ )
1660 s = XDeint8x4TestInt( rec, i_src2,
1661 next, i_src2, mvx+dx[i], mvy+dy[i],
1681 if( mvs > 4 && mvs < 256 )
1684 /* XXX: for now only HPEL (too slow) */
1685 for( i_step = 0; i_step < 4; i_step++ )
1691 for( i = 0; i < 8; i++ )
1693 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1694 mvx+dx[i], mvy+dy[i],
1712 uint8_t buffer[8*4];
1713 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1714 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1716 //XDeint8x8Set( dst, i_dst, 0 );
1723 /* Kernel interpolation (1,-5,20,20,-5,1)
1724 * Lose a bit more details+add aliasing than edge interpol but avoid
1727 static inline uint8_t clip1( int a )
1736 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1737 uint8_t *src, int i_src )
1742 for( y = 0; y < 8; y += 2 )
1744 const int i_src2 = i_src*2;
1746 memcpy( dst, src, 8 );
1749 for( x = 0; x < 8; x++ )
1753 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1754 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1755 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1757 dst[x] = clip1( ( pix + 16 ) >> 5 );
1767 /* NxN arbitray size (and then only use pixel in the NxN block)
1769 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1770 int i_height, int i_width )
1777 /* Detect interlacing */
1778 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1781 for( y = 0; y < i_height - 2; y += 2 )
1783 const uint8_t *s = &src[y*i_src];
1784 for( x = 0; x < i_width; x++ )
1786 fr += ssd(s[ x] - s[1*i_src+x]);
1787 ff += ssd(s[ x] - s[2*i_src+x]);
1789 if( ff < fr && fr > i_width / 2 )
1793 return fc < 2 ? false : true;
1796 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1797 uint8_t *src, int i_src,
1798 int i_width, int i_height )
1803 for( y = 0; y < i_height; y += 2 )
1805 memcpy( dst, src, i_width );
1808 if( y < i_height - 2 )
1810 for( x = 0; x < i_width; x++ )
1811 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1815 /* Blend last line */
1816 for( x = 0; x < i_width; x++ )
1817 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1824 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1825 uint8_t *src, int i_src,
1826 int i_width, int i_height )
1831 for( y = 0; y < i_height; y += 2 )
1833 memcpy( dst, src, i_width );
1836 if( y < i_height - 2 )
1838 for( x = 0; x < i_width; x++ )
1839 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1843 /* Blend last line */
1844 for( x = 0; x < i_width; x++ )
1845 dst[x] = (src[x] + src[i_src+x]) >> 1;
1852 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1853 int i_width, int i_height )
1855 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1856 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1858 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1862 static inline int median( int a, int b, int c )
1864 int min = a, max =a;
1875 return a + b + c - min - max;
1881 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1882 uint8_t *src, int i_src,
1883 const int i_mbx, int i_modx )
1887 for( x = 0; x < i_mbx; x++ )
1890 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1892 if( x == 0 || x == i_mbx - 1 )
1893 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1895 XDeint8x8FieldC( dst, i_dst, src, i_src );
1899 XDeint8x8MergeC( dst, i_dst,
1900 &src[0*i_src], 2*i_src,
1901 &src[1*i_src], 2*i_src );
1909 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1911 #ifdef CAN_COMPILE_MMXEXT
1912 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1913 uint8_t *src, int i_src,
1914 const int i_mbx, int i_modx )
1918 /* Reset current line */
1919 for( x = 0; x < i_mbx; x++ )
1922 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1924 if( x == 0 || x == i_mbx - 1 )
1925 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1927 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1931 XDeint8x8MergeMMXEXT( dst, i_dst,
1932 &src[0*i_src], 2*i_src,
1933 &src[1*i_src], 2*i_src );
1941 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1945 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1949 /* Copy image and skip lines */
1950 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1952 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1953 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1955 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1956 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1958 const int i_dst = p_outpic->p[i_plane].i_pitch;
1959 const int i_src = p_pic->p[i_plane].i_pitch;
1963 for( y = 0; y < i_mby; y++ )
1965 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1966 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1968 #ifdef CAN_COMPILE_MMXEXT
1969 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1970 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1973 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1976 /* Last line (C only)*/
1979 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1980 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1982 for( x = 0; x < i_mbx; x++ )
1984 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1991 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1995 #ifdef CAN_COMPILE_MMXEXT
1996 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
2001 /*****************************************************************************
2002 * SendEvents: forward mouse and keyboard events to the parent p_vout
2003 *****************************************************************************/
2004 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
2005 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
2007 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
2008 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
2009 vlc_value_t sentval = newval;
2011 if( !strcmp( psz_var, "mouse-y" ) )
2013 switch( p_vout->p_sys->i_mode )
2015 case DEINTERLACE_MEAN:
2016 case DEINTERLACE_DISCARD:
2022 var_Set( p_vout, psz_var, sentval );
2027 /*****************************************************************************
2028 * FilterCallback: called when changing the deinterlace method on the fly.
2029 *****************************************************************************/
2030 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
2031 vlc_value_t oldval, vlc_value_t newval,
2034 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2035 vout_thread_t * p_vout = (vout_thread_t *)p_this;
2036 int i_old_mode = p_vout->p_sys->i_mode;
2038 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
2040 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
2042 SetFilterMethod( p_vout, newval.psz_string );
2044 switch( p_vout->render.i_chroma )
2046 case VLC_FOURCC('I','4','2','2'):
2047 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2051 case VLC_FOURCC('I','4','2','0'):
2052 case VLC_FOURCC('I','Y','U','V'):
2053 case VLC_FOURCC('Y','V','1','2'):
2054 switch( p_vout->p_sys->i_mode )
2056 case DEINTERLACE_MEAN:
2057 case DEINTERLACE_DISCARD:
2058 if( ( i_old_mode == DEINTERLACE_MEAN )
2059 || ( i_old_mode == DEINTERLACE_DISCARD ) )
2061 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2066 case DEINTERLACE_BOB:
2067 case DEINTERLACE_BLEND:
2068 case DEINTERLACE_LINEAR:
2069 if( ( i_old_mode == DEINTERLACE_BOB )
2070 || ( i_old_mode == DEINTERLACE_BLEND )
2071 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2073 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2084 /* We need to kill the old vout */
2086 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2088 vlc_object_detach( p_vout->p_sys->p_vout );
2089 vout_Destroy( p_vout->p_sys->p_vout );
2091 /* Try to open a new video output */
2092 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2094 if( p_vout->p_sys->p_vout == NULL )
2096 /* Everything failed */
2097 msg_Err( p_vout, "cannot open vout, aborting" );
2099 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2100 return VLC_EGENERIC;
2103 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2105 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2109 /*****************************************************************************
2110 * SendEventsToChild: forward events to the child/children vout
2111 *****************************************************************************/
2112 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2113 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2115 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2116 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2117 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2122 /*****************************************************************************
2123 * video filter2 functions
2124 *****************************************************************************/
2125 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2127 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2128 picture_t *p_pic_dst;
2130 /* Request output picture */
2131 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
2132 if( p_pic_dst == NULL )
2134 msg_Warn( p_filter, "can't get output picture" );
2138 switch( p_vout->p_sys->i_mode )
2140 case DEINTERLACE_DISCARD:
2142 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2144 msg_Err( p_vout, "discarding lines is not supported yet" );
2145 p_pic_dst->pf_release( p_pic_dst );
2149 case DEINTERLACE_BOB:
2151 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2152 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2156 case DEINTERLACE_LINEAR:
2158 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2159 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2161 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2162 p_pic_dst->pf_release( p_pic_dst );
2166 case DEINTERLACE_MEAN:
2167 RenderMean( p_vout, p_pic_dst, p_pic );
2170 case DEINTERLACE_BLEND:
2171 RenderBlend( p_vout, p_pic_dst, p_pic );
2175 RenderX( p_pic_dst, p_pic );
2179 p_pic_dst->date = p_pic->date;
2180 p_pic_dst->b_force = p_pic->b_force;
2181 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2182 p_pic_dst->b_progressive = true;
2183 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2185 p_pic->pf_release( p_pic );
2189 /*****************************************************************************
2191 *****************************************************************************/
2192 static int OpenFilter( vlc_object_t *p_this )
2194 filter_t *p_filter = (filter_t*)p_this;
2195 vout_thread_t *p_vout;
2198 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2199 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2200 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2201 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2203 return VLC_EGENERIC;
2206 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2208 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2209 vlc_object_attach( p_vout, p_filter );
2210 p_filter->p_sys = (filter_sys_t *)p_vout;
2211 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2213 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2215 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2216 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2217 var_Set( p_filter, "deinterlace-mode", val );
2219 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2221 vlc_object_detach( p_vout );
2222 vlc_object_release( p_vout );
2223 return VLC_EGENERIC;
2226 p_filter->pf_video_filter = Deinterlace;
2228 msg_Dbg( p_filter, "deinterlacing" );
2233 /*****************************************************************************
2234 * CloseFilter: clean up the filter
2235 *****************************************************************************/
2236 static void CloseFilter( vlc_object_t *p_this )
2238 filter_t *p_filter = (filter_t*)p_this;
2239 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2241 Destroy( VLC_OBJECT(p_vout) );
2242 vlc_object_detach( p_vout );
2243 vlc_object_release( p_vout );