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_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 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
599 p_out += p_outpic->p[i_plane].i_pitch;
600 p_in += 2 * p_pic->p[i_plane].i_pitch;
604 case VLC_FOURCC('I','4','2','2'):
606 i_increment = 2 * p_pic->p[i_plane].i_pitch;
608 if( i_plane == Y_PLANE )
610 for( ; p_out < p_out_end ; )
612 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
613 p_out += p_outpic->p[i_plane].i_pitch;
614 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
615 p_out += p_outpic->p[i_plane].i_pitch;
621 for( ; p_out < p_out_end ; )
623 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
624 p_out += p_outpic->p[i_plane].i_pitch;
636 /*****************************************************************************
637 * RenderBob: renders a BOB picture - simple copy
638 *****************************************************************************/
639 static void RenderBob( vout_thread_t *p_vout,
640 picture_t *p_outpic, picture_t *p_pic, int i_field )
644 /* Copy image and skip lines */
645 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
647 uint8_t *p_in, *p_out_end, *p_out;
649 p_in = p_pic->p[i_plane].p_pixels;
650 p_out = p_outpic->p[i_plane].p_pixels;
651 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
652 * p_outpic->p[i_plane].i_visible_lines;
654 switch( p_vout->render.i_chroma )
656 case VLC_FOURCC('I','4','2','0'):
657 case VLC_FOURCC('I','Y','U','V'):
658 case VLC_FOURCC('Y','V','1','2'):
659 /* For BOTTOM field we need to add the first line */
662 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
663 p_in += p_pic->p[i_plane].i_pitch;
664 p_out += p_outpic->p[i_plane].i_pitch;
667 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
669 for( ; p_out < p_out_end ; )
671 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
673 p_out += p_outpic->p[i_plane].i_pitch;
675 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
677 p_in += 2 * p_pic->p[i_plane].i_pitch;
678 p_out += p_outpic->p[i_plane].i_pitch;
681 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
683 /* For TOP field we need to add the last line */
686 p_in += p_pic->p[i_plane].i_pitch;
687 p_out += p_outpic->p[i_plane].i_pitch;
688 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
692 case VLC_FOURCC('I','4','2','2'):
693 /* For BOTTOM field we need to add the first line */
696 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
697 p_in += p_pic->p[i_plane].i_pitch;
698 p_out += p_outpic->p[i_plane].i_pitch;
701 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
703 if( i_plane == Y_PLANE )
705 for( ; p_out < p_out_end ; )
707 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
709 p_out += p_outpic->p[i_plane].i_pitch;
711 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
713 p_in += 2 * p_pic->p[i_plane].i_pitch;
714 p_out += p_outpic->p[i_plane].i_pitch;
719 for( ; p_out < p_out_end ; )
721 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
723 p_out += p_outpic->p[i_plane].i_pitch;
724 p_in += 2 * p_pic->p[i_plane].i_pitch;
728 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
730 /* For TOP field we need to add the last line */
733 p_in += p_pic->p[i_plane].i_pitch;
734 p_out += p_outpic->p[i_plane].i_pitch;
735 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
742 #define Merge p_vout->p_sys->pf_merge
743 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
745 /*****************************************************************************
746 * RenderLinear: BOB with linear interpolation
747 *****************************************************************************/
748 static void RenderLinear( vout_thread_t *p_vout,
749 picture_t *p_outpic, picture_t *p_pic, int i_field )
753 /* Copy image and skip lines */
754 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
756 uint8_t *p_in, *p_out_end, *p_out;
758 p_in = p_pic->p[i_plane].p_pixels;
759 p_out = p_outpic->p[i_plane].p_pixels;
760 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
761 * p_outpic->p[i_plane].i_visible_lines;
763 /* For BOTTOM field we need to add the first line */
766 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
767 p_in += p_pic->p[i_plane].i_pitch;
768 p_out += p_outpic->p[i_plane].i_pitch;
771 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
773 for( ; p_out < p_out_end ; )
775 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
777 p_out += p_outpic->p[i_plane].i_pitch;
779 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
780 p_pic->p[i_plane].i_pitch );
782 p_in += 2 * p_pic->p[i_plane].i_pitch;
783 p_out += p_outpic->p[i_plane].i_pitch;
786 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
788 /* For TOP field we need to add the last line */
791 p_in += p_pic->p[i_plane].i_pitch;
792 p_out += p_outpic->p[i_plane].i_pitch;
793 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
799 static void RenderMean( vout_thread_t *p_vout,
800 picture_t *p_outpic, picture_t *p_pic )
804 /* Copy image and skip lines */
805 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
807 uint8_t *p_in, *p_out_end, *p_out;
809 p_in = p_pic->p[i_plane].p_pixels;
811 p_out = p_outpic->p[i_plane].p_pixels;
812 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
813 * p_outpic->p[i_plane].i_visible_lines;
815 /* All lines: mean value */
816 for( ; p_out < p_out_end ; )
818 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
819 p_pic->p[i_plane].i_pitch );
821 p_out += p_outpic->p[i_plane].i_pitch;
822 p_in += 2 * p_pic->p[i_plane].i_pitch;
828 static void RenderBlend( vout_thread_t *p_vout,
829 picture_t *p_outpic, picture_t *p_pic )
833 /* Copy image and skip lines */
834 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
836 uint8_t *p_in, *p_out_end, *p_out;
838 p_in = p_pic->p[i_plane].p_pixels;
840 p_out = p_outpic->p[i_plane].p_pixels;
841 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
842 * p_outpic->p[i_plane].i_visible_lines;
844 switch( p_vout->render.i_chroma )
846 case VLC_FOURCC('I','4','2','0'):
847 case VLC_FOURCC('I','Y','U','V'):
848 case VLC_FOURCC('Y','V','1','2'):
849 /* First line: simple copy */
850 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
851 p_out += p_outpic->p[i_plane].i_pitch;
853 /* Remaining lines: mean value */
854 for( ; p_out < p_out_end ; )
856 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
857 p_pic->p[i_plane].i_pitch );
859 p_out += p_outpic->p[i_plane].i_pitch;
860 p_in += p_pic->p[i_plane].i_pitch;
864 case VLC_FOURCC('I','4','2','2'):
865 /* First line: simple copy */
866 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
867 p_out += p_outpic->p[i_plane].i_pitch;
869 /* Remaining lines: mean value */
870 if( i_plane == Y_PLANE )
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;
884 for( ; p_out < p_out_end ; )
886 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
887 p_pic->p[i_plane].i_pitch );
889 p_out += p_outpic->p[i_plane].i_pitch;
890 p_in += 2*p_pic->p[i_plane].i_pitch;
901 static void MergeGeneric( void *_p_dest, const void *_p_s1,
902 const void *_p_s2, size_t i_bytes )
904 uint8_t* p_dest = (uint8_t*)_p_dest;
905 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
906 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
907 uint8_t* p_end = p_dest + i_bytes - 8;
909 while( p_dest < p_end )
911 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
912 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
913 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
914 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
915 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
916 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
917 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
918 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
923 while( p_dest < p_end )
925 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
929 #if defined(CAN_COMPILE_MMXEXT)
930 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
933 uint8_t* p_dest = (uint8_t*)_p_dest;
934 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
935 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
936 uint8_t* p_end = p_dest + i_bytes - 8;
937 while( p_dest < p_end )
939 __asm__ __volatile__( "movq %2,%%mm1;"
941 "movq %%mm1, %0" :"=m" (*p_dest):
951 while( p_dest < p_end )
953 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
958 #if defined(CAN_COMPILE_3DNOW)
959 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
962 uint8_t* p_dest = (uint8_t*)_p_dest;
963 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
964 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
965 uint8_t* p_end = p_dest + i_bytes - 8;
966 while( p_dest < p_end )
968 __asm__ __volatile__( "movq %2,%%mm1;"
970 "movq %%mm1, %0" :"=m" (*p_dest):
980 while( p_dest < p_end )
982 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
987 #if defined(CAN_COMPILE_SSE)
988 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
991 uint8_t* p_dest = (uint8_t*)_p_dest;
992 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
993 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
995 while( (uintptr_t)p_s1 % 16 )
997 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
999 p_end = p_dest + i_bytes - 16;
1000 while( p_dest < p_end )
1002 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1004 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1014 while( p_dest < p_end )
1016 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1021 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1022 static void EndMMX( void )
1024 __asm__ __volatile__( "emms" :: );
1028 #if defined(CAN_COMPILE_3DNOW)
1029 static void End3DNow( void )
1031 __asm__ __volatile__( "femms" :: );
1035 #ifdef CAN_COMPILE_C_ALTIVEC
1036 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1037 const void *_p_s2, size_t i_bytes )
1039 uint8_t *p_dest = (uint8_t *)_p_dest;
1040 uint8_t *p_s1 = (uint8_t *)_p_s1;
1041 uint8_t *p_s2 = (uint8_t *)_p_s2;
1042 uint8_t *p_end = p_dest + i_bytes - 15;
1044 /* Use C until the first 16-bytes aligned destination pixel */
1045 while( (int)p_dest & 0xF )
1047 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1050 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1052 /* Unaligned source */
1053 vector unsigned char s1v, s2v, destv;
1054 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1055 vector unsigned char perm1v, perm2v;
1057 perm1v = vec_lvsl( 0, p_s1 );
1058 perm2v = vec_lvsl( 0, p_s2 );
1059 s1oldv = vec_ld( 0, p_s1 );
1060 s2oldv = vec_ld( 0, p_s2 );
1062 while( p_dest < p_end )
1064 s1newv = vec_ld( 16, p_s1 );
1065 s2newv = vec_ld( 16, p_s2 );
1066 s1v = vec_perm( s1oldv, s1newv, perm1v );
1067 s2v = vec_perm( s2oldv, s2newv, perm2v );
1070 destv = vec_avg( s1v, s2v );
1071 vec_st( destv, 0, p_dest );
1080 /* Aligned source */
1081 vector unsigned char s1v, s2v, destv;
1083 while( p_dest < p_end )
1085 s1v = vec_ld( 0, p_s1 );
1086 s2v = vec_ld( 0, p_s2 );
1087 destv = vec_avg( s1v, s2v );
1088 vec_st( destv, 0, p_dest );
1098 while( p_dest < p_end )
1100 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1105 /*****************************************************************************
1106 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1107 * and apply a process to recreate the bottom field :
1108 * If a 8x8 block is classified as :
1109 * - progressive: it applies a small blend (1,6,1)
1111 * * in the MMX version: we do a ME between the 2 fields, if there is a
1112 * good match we use MC to recreate the bottom field (with a small
1114 * * otherwise: it recreates the bottom field by an edge oriented
1116 *****************************************************************************/
1118 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1119 * XXX: It need to access to 8x10
1120 * We use more than 8 lines to help with scrolling (text)
1121 * (and because XDeint8x8Frame use line 9)
1122 * XXX: smooth/uniform area with noise detection doesn't works well
1123 * but it's not really a problem because they don't have much details anyway
1125 static inline int ssd( int a ) { return a*a; }
1126 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1132 /* Detect interlacing */
1134 for( y = 0; y < 7; y += 2 )
1137 for( x = 0; x < 8; x++ )
1139 fr += ssd(src[ x] - src[1*i_src+x]) +
1140 ssd(src[i_src+x] - src[2*i_src+x]);
1141 ff += ssd(src[ x] - src[2*i_src+x]) +
1142 ssd(src[i_src+x] - src[3*i_src+x]);
1144 if( ff < 6*fr/8 && fr > 32 )
1150 return fc < 1 ? false : true;
1152 #ifdef CAN_COMPILE_MMXEXT
1153 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1160 /* Detect interlacing */
1162 pxor_r2r( mm7, mm7 );
1163 for( y = 0; y < 9; y += 2 )
1166 pxor_r2r( mm5, mm5 );
1167 pxor_r2r( mm6, mm6 );
1168 for( x = 0; x < 8; x+=4 )
1170 movd_m2r( src[ x], mm0 );
1171 movd_m2r( src[1*i_src+x], mm1 );
1172 movd_m2r( src[2*i_src+x], mm2 );
1173 movd_m2r( src[3*i_src+x], mm3 );
1175 punpcklbw_r2r( mm7, mm0 );
1176 punpcklbw_r2r( mm7, mm1 );
1177 punpcklbw_r2r( mm7, mm2 );
1178 punpcklbw_r2r( mm7, mm3 );
1180 movq_r2r( mm0, mm4 );
1182 psubw_r2r( mm1, mm0 );
1183 psubw_r2r( mm2, mm4 );
1185 psubw_r2r( mm1, mm2 );
1186 psubw_r2r( mm1, mm3 );
1188 pmaddwd_r2r( mm0, mm0 );
1189 pmaddwd_r2r( mm4, mm4 );
1190 pmaddwd_r2r( mm2, mm2 );
1191 pmaddwd_r2r( mm3, mm3 );
1192 paddd_r2r( mm0, mm2 );
1193 paddd_r2r( mm4, mm3 );
1194 paddd_r2r( mm2, mm5 );
1195 paddd_r2r( mm3, mm6 );
1198 movq_r2r( mm5, mm0 );
1199 psrlq_i2r( 32, mm0 );
1200 paddd_r2r( mm0, mm5 );
1201 movd_r2m( mm5, fr );
1203 movq_r2r( mm6, mm0 );
1204 psrlq_i2r( 32, mm0 );
1205 paddd_r2r( mm0, mm6 );
1206 movd_r2m( mm6, ff );
1208 if( ff < 6*fr/8 && fr > 32 )
1217 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1218 * This won't destroy details, and help if there is a bit of interlacing.
1219 * (It helps with paning to avoid flickers)
1223 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1224 uint8_t *src, int i_src )
1229 for( y = 0; y < 8; y += 2 )
1231 memcpy( dst, src, 8 );
1234 for( x = 0; x < 8; x++ )
1235 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1241 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1242 uint8_t *src1, int i_src1,
1243 uint8_t *src2, int i_src2 )
1248 for( y = 0; y < 8; y += 2 )
1250 memcpy( dst, src1, 8 );
1253 for( x = 0; x < 8; x++ )
1254 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1262 #ifdef CAN_COMPILE_MMXEXT
1263 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1264 uint8_t *src1, int i_src1,
1265 uint8_t *src2, int i_src2 )
1267 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1271 pxor_r2r( mm7, mm7 );
1272 for( y = 0; y < 8; y += 2 )
1274 for( x = 0; x < 8; x +=4 )
1276 movd_m2r( src1[x], mm0 );
1277 movd_r2m( mm0, dst[x] );
1279 movd_m2r( src2[x], mm1 );
1280 movd_m2r( src1[i_src1+x], mm2 );
1282 punpcklbw_r2r( mm7, mm0 );
1283 punpcklbw_r2r( mm7, mm1 );
1284 punpcklbw_r2r( mm7, mm2 );
1285 paddw_r2r( mm1, mm1 );
1286 movq_r2r( mm1, mm3 );
1287 paddw_r2r( mm3, mm3 );
1288 paddw_r2r( mm2, mm0 );
1289 paddw_r2r( mm3, mm1 );
1290 paddw_m2r( m_4, mm1 );
1291 paddw_r2r( mm1, mm0 );
1292 psraw_i2r( 3, mm0 );
1293 packuswb_r2r( mm7, mm0 );
1294 movd_r2m( mm0, dst[i_dst+x] );
1305 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1308 for( y = 0; y < 8; y++ )
1309 memset( &dst[y*i_dst], v, 8 );
1312 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1315 * TODO: a better one for the inner part.
1317 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1318 uint8_t *src, int i_src )
1323 for( y = 0; y < 8; y += 2 )
1325 memcpy( dst, src, 8 );
1328 for( x = 0; x < 8; x++ )
1329 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1334 #ifdef CAN_COMPILE_MMXEXT
1335 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1336 uint8_t *src, int i_src )
1341 for( y = 0; y < 8; y += 2 )
1343 movq_m2r( src[0], mm0 );
1344 movq_r2m( mm0, dst[0] );
1347 movq_m2r( src[2*i_src], mm1 );
1348 pavgb_r2r( mm1, mm0 );
1350 movq_r2m( mm0, dst[0] );
1358 /* XDeint8x8Field: Edge oriented interpolation
1359 * (Need -4 and +5 pixels H, +1 line)
1361 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1362 uint8_t *src, int i_src )
1367 for( y = 0; y < 8; y += 2 )
1369 memcpy( dst, src, 8 );
1372 for( x = 0; x < 8; x++ )
1374 uint8_t *src2 = &src[2*i_src];
1375 /* I use 8 pixels just to match the MMX version, but it's overkill
1376 * 5 would be enough (less isn't good) */
1377 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1378 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1379 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1380 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1382 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1383 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1384 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1385 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1387 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1388 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1389 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1390 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1392 if( c0 < c1 && c1 <= c2 )
1393 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1394 else if( c2 < c1 && c1 <= c0 )
1395 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1397 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1404 #ifdef CAN_COMPILE_MMXEXT
1405 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1406 uint8_t *src, int i_src )
1411 for( y = 0; y < 8; y += 2 )
1413 memcpy( dst, src, 8 );
1416 for( x = 0; x < 8; x++ )
1418 uint8_t *src2 = &src[2*i_src];
1421 movq_m2r( src[x-2], mm0 );
1422 movq_m2r( src[x-3], mm1 );
1423 movq_m2r( src[x-4], mm2 );
1425 psadbw_m2r( src2[x-4], mm0 );
1426 psadbw_m2r( src2[x-3], mm1 );
1427 psadbw_m2r( src2[x-2], mm2 );
1429 movd_r2m( mm0, c2 );
1430 movd_r2m( mm1, c1 );
1431 movd_r2m( mm2, c0 );
1433 if( c0 < c1 && c1 <= c2 )
1434 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1435 else if( c2 < c1 && c1 <= c0 )
1436 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1438 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1448 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1449 uint8_t *pix2, int i_pix2 )
1454 for( y = 0; y < 8; y++ )
1455 for( x = 0; x < 8; x++ )
1456 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1460 #ifdef CAN_COMPILE_MMXEXT
1461 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1462 uint8_t *pix2, int i_pix2 )
1467 pxor_r2r( mm7, mm7 );
1468 pxor_r2r( mm6, mm6 );
1470 for( y = 0; y < 8; y++ )
1472 movq_m2r( pix1[0], mm0 );
1473 movq_m2r( pix2[0], mm1 );
1475 movq_r2r( mm0, mm2 );
1476 movq_r2r( mm1, mm3 );
1478 punpcklbw_r2r( mm7, mm0 );
1479 punpckhbw_r2r( mm7, mm2 );
1480 punpcklbw_r2r( mm7, mm1 );
1481 punpckhbw_r2r( mm7, mm3 );
1483 psubw_r2r( mm1, mm0 );
1484 psubw_r2r( mm3, mm2 );
1486 pmaddwd_r2r( mm0, mm0 );
1487 pmaddwd_r2r( mm2, mm2 );
1489 paddd_r2r( mm2, mm0 );
1490 paddd_r2r( mm0, mm6 );
1496 movq_r2r( mm6, mm7 );
1497 psrlq_i2r( 32, mm7 );
1498 paddd_r2r( mm6, mm7 );
1507 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1508 #ifdef CAN_COMPILE_MMXEXT
1511 * TODO: mmx version (easier in sse2)
1513 static inline void XDeintMC( uint8_t *dst, int i_dst,
1514 uint8_t *src, int i_src,
1516 int i_width, int i_height )
1518 const int d4x = mvx&0x03;
1519 const int d4y = mvy&0x03;
1521 const int cA = (4-d4x)*(4-d4y);
1522 const int cB = d4x *(4-d4y);
1523 const int cC = (4-d4x)*d4y;
1524 const int cD = d4x *d4y;
1530 src += (mvy >> 2) * i_src + (mvx >> 2);
1533 for( y = 0; y < i_height; y++ )
1535 for( x = 0; x < i_width; x++ )
1537 dst[x] = ( cA*src[x] + cB*src[x+1] +
1538 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1546 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1547 uint8_t *pix2, int i_pix2 )
1551 movq_m2r( pix1[0*i_pix1], mm0 );
1552 movq_m2r( pix1[1*i_pix1], mm1 );
1554 psadbw_m2r( pix2[0*i_pix2], mm0 );
1555 psadbw_m2r( pix2[1*i_pix2], mm1 );
1557 movq_m2r( pix1[2*i_pix1], mm2 );
1558 movq_m2r( pix1[3*i_pix1], mm3 );
1559 psadbw_m2r( pix2[2*i_pix2], mm2 );
1560 psadbw_m2r( pix2[3*i_pix2], mm3 );
1562 paddd_r2r( mm1, mm0 );
1563 paddd_r2r( mm3, mm2 );
1564 paddd_r2r( mm2, mm0 );
1570 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1571 uint8_t *ref, int i_stride,
1573 int xmax, int ymax )
1575 uint8_t buffer[8*4];
1577 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1580 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1581 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1583 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1584 uint8_t *ref, int i_stride,
1586 int xmax, int ymax )
1588 if( abs(mx) >= xmax || abs(my) >= ymax )
1591 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1594 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1595 uint8_t *src, int i_src,
1597 int xmax, int ymax )
1599 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1600 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1601 uint8_t *next = &src[i_src];
1602 const int i_src2 = 2*i_src;
1607 uint8_t *rec = &dst[i_dst];
1609 /* We construct with intra method the missing field */
1610 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1612 /* Now we will try to find a match with ME with the other field */
1614 /* ME: A small/partial EPZS
1615 * We search only for small MV (with high motion intra will be perfect */
1616 if( xmax > 4 ) xmax = 4;
1617 if( ymax > 4 ) ymax = 4;
1619 /* Init with NULL Mv */
1621 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1623 /* Try predicted Mv */
1624 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1630 /* Search interger pel (small mv) */
1631 for( i_step = 0; i_step < 4; i_step++ )
1637 for( i = 0; i < 4; i++ )
1639 s = XDeint8x4TestInt( rec, i_src2,
1640 next, i_src2, mvx+dx[i], mvy+dy[i],
1660 if( mvs > 4 && mvs < 256 )
1663 /* XXX: for now only HPEL (too slow) */
1664 for( i_step = 0; i_step < 4; i_step++ )
1670 for( i = 0; i < 8; i++ )
1672 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1673 mvx+dx[i], mvy+dy[i],
1691 uint8_t buffer[8*4];
1692 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1693 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1695 //XDeint8x8Set( dst, i_dst, 0 );
1702 /* Kernel interpolation (1,-5,20,20,-5,1)
1703 * Lose a bit more details+add aliasing than edge interpol but avoid
1706 static inline uint8_t clip1( int a )
1715 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1716 uint8_t *src, int i_src )
1721 for( y = 0; y < 8; y += 2 )
1723 const int i_src2 = i_src*2;
1725 memcpy( dst, src, 8 );
1728 for( x = 0; x < 8; x++ )
1732 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1733 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1734 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1736 dst[x] = clip1( ( pix + 16 ) >> 5 );
1746 /* NxN arbitray size (and then only use pixel in the NxN block)
1748 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1749 int i_height, int i_width )
1756 /* Detect interlacing */
1757 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1760 for( y = 0; y < i_height - 2; y += 2 )
1762 const uint8_t *s = &src[y*i_src];
1763 for( x = 0; x < i_width; x++ )
1765 fr += ssd(s[ x] - s[1*i_src+x]);
1766 ff += ssd(s[ x] - s[2*i_src+x]);
1768 if( ff < fr && fr > i_width / 2 )
1772 return fc < 2 ? false : true;
1775 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1776 uint8_t *src, int i_src,
1777 int i_width, int i_height )
1782 for( y = 0; y < i_height; y += 2 )
1784 memcpy( dst, src, i_width );
1787 if( y < i_height - 2 )
1789 for( x = 0; x < i_width; x++ )
1790 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1794 /* Blend last line */
1795 for( x = 0; x < i_width; x++ )
1796 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1803 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1804 uint8_t *src, int i_src,
1805 int i_width, int i_height )
1810 for( y = 0; y < i_height; y += 2 )
1812 memcpy( dst, src, i_width );
1815 if( y < i_height - 2 )
1817 for( x = 0; x < i_width; x++ )
1818 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1822 /* Blend last line */
1823 for( x = 0; x < i_width; x++ )
1824 dst[x] = (src[x] + src[i_src+x]) >> 1;
1831 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1832 int i_width, int i_height )
1834 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1835 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1837 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1841 static inline int median( int a, int b, int c )
1843 int min = a, max =a;
1854 return a + b + c - min - max;
1860 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1861 uint8_t *src, int i_src,
1862 const int i_mbx, int i_modx )
1866 for( x = 0; x < i_mbx; x++ )
1869 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1871 if( x == 0 || x == i_mbx - 1 )
1872 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1874 XDeint8x8FieldC( dst, i_dst, src, i_src );
1878 XDeint8x8MergeC( dst, i_dst,
1879 &src[0*i_src], 2*i_src,
1880 &src[1*i_src], 2*i_src );
1888 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1890 #ifdef CAN_COMPILE_MMXEXT
1891 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1892 uint8_t *src, int i_src,
1893 const int i_mbx, int i_modx )
1897 /* Reset current line */
1898 for( x = 0; x < i_mbx; x++ )
1901 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1903 if( x == 0 || x == i_mbx - 1 )
1904 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1906 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1910 XDeint8x8MergeMMXEXT( dst, i_dst,
1911 &src[0*i_src], 2*i_src,
1912 &src[1*i_src], 2*i_src );
1920 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1924 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1928 /* Copy image and skip lines */
1929 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1931 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1932 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1934 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1935 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1937 const int i_dst = p_outpic->p[i_plane].i_pitch;
1938 const int i_src = p_pic->p[i_plane].i_pitch;
1942 for( y = 0; y < i_mby; y++ )
1944 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1945 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1947 #ifdef CAN_COMPILE_MMXEXT
1948 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1949 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1952 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1955 /* Last line (C only)*/
1958 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1959 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1961 for( x = 0; x < i_mbx; x++ )
1963 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1970 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1974 #ifdef CAN_COMPILE_MMXEXT
1975 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1980 /*****************************************************************************
1981 * SendEvents: forward mouse and keyboard events to the parent p_vout
1982 *****************************************************************************/
1983 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1984 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1986 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
1987 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1988 vlc_value_t sentval = newval;
1990 if( !strcmp( psz_var, "mouse-y" ) )
1992 switch( p_vout->p_sys->i_mode )
1994 case DEINTERLACE_MEAN:
1995 case DEINTERLACE_DISCARD:
2001 var_Set( p_vout, psz_var, sentval );
2006 /*****************************************************************************
2007 * FilterCallback: called when changing the deinterlace method on the fly.
2008 *****************************************************************************/
2009 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
2010 vlc_value_t oldval, vlc_value_t newval,
2013 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2014 vout_thread_t * p_vout = (vout_thread_t *)p_this;
2015 int i_old_mode = p_vout->p_sys->i_mode;
2017 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
2019 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
2021 SetFilterMethod( p_vout, newval.psz_string );
2023 switch( p_vout->render.i_chroma )
2025 case VLC_FOURCC('I','4','2','2'):
2026 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2030 case VLC_FOURCC('I','4','2','0'):
2031 case VLC_FOURCC('I','Y','U','V'):
2032 case VLC_FOURCC('Y','V','1','2'):
2033 switch( p_vout->p_sys->i_mode )
2035 case DEINTERLACE_MEAN:
2036 case DEINTERLACE_DISCARD:
2037 if( ( i_old_mode == DEINTERLACE_MEAN )
2038 || ( i_old_mode == DEINTERLACE_DISCARD ) )
2040 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2045 case DEINTERLACE_BOB:
2046 case DEINTERLACE_BLEND:
2047 case DEINTERLACE_LINEAR:
2048 if( ( i_old_mode == DEINTERLACE_BOB )
2049 || ( i_old_mode == DEINTERLACE_BLEND )
2050 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2052 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2063 /* We need to kill the old vout */
2065 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2067 vlc_object_detach( p_vout->p_sys->p_vout );
2068 vout_Destroy( p_vout->p_sys->p_vout );
2070 /* Try to open a new video output */
2071 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2073 if( p_vout->p_sys->p_vout == NULL )
2075 /* Everything failed */
2076 msg_Err( p_vout, "cannot open vout, aborting" );
2078 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2079 return VLC_EGENERIC;
2082 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2084 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2088 /*****************************************************************************
2089 * SendEventsToChild: forward events to the child/children vout
2090 *****************************************************************************/
2091 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2092 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2094 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2095 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2096 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2101 /*****************************************************************************
2102 * video filter2 functions
2103 *****************************************************************************/
2104 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2106 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2107 picture_t *p_pic_dst;
2109 /* Request output picture */
2110 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
2111 if( p_pic_dst == NULL )
2113 msg_Warn( p_filter, "can't get output picture" );
2117 switch( p_vout->p_sys->i_mode )
2119 case DEINTERLACE_DISCARD:
2121 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2123 msg_Err( p_vout, "discarding lines is not supported yet" );
2124 p_pic_dst->pf_release( p_pic_dst );
2128 case DEINTERLACE_BOB:
2130 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2131 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2135 case DEINTERLACE_LINEAR:
2137 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2138 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2140 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2141 p_pic_dst->pf_release( p_pic_dst );
2145 case DEINTERLACE_MEAN:
2146 RenderMean( p_vout, p_pic_dst, p_pic );
2149 case DEINTERLACE_BLEND:
2150 RenderBlend( p_vout, p_pic_dst, p_pic );
2154 RenderX( p_pic_dst, p_pic );
2158 p_pic_dst->date = p_pic->date;
2159 p_pic_dst->b_force = p_pic->b_force;
2160 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2161 p_pic_dst->b_progressive = true;
2162 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2164 p_pic->pf_release( p_pic );
2168 /*****************************************************************************
2170 *****************************************************************************/
2171 static int OpenFilter( vlc_object_t *p_this )
2173 filter_t *p_filter = (filter_t*)p_this;
2174 vout_thread_t *p_vout;
2177 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2178 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2179 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2180 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2182 return VLC_EGENERIC;
2185 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2187 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2188 vlc_object_attach( p_vout, p_filter );
2189 p_filter->p_sys = (filter_sys_t *)p_vout;
2190 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2192 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2194 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2195 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2196 var_Set( p_filter, "deinterlace-mode", val );
2198 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2200 vlc_object_detach( p_vout );
2201 vlc_object_release( p_vout );
2202 return VLC_EGENERIC;
2205 p_filter->pf_video_filter = Deinterlace;
2207 msg_Dbg( p_filter, "deinterlacing" );
2212 /*****************************************************************************
2213 * CloseFilter: clean up the filter
2214 *****************************************************************************/
2215 static void CloseFilter( vlc_object_t *p_this )
2217 filter_t *p_filter = (filter_t*)p_this;
2218 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2220 Destroy( VLC_OBJECT(p_vout) );
2221 vlc_object_detach( p_vout );
2222 vlc_object_release( p_vout );