1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
7 * Author: Sam Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
34 #include "vlc_filter.h"
40 #ifdef CAN_COMPILE_MMXEXT
44 #include "filter_common.h"
46 #define DEINTERLACE_DISCARD 1
47 #define DEINTERLACE_MEAN 2
48 #define DEINTERLACE_BLEND 3
49 #define DEINTERLACE_BOB 4
50 #define DEINTERLACE_LINEAR 5
51 #define DEINTERLACE_X 6
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Create ( vlc_object_t * );
57 static void Destroy ( vlc_object_t * );
59 static int Init ( vout_thread_t * );
60 static void End ( vout_thread_t * );
61 static void Render ( vout_thread_t *, picture_t * );
63 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
64 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
65 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
66 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
67 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
68 static void RenderX ( vout_thread_t *, picture_t *, picture_t * );
70 static void MergeGeneric ( void *, const void *, const void *, size_t );
71 #if defined(CAN_COMPILE_C_ALTIVEC)
72 static void MergeAltivec ( void *, const void *, const void *, size_t );
74 #if defined(CAN_COMPILE_MMXEXT)
75 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
77 #if defined(CAN_COMPILE_3DNOW)
78 static void Merge3DNow ( void *, const void *, const void *, size_t );
80 #if defined(CAN_COMPILE_SSE)
81 static void MergeSSE2 ( void *, const void *, const void *, size_t );
83 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
84 static void EndMMX ( void );
86 #if defined(CAN_COMPILE_3DNOW)
87 static void End3DNow ( void );
90 static int SendEvents ( vlc_object_t *, char const *,
91 vlc_value_t, vlc_value_t, void * );
93 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
94 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
96 static int OpenFilter( vlc_object_t *p_this );
97 static void CloseFilter( vlc_object_t *p_this );
99 /*****************************************************************************
100 * Callback prototypes
101 *****************************************************************************/
102 static int FilterCallback ( vlc_object_t *, char const *,
103 vlc_value_t, vlc_value_t, void * );
105 /*****************************************************************************
107 *****************************************************************************/
108 #define MODE_TEXT N_("Deinterlace mode")
109 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
111 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
112 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
114 #define FILTER_CFG_PREFIX "sout-deinterlace-"
116 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", "x" };
117 static char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
118 N_("Bob"), N_("Linear"), "X" };
121 set_description( _("Deinterlacing video filter") );
122 set_shortname( N_("Deinterlace" ));
123 set_capability( "video filter", 0 );
124 set_category( CAT_VIDEO );
125 set_subcategory( SUBCAT_VIDEO_VFILTER );
127 set_section( N_("Display"),NULL);
128 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
129 MODE_LONGTEXT, VLC_FALSE );
130 change_string_list( mode_list, mode_list_text, 0 );
132 add_shortcut( "deinterlace" );
133 set_callbacks( Create, Destroy );
136 set_capability( "video filter2", 0 );
137 set_section( N_("Streaming"),NULL);
138 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
139 SOUT_MODE_LONGTEXT, VLC_FALSE );
140 change_string_list( mode_list, mode_list_text, 0 );
141 set_callbacks( OpenFilter, CloseFilter );
144 static const char *ppsz_filter_options[] = {
148 /*****************************************************************************
149 * vout_sys_t: Deinterlace video output method descriptor
150 *****************************************************************************
151 * This structure is part of the video output thread descriptor.
152 * It describes the Deinterlace specific properties of an output thread.
153 *****************************************************************************/
156 int i_mode; /* Deinterlace mode */
157 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
162 vout_thread_t *p_vout;
164 vlc_mutex_t filter_lock;
166 void (*pf_merge) ( void *, const void *, const void *, size_t );
167 void (*pf_end_merge) ( void );
170 /*****************************************************************************
171 * Control: control facility for the vout (forwards to child vout)
172 *****************************************************************************/
173 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
175 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
178 /*****************************************************************************
179 * Create: allocates Deinterlace video thread output method
180 *****************************************************************************
181 * This function allocates and initializes a Deinterlace vout method.
182 *****************************************************************************/
183 static int Create( vlc_object_t *p_this )
185 vout_thread_t *p_vout = (vout_thread_t *)p_this;
188 /* Allocate structure */
189 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
190 if( p_vout->p_sys == NULL )
192 msg_Err( p_vout, "out of memory" );
196 p_vout->pf_init = Init;
197 p_vout->pf_end = End;
198 p_vout->pf_manage = NULL;
199 p_vout->pf_render = Render;
200 p_vout->pf_display = NULL;
201 p_vout->pf_control = Control;
203 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
204 p_vout->p_sys->b_double_rate = VLC_FALSE;
205 p_vout->p_sys->last_date = 0;
206 p_vout->p_sys->p_vout = 0;
207 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
209 #if defined(CAN_COMPILE_C_ALTIVEC)
210 if( p_vout->p_libvlc_global->i_cpu & CPU_CAPABILITY_ALTIVEC )
212 p_vout->p_sys->pf_merge = MergeAltivec;
213 p_vout->p_sys->pf_end_merge = NULL;
217 #if defined(CAN_COMPILE_SSE)
218 if( p_vout->p_libvlc_global->i_cpu & CPU_CAPABILITY_SSE2 )
220 p_vout->p_sys->pf_merge = MergeSSE2;
221 p_vout->p_sys->pf_end_merge = EndMMX;
225 #if defined(CAN_COMPILE_MMXEXT)
226 if( p_vout->p_libvlc_global->i_cpu & CPU_CAPABILITY_MMXEXT )
228 p_vout->p_sys->pf_merge = MergeMMXEXT;
229 p_vout->p_sys->pf_end_merge = EndMMX;
233 #if defined(CAN_COMPILE_3DNOW)
234 if( p_vout->p_libvlc_global->i_cpu & CPU_CAPABILITY_3DNOW )
236 p_vout->p_sys->pf_merge = Merge3DNow;
237 p_vout->p_sys->pf_end_merge = End3DNow;
242 p_vout->p_sys->pf_merge = MergeGeneric;
243 p_vout->p_sys->pf_end_merge = NULL;
246 /* Look what method was requested */
247 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
248 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
250 if( val.psz_string == NULL )
252 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
253 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
255 val.psz_string = strdup( "discard" );
258 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
260 SetFilterMethod( p_vout, val.psz_string );
262 free( val.psz_string );
267 /*****************************************************************************
268 * SetFilterMethod: setup the deinterlace method to use.
269 *****************************************************************************/
270 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
272 if( !strcmp( psz_method, "discard" ) )
274 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
275 p_vout->p_sys->b_double_rate = VLC_FALSE;
277 else if( !strcmp( psz_method, "mean" ) )
279 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
280 p_vout->p_sys->b_double_rate = VLC_FALSE;
282 else if( !strcmp( psz_method, "blend" )
283 || !strcmp( psz_method, "average" )
284 || !strcmp( psz_method, "combine-fields" ) )
286 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
287 p_vout->p_sys->b_double_rate = VLC_FALSE;
289 else if( !strcmp( psz_method, "bob" )
290 || !strcmp( psz_method, "progressive-scan" ) )
292 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
293 p_vout->p_sys->b_double_rate = VLC_TRUE;
295 else if( !strcmp( psz_method, "linear" ) )
297 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
298 p_vout->p_sys->b_double_rate = VLC_TRUE;
300 else if( !strcmp( psz_method, "x" ) )
302 p_vout->p_sys->i_mode = DEINTERLACE_X;
303 p_vout->p_sys->b_double_rate = VLC_FALSE;
307 msg_Err( p_vout, "no valid deinterlace mode provided, "
308 "using \"discard\"" );
311 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
314 /*****************************************************************************
315 * Init: initialize Deinterlace video thread output method
316 *****************************************************************************/
317 static int Init( vout_thread_t *p_vout )
322 I_OUTPUTPICTURES = 0;
324 /* Initialize the output structure, full of directbuffers since we want
325 * the decoder to output directly to our structures. */
326 switch( p_vout->render.i_chroma )
328 case VLC_FOURCC('I','4','2','0'):
329 case VLC_FOURCC('I','Y','U','V'):
330 case VLC_FOURCC('Y','V','1','2'):
331 case VLC_FOURCC('I','4','2','2'):
332 p_vout->output.i_chroma = p_vout->render.i_chroma;
333 p_vout->output.i_width = p_vout->render.i_width;
334 p_vout->output.i_height = p_vout->render.i_height;
335 p_vout->output.i_aspect = p_vout->render.i_aspect;
336 p_vout->fmt_out = p_vout->fmt_in;
340 return VLC_EGENERIC; /* unknown chroma */
344 /* Try to open the real video output */
345 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
347 if( p_vout->p_sys->p_vout == NULL )
349 /* Everything failed */
350 msg_Err( p_vout, "cannot open vout, aborting" );
355 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
357 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
359 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
361 ADD_PARENT_CALLBACKS( SendEventsToChild );
366 /*****************************************************************************
367 * SpawnRealVout: spawn the real video output.
368 *****************************************************************************/
369 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
371 vout_thread_t *p_real_vout = NULL;
372 video_format_t fmt = {0};
374 msg_Dbg( p_vout, "spawning the real video output" );
376 fmt = p_vout->fmt_out;
378 switch( p_vout->render.i_chroma )
380 case VLC_FOURCC('I','4','2','0'):
381 case VLC_FOURCC('I','Y','U','V'):
382 case VLC_FOURCC('Y','V','1','2'):
383 switch( p_vout->p_sys->i_mode )
385 case DEINTERLACE_MEAN:
386 case DEINTERLACE_DISCARD:
387 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
389 p_real_vout = vout_Create( p_vout, &fmt );
392 case DEINTERLACE_BOB:
393 case DEINTERLACE_BLEND:
394 case DEINTERLACE_LINEAR:
396 p_real_vout = vout_Create( p_vout, &fmt );
401 case VLC_FOURCC('I','4','2','2'):
402 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
403 p_real_vout = vout_Create( p_vout, &fmt );
413 /*****************************************************************************
414 * End: terminate Deinterlace video thread output method
415 *****************************************************************************/
416 static void End( vout_thread_t *p_vout )
420 /* Free the fake output buffers we allocated */
421 for( i_index = I_OUTPUTPICTURES ; i_index ; )
424 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
427 if( p_vout->p_sys->p_vout )
429 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
430 vlc_object_detach( p_vout->p_sys->p_vout );
431 vout_Destroy( p_vout->p_sys->p_vout );
434 DEL_PARENT_CALLBACKS( SendEventsToChild );
437 /*****************************************************************************
438 * Destroy: destroy Deinterlace video thread output method
439 *****************************************************************************
440 * Terminate an output method created by DeinterlaceCreateOutputMethod
441 *****************************************************************************/
442 static void Destroy( vlc_object_t *p_this )
444 vout_thread_t *p_vout = (vout_thread_t *)p_this;
445 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
446 free( p_vout->p_sys );
449 /*****************************************************************************
450 * Render: displays previously rendered output
451 *****************************************************************************
452 * This function send the currently rendered image to Deinterlace image,
453 * waits until it is displayed and switch the two rendering buffers, preparing
455 *****************************************************************************/
456 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
458 vout_sys_t *p_sys = p_vout->p_sys;
459 picture_t *pp_outpic[2];
461 p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
462 p_vout->fmt_in.i_x_offset;
463 p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
464 p_vout->fmt_in.i_y_offset;
465 p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
466 p_vout->fmt_in.i_visible_width;
467 p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
468 p_vout->fmt_in.i_visible_height;
469 if( p_vout->p_sys->i_mode == DEINTERLACE_MEAN ||
470 p_vout->p_sys->i_mode == DEINTERLACE_DISCARD )
472 p_vout->fmt_out.i_y_offset /= 2; p_sys->p_vout->fmt_in.i_y_offset /= 2;
473 p_vout->fmt_out.i_visible_height /= 2;
474 p_sys->p_vout->fmt_in.i_visible_height /= 2;
477 pp_outpic[0] = pp_outpic[1] = NULL;
479 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
481 /* Get a new picture */
482 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
486 if( p_vout->b_die || p_vout->b_error )
488 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
491 msleep( VOUT_OUTMEM_SLEEP );
494 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
496 /* If we are using double rate, get an additional new picture */
497 if( p_vout->p_sys->b_double_rate )
499 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
503 if( p_vout->b_die || p_vout->b_error )
505 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
506 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
509 msleep( VOUT_OUTMEM_SLEEP );
512 /* 20ms is a bit arbitrary, but it's only for the first image we get */
513 if( !p_vout->p_sys->last_date )
515 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
516 p_pic->date + 20000 );
520 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
521 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
523 p_vout->p_sys->last_date = p_pic->date;
526 switch( p_vout->p_sys->i_mode )
528 case DEINTERLACE_DISCARD:
529 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
530 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
533 case DEINTERLACE_BOB:
534 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
535 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
536 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
537 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
540 case DEINTERLACE_LINEAR:
541 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
542 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
543 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
544 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
547 case DEINTERLACE_MEAN:
548 RenderMean( p_vout, pp_outpic[0], p_pic );
549 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
552 case DEINTERLACE_BLEND:
553 RenderBlend( p_vout, pp_outpic[0], p_pic );
554 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
558 RenderX( p_vout, pp_outpic[0], p_pic );
559 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
562 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
565 /*****************************************************************************
566 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
567 *****************************************************************************/
568 static void RenderDiscard( vout_thread_t *p_vout,
569 picture_t *p_outpic, picture_t *p_pic, int i_field )
573 /* Copy image and skip lines */
574 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
576 uint8_t *p_in, *p_out_end, *p_out;
579 p_in = p_pic->p[i_plane].p_pixels
580 + i_field * p_pic->p[i_plane].i_pitch;
582 p_out = p_outpic->p[i_plane].p_pixels;
583 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
584 * p_outpic->p[i_plane].i_visible_lines;
586 switch( p_vout->render.i_chroma )
588 case VLC_FOURCC('I','4','2','0'):
589 case VLC_FOURCC('I','Y','U','V'):
590 case VLC_FOURCC('Y','V','1','2'):
592 for( ; p_out < p_out_end ; )
594 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
595 p_pic->p[i_plane].i_pitch );
597 p_out += p_outpic->p[i_plane].i_pitch;
598 p_in += 2 * p_pic->p[i_plane].i_pitch;
602 case VLC_FOURCC('I','4','2','2'):
604 i_increment = 2 * p_pic->p[i_plane].i_pitch;
606 if( i_plane == Y_PLANE )
608 for( ; p_out < p_out_end ; )
610 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
611 p_pic->p[i_plane].i_pitch );
612 p_out += p_outpic->p[i_plane].i_pitch;
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;
621 for( ; p_out < p_out_end ; )
623 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
624 p_pic->p[i_plane].i_pitch );
625 p_out += p_outpic->p[i_plane].i_pitch;
637 /*****************************************************************************
638 * RenderBob: renders a BOB picture - simple copy
639 *****************************************************************************/
640 static void RenderBob( vout_thread_t *p_vout,
641 picture_t *p_outpic, picture_t *p_pic, int i_field )
645 /* Copy image and skip lines */
646 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
648 uint8_t *p_in, *p_out_end, *p_out;
650 p_in = p_pic->p[i_plane].p_pixels;
651 p_out = p_outpic->p[i_plane].p_pixels;
652 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
653 * p_outpic->p[i_plane].i_visible_lines;
655 switch( p_vout->render.i_chroma )
657 case VLC_FOURCC('I','4','2','0'):
658 case VLC_FOURCC('I','Y','U','V'):
659 case VLC_FOURCC('Y','V','1','2'):
660 /* For BOTTOM field we need to add the first line */
663 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
664 p_pic->p[i_plane].i_pitch );
665 p_in += p_pic->p[i_plane].i_pitch;
666 p_out += p_outpic->p[i_plane].i_pitch;
669 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
671 for( ; p_out < p_out_end ; )
673 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
674 p_pic->p[i_plane].i_pitch );
676 p_out += p_outpic->p[i_plane].i_pitch;
678 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
679 p_pic->p[i_plane].i_pitch );
681 p_in += 2 * p_pic->p[i_plane].i_pitch;
682 p_out += p_outpic->p[i_plane].i_pitch;
685 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
686 p_pic->p[i_plane].i_pitch );
688 /* For TOP field we need to add the last line */
691 p_in += p_pic->p[i_plane].i_pitch;
692 p_out += p_outpic->p[i_plane].i_pitch;
693 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
694 p_pic->p[i_plane].i_pitch );
698 case VLC_FOURCC('I','4','2','2'):
699 /* For BOTTOM field we need to add the first line */
702 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
703 p_pic->p[i_plane].i_pitch );
704 p_in += p_pic->p[i_plane].i_pitch;
705 p_out += p_outpic->p[i_plane].i_pitch;
708 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
710 if( i_plane == Y_PLANE )
712 for( ; p_out < p_out_end ; )
714 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
715 p_pic->p[i_plane].i_pitch );
717 p_out += p_outpic->p[i_plane].i_pitch;
719 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
720 p_pic->p[i_plane].i_pitch );
722 p_in += 2 * p_pic->p[i_plane].i_pitch;
723 p_out += p_outpic->p[i_plane].i_pitch;
728 for( ; p_out < p_out_end ; )
730 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
731 p_pic->p[i_plane].i_pitch );
733 p_out += p_outpic->p[i_plane].i_pitch;
734 p_in += 2 * p_pic->p[i_plane].i_pitch;
738 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
739 p_pic->p[i_plane].i_pitch );
741 /* For TOP field we need to add the last line */
744 p_in += p_pic->p[i_plane].i_pitch;
745 p_out += p_outpic->p[i_plane].i_pitch;
746 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
747 p_pic->p[i_plane].i_pitch );
754 #define Merge p_vout->p_sys->pf_merge
755 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
757 /*****************************************************************************
758 * RenderLinear: BOB with linear interpolation
759 *****************************************************************************/
760 static void RenderLinear( vout_thread_t *p_vout,
761 picture_t *p_outpic, picture_t *p_pic, int i_field )
765 /* Copy image and skip lines */
766 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
768 uint8_t *p_in, *p_out_end, *p_out;
770 p_in = p_pic->p[i_plane].p_pixels;
771 p_out = p_outpic->p[i_plane].p_pixels;
772 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
773 * p_outpic->p[i_plane].i_visible_lines;
775 /* For BOTTOM field we need to add the first line */
778 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
779 p_pic->p[i_plane].i_pitch );
780 p_in += p_pic->p[i_plane].i_pitch;
781 p_out += p_outpic->p[i_plane].i_pitch;
784 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
786 for( ; p_out < p_out_end ; )
788 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
789 p_pic->p[i_plane].i_pitch );
791 p_out += p_outpic->p[i_plane].i_pitch;
793 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
794 p_pic->p[i_plane].i_pitch );
796 p_in += 2 * p_pic->p[i_plane].i_pitch;
797 p_out += p_outpic->p[i_plane].i_pitch;
800 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
801 p_pic->p[i_plane].i_pitch );
803 /* For TOP field we need to add the last line */
806 p_in += p_pic->p[i_plane].i_pitch;
807 p_out += p_outpic->p[i_plane].i_pitch;
808 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
809 p_pic->p[i_plane].i_pitch );
815 static void RenderMean( vout_thread_t *p_vout,
816 picture_t *p_outpic, picture_t *p_pic )
820 /* Copy image and skip lines */
821 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
823 uint8_t *p_in, *p_out_end, *p_out;
825 p_in = p_pic->p[i_plane].p_pixels;
827 p_out = p_outpic->p[i_plane].p_pixels;
828 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
829 * p_outpic->p[i_plane].i_visible_lines;
831 /* All lines: mean value */
832 for( ; p_out < p_out_end ; )
834 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
835 p_pic->p[i_plane].i_pitch );
837 p_out += p_outpic->p[i_plane].i_pitch;
838 p_in += 2 * p_pic->p[i_plane].i_pitch;
844 static void RenderBlend( vout_thread_t *p_vout,
845 picture_t *p_outpic, picture_t *p_pic )
849 /* Copy image and skip lines */
850 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
852 uint8_t *p_in, *p_out_end, *p_out;
854 p_in = p_pic->p[i_plane].p_pixels;
856 p_out = p_outpic->p[i_plane].p_pixels;
857 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
858 * p_outpic->p[i_plane].i_visible_lines;
860 switch( p_vout->render.i_chroma )
862 case VLC_FOURCC('I','4','2','0'):
863 case VLC_FOURCC('I','Y','U','V'):
864 case VLC_FOURCC('Y','V','1','2'):
865 /* First line: simple copy */
866 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
867 p_pic->p[i_plane].i_pitch );
868 p_out += p_outpic->p[i_plane].i_pitch;
870 /* Remaining lines: mean value */
871 for( ; p_out < p_out_end ; )
873 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
874 p_pic->p[i_plane].i_pitch );
876 p_out += p_outpic->p[i_plane].i_pitch;
877 p_in += p_pic->p[i_plane].i_pitch;
881 case VLC_FOURCC('I','4','2','2'):
882 /* First line: simple copy */
883 p_vout->p_libvlc->pf_memcpy( p_out, p_in,
884 p_pic->p[i_plane].i_pitch );
885 p_out += p_outpic->p[i_plane].i_pitch;
887 /* Remaining lines: mean value */
888 if( i_plane == Y_PLANE )
890 for( ; p_out < p_out_end ; )
892 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
893 p_pic->p[i_plane].i_pitch );
895 p_out += p_outpic->p[i_plane].i_pitch;
896 p_in += p_pic->p[i_plane].i_pitch;
902 for( ; p_out < p_out_end ; )
904 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
905 p_pic->p[i_plane].i_pitch );
907 p_out += p_outpic->p[i_plane].i_pitch;
908 p_in += 2*p_pic->p[i_plane].i_pitch;
919 static void MergeGeneric( void *_p_dest, const void *_p_s1,
920 const void *_p_s2, size_t i_bytes )
922 uint8_t* p_dest = (uint8_t*)_p_dest;
923 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
924 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
925 uint8_t* p_end = p_dest + i_bytes - 8;
927 while( p_dest < p_end )
929 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
930 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
931 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
932 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
933 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
934 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
935 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
936 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
941 while( p_dest < p_end )
943 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
947 #if defined(CAN_COMPILE_MMXEXT)
948 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
951 uint8_t* p_dest = (uint8_t*)_p_dest;
952 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
953 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
954 uint8_t* p_end = p_dest + i_bytes - 8;
955 while( p_dest < p_end )
957 __asm__ __volatile__( "movq %2,%%mm1;"
959 "movq %%mm1, %0" :"=m" (*p_dest):
969 while( p_dest < p_end )
971 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
976 #if defined(CAN_COMPILE_3DNOW)
977 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
980 uint8_t* p_dest = (uint8_t*)_p_dest;
981 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
982 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
983 uint8_t* p_end = p_dest + i_bytes - 8;
984 while( p_dest < p_end )
986 __asm__ __volatile__( "movq %2,%%mm1;"
988 "movq %%mm1, %0" :"=m" (*p_dest):
998 while( p_dest < p_end )
1000 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1005 #if defined(CAN_COMPILE_SSE)
1006 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1009 uint8_t* p_dest = (uint8_t*)_p_dest;
1010 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1011 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1013 while( (ptrdiff_t)p_s1 % 16 )
1015 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1017 p_end = p_dest + i_bytes - 16;
1018 while( p_dest < p_end )
1020 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1022 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1032 while( p_dest < p_end )
1034 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1039 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1040 static void EndMMX( void )
1042 __asm__ __volatile__( "emms" :: );
1046 #if defined(CAN_COMPILE_3DNOW)
1047 static void End3DNow( void )
1049 __asm__ __volatile__( "femms" :: );
1053 #ifdef CAN_COMPILE_C_ALTIVEC
1054 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1055 const void *_p_s2, size_t i_bytes )
1057 uint8_t *p_dest = (uint8_t *)_p_dest;
1058 uint8_t *p_s1 = (uint8_t *)_p_s1;
1059 uint8_t *p_s2 = (uint8_t *)_p_s2;
1060 uint8_t *p_end = p_dest + i_bytes - 15;
1062 /* Use C until the first 16-bytes aligned destination pixel */
1063 while( (int)p_dest & 0xF )
1065 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1068 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1070 /* Unaligned source */
1071 vector unsigned char s1v, s2v, destv;
1072 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1073 vector unsigned char perm1v, perm2v;
1075 perm1v = vec_lvsl( 0, p_s1 );
1076 perm2v = vec_lvsl( 0, p_s2 );
1077 s1oldv = vec_ld( 0, p_s1 );
1078 s2oldv = vec_ld( 0, p_s2 );
1080 while( p_dest < p_end )
1082 s1newv = vec_ld( 16, p_s1 );
1083 s2newv = vec_ld( 16, p_s2 );
1084 s1v = vec_perm( s1oldv, s1newv, perm1v );
1085 s2v = vec_perm( s2oldv, s2newv, perm2v );
1088 destv = vec_avg( s1v, s2v );
1089 vec_st( destv, 0, p_dest );
1098 /* Aligned source */
1099 vector unsigned char s1v, s2v, destv;
1101 while( p_dest < p_end )
1103 s1v = vec_ld( 0, p_s1 );
1104 s2v = vec_ld( 0, p_s2 );
1105 destv = vec_avg( s1v, s2v );
1106 vec_st( destv, 0, p_dest );
1116 while( p_dest < p_end )
1118 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1123 /*****************************************************************************
1124 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1125 * and apply a process to recreate the bottom field :
1126 * If a 8x8 block is classified as :
1127 * - progressive: it applies a small blend (1,6,1)
1129 * * in the MMX version: we do a ME between the 2 fields, if there is a
1130 * good match we use MC to recreate the bottom field (with a small
1132 * * otherwise: it recreates the bottom field by an edge oriented
1134 *****************************************************************************/
1136 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1137 * XXX: It need to access to 8x10
1138 * We use more than 8 lines to help with scrolling (text)
1139 * (and because XDeint8x8Frame use line 9)
1140 * XXX: smooth/uniform area with noise detection doesn't works well
1141 * but it's not really a problem because they don't have much details anyway
1143 static inline int ssd( int a ) { return a*a; }
1144 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1150 /* Detect interlacing */
1152 for( y = 0; y < 7; y += 2 )
1155 for( x = 0; x < 8; x++ )
1157 fr += ssd(src[ x] - src[1*i_src+x]) +
1158 ssd(src[i_src+x] - src[2*i_src+x]);
1159 ff += ssd(src[ x] - src[2*i_src+x]) +
1160 ssd(src[i_src+x] - src[3*i_src+x]);
1162 if( ff < 6*fr/8 && fr > 32 )
1168 return fc < 1 ? VLC_FALSE : VLC_TRUE;
1170 #ifdef CAN_COMPILE_MMXEXT
1171 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1178 /* Detect interlacing */
1180 pxor_r2r( mm7, mm7 );
1181 for( y = 0; y < 9; y += 2 )
1184 pxor_r2r( mm5, mm5 );
1185 pxor_r2r( mm6, mm6 );
1186 for( x = 0; x < 8; x+=4 )
1188 movd_m2r( src[ x], mm0 );
1189 movd_m2r( src[1*i_src+x], mm1 );
1190 movd_m2r( src[2*i_src+x], mm2 );
1191 movd_m2r( src[3*i_src+x], mm3 );
1193 punpcklbw_r2r( mm7, mm0 );
1194 punpcklbw_r2r( mm7, mm1 );
1195 punpcklbw_r2r( mm7, mm2 );
1196 punpcklbw_r2r( mm7, mm3 );
1198 movq_r2r( mm0, mm4 );
1200 psubw_r2r( mm1, mm0 );
1201 psubw_r2r( mm2, mm4 );
1203 psubw_r2r( mm1, mm2 );
1204 psubw_r2r( mm1, mm3 );
1206 pmaddwd_r2r( mm0, mm0 );
1207 pmaddwd_r2r( mm4, mm4 );
1208 pmaddwd_r2r( mm2, mm2 );
1209 pmaddwd_r2r( mm3, mm3 );
1210 paddd_r2r( mm0, mm2 );
1211 paddd_r2r( mm4, mm3 );
1212 paddd_r2r( mm2, mm5 );
1213 paddd_r2r( mm3, mm6 );
1216 movq_r2r( mm5, mm0 );
1217 psrlq_i2r( 32, mm0 );
1218 paddd_r2r( mm0, mm5 );
1219 movd_r2m( mm5, fr );
1221 movq_r2r( mm6, mm0 );
1222 psrlq_i2r( 32, mm0 );
1223 paddd_r2r( mm0, mm6 );
1224 movd_r2m( mm6, ff );
1226 if( ff < 6*fr/8 && fr > 32 )
1235 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1236 * This won't destroy details, and help if there is a bit of interlacing.
1237 * (It helps with paning to avoid flickers)
1241 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1242 uint8_t *src, int i_src )
1247 for( y = 0; y < 8; y += 2 )
1249 memcpy( dst, src, 8 );
1252 for( x = 0; x < 8; x++ )
1253 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1259 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1260 uint8_t *src1, int i_src1,
1261 uint8_t *src2, int i_src2 )
1266 for( y = 0; y < 8; y += 2 )
1268 memcpy( dst, src1, 8 );
1271 for( x = 0; x < 8; x++ )
1272 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1280 #ifdef CAN_COMPILE_MMXEXT
1281 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1282 uint8_t *src1, int i_src1,
1283 uint8_t *src2, int i_src2 )
1285 static const uint64_t m_4 = I64C(0x0004000400040004);
1289 pxor_r2r( mm7, mm7 );
1290 for( y = 0; y < 8; y += 2 )
1292 for( x = 0; x < 8; x +=4 )
1294 movd_m2r( src1[x], mm0 );
1295 movd_r2m( mm0, dst[x] );
1297 movd_m2r( src2[x], mm1 );
1298 movd_m2r( src1[i_src1+x], mm2 );
1300 punpcklbw_r2r( mm7, mm0 );
1301 punpcklbw_r2r( mm7, mm1 );
1302 punpcklbw_r2r( mm7, mm2 );
1303 paddw_r2r( mm1, mm1 );
1304 movq_r2r( mm1, mm3 );
1305 paddw_r2r( mm3, mm3 );
1306 paddw_r2r( mm2, mm0 );
1307 paddw_r2r( mm3, mm1 );
1308 paddw_m2r( m_4, mm1 );
1309 paddw_r2r( mm1, mm0 );
1310 psraw_i2r( 3, mm0 );
1311 packuswb_r2r( mm7, mm0 );
1312 movd_r2m( mm0, dst[i_dst+x] );
1323 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1326 for( y = 0; y < 8; y++ )
1327 memset( &dst[y*i_dst], v, 8 );
1330 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1333 * TODO: a better one for the inner part.
1335 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1336 uint8_t *src, int i_src )
1341 for( y = 0; y < 8; y += 2 )
1343 memcpy( dst, src, 8 );
1346 for( x = 0; x < 8; x++ )
1347 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1352 #ifdef CAN_COMPILE_MMXEXT
1353 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1354 uint8_t *src, int i_src )
1359 for( y = 0; y < 8; y += 2 )
1361 movq_m2r( src[0], mm0 );
1362 movq_r2m( mm0, dst[0] );
1365 movq_m2r( src[2*i_src], mm1 );
1366 pavgb_r2r( mm1, mm0 );
1368 movq_r2m( mm0, dst[0] );
1376 /* XDeint8x8Field: Edge oriented interpolation
1377 * (Need -4 and +5 pixels H, +1 line)
1379 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1380 uint8_t *src, int i_src )
1385 for( y = 0; y < 8; y += 2 )
1387 memcpy( dst, src, 8 );
1390 for( x = 0; x < 8; x++ )
1392 uint8_t *src2 = &src[2*i_src];
1393 /* I use 8 pixels just to match the MMX version, but it's overkill
1394 * 5 would be enough (less isn't good) */
1395 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1396 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1397 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1398 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1400 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1401 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1402 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1403 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1405 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1406 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1407 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1408 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1410 if( c0 < c1 && c1 <= c2 )
1411 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1412 else if( c2 < c1 && c1 <= c0 )
1413 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1415 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1422 #ifdef CAN_COMPILE_MMXEXT
1423 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1424 uint8_t *src, int i_src )
1429 for( y = 0; y < 8; y += 2 )
1431 memcpy( dst, src, 8 );
1434 for( x = 0; x < 8; x++ )
1436 uint8_t *src2 = &src[2*i_src];
1439 movq_m2r( src[x-2], mm0 );
1440 movq_m2r( src[x-3], mm1 );
1441 movq_m2r( src[x-4], mm2 );
1443 psadbw_m2r( src2[x-4], mm0 );
1444 psadbw_m2r( src2[x-3], mm1 );
1445 psadbw_m2r( src2[x-2], mm2 );
1447 movd_r2m( mm0, c2 );
1448 movd_r2m( mm1, c1 );
1449 movd_r2m( mm2, c0 );
1451 if( c0 < c1 && c1 <= c2 )
1452 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1453 else if( c2 < c1 && c1 <= c0 )
1454 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1456 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1466 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1467 uint8_t *pix2, int i_pix2 )
1472 for( y = 0; y < 8; y++ )
1473 for( x = 0; x < 8; x++ )
1474 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1478 #ifdef CAN_COMPILE_MMXEXT
1479 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1480 uint8_t *pix2, int i_pix2 )
1485 pxor_r2r( mm7, mm7 );
1486 pxor_r2r( mm6, mm6 );
1488 for( y = 0; y < 8; y++ )
1490 movq_m2r( pix1[0], mm0 );
1491 movq_m2r( pix2[0], mm1 );
1493 movq_r2r( mm0, mm2 );
1494 movq_r2r( mm1, mm3 );
1496 punpcklbw_r2r( mm7, mm0 );
1497 punpckhbw_r2r( mm7, mm2 );
1498 punpcklbw_r2r( mm7, mm1 );
1499 punpckhbw_r2r( mm7, mm3 );
1501 psubw_r2r( mm1, mm0 );
1502 psubw_r2r( mm3, mm2 );
1504 pmaddwd_r2r( mm0, mm0 );
1505 pmaddwd_r2r( mm2, mm2 );
1507 paddd_r2r( mm2, mm0 );
1508 paddd_r2r( mm0, mm6 );
1514 movq_r2r( mm6, mm7 );
1515 psrlq_i2r( 32, mm7 );
1516 paddd_r2r( mm6, mm7 );
1525 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1526 #ifdef CAN_COMPILE_MMXEXT
1529 * TODO: mmx version (easier in sse2)
1531 static inline void XDeintMC( uint8_t *dst, int i_dst,
1532 uint8_t *src, int i_src,
1534 int i_width, int i_height )
1536 const int d4x = mvx&0x03;
1537 const int d4y = mvy&0x03;
1539 const int cA = (4-d4x)*(4-d4y);
1540 const int cB = d4x *(4-d4y);
1541 const int cC = (4-d4x)*d4y;
1542 const int cD = d4x *d4y;
1548 src += (mvy >> 2) * i_src + (mvx >> 2);
1551 for( y = 0; y < i_height; y++ )
1553 for( x = 0; x < i_width; x++ )
1555 dst[x] = ( cA*src[x] + cB*src[x+1] +
1556 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1564 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1565 uint8_t *pix2, int i_pix2 )
1569 movq_m2r( pix1[0*i_pix1], mm0 );
1570 movq_m2r( pix1[1*i_pix1], mm1 );
1572 psadbw_m2r( pix2[0*i_pix2], mm0 );
1573 psadbw_m2r( pix2[1*i_pix2], mm1 );
1575 movq_m2r( pix1[2*i_pix1], mm2 );
1576 movq_m2r( pix1[3*i_pix1], mm3 );
1577 psadbw_m2r( pix2[2*i_pix2], mm2 );
1578 psadbw_m2r( pix2[3*i_pix2], mm3 );
1580 paddd_r2r( mm1, mm0 );
1581 paddd_r2r( mm3, mm2 );
1582 paddd_r2r( mm2, mm0 );
1588 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1589 uint8_t *ref, int i_stride,
1591 int xmax, int ymax )
1593 uint8_t buffer[8*4];
1595 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1598 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1599 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1601 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1602 uint8_t *ref, int i_stride,
1604 int xmax, int ymax )
1606 if( abs(mx) >= xmax || abs(my) >= ymax )
1609 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1612 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1613 uint8_t *src, int i_src,
1615 int xmax, int ymax )
1617 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1618 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1619 uint8_t *next = &src[i_src];
1620 const int i_src2 = 2*i_src;
1625 uint8_t *rec = &dst[i_dst];
1627 /* We construct with intra method the missing field */
1628 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1630 /* Now we will try to find a match with ME with the other field */
1632 /* ME: A small/partial EPZS
1633 * We search only for small MV (with high motion intra will be perfect */
1634 if( xmax > 4 ) xmax = 4;
1635 if( ymax > 4 ) ymax = 4;
1637 /* Init with NULL Mv */
1639 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1641 /* Try predicted Mv */
1642 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1648 /* Search interger pel (small mv) */
1649 for( i_step = 0; i_step < 4; i_step++ )
1655 for( i = 0; i < 4; i++ )
1657 s = XDeint8x4TestInt( rec, i_src2,
1658 next, i_src2, mvx+dx[i], mvy+dy[i],
1678 if( mvs > 4 && mvs < 256 )
1681 /* XXX: for now only HPEL (too slow) */
1682 for( i_step = 0; i_step < 4; i_step++ )
1688 for( i = 0; i < 8; i++ )
1690 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1691 mvx+dx[i], mvy+dy[i],
1709 uint8_t buffer[8*4];
1710 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1711 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1713 //XDeint8x8Set( dst, i_dst, 0 );
1720 /* Kernel interpolation (1,-5,20,20,-5,1)
1721 * Lose a bit more details+add aliasing than edge interpol but avoid
1724 static inline uint8_t clip1( int a )
1733 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1734 uint8_t *src, int i_src )
1739 for( y = 0; y < 8; y += 2 )
1741 const int i_src2 = i_src*2;
1743 memcpy( dst, src, 8 );
1746 for( x = 0; x < 8; x++ )
1750 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1751 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1752 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1754 dst[x] = clip1( ( pix + 16 ) >> 5 );
1764 /* NxN arbitray size (and then only use pixel in the NxN block)
1766 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1767 int i_height, int i_width )
1774 /* Detect interlacing */
1775 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1778 for( y = 0; y < i_height - 2; y += 2 )
1780 const uint8_t *s = &src[y*i_src];
1781 for( x = 0; x < i_width; x++ )
1783 fr += ssd(s[ x] - s[1*i_src+x]);
1784 ff += ssd(s[ x] - s[2*i_src+x]);
1786 if( ff < fr && fr > i_width / 2 )
1790 return fc < 2 ? VLC_FALSE : VLC_TRUE;
1793 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1794 uint8_t *src, int i_src,
1795 int i_width, int i_height )
1800 for( y = 0; y < i_height; y += 2 )
1802 memcpy( dst, src, i_width );
1805 if( y < i_height - 2 )
1807 for( x = 0; x < i_width; x++ )
1808 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1812 /* Blend last line */
1813 for( x = 0; x < i_width; x++ )
1814 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1821 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1822 uint8_t *src, int i_src,
1823 int i_width, int i_height )
1828 for( y = 0; y < i_height; y += 2 )
1830 memcpy( dst, src, i_width );
1833 if( y < i_height - 2 )
1835 for( x = 0; x < i_width; x++ )
1836 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1840 /* Blend last line */
1841 for( x = 0; x < i_width; x++ )
1842 dst[x] = (src[x] + src[i_src+x]) >> 1;
1849 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1850 int i_width, int i_height )
1852 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1853 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1855 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1859 static inline int median( int a, int b, int c )
1861 int min = a, max =a;
1872 return a + b + c - min - max;
1878 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1879 uint8_t *src, int i_src,
1880 const int i_mbx, int i_modx )
1884 for( x = 0; x < i_mbx; x++ )
1887 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1889 if( x == 0 || x == i_mbx - 1 )
1890 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1892 XDeint8x8FieldC( dst, i_dst, src, i_src );
1896 XDeint8x8MergeC( dst, i_dst,
1897 &src[0*i_src], 2*i_src,
1898 &src[1*i_src], 2*i_src );
1906 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1908 #ifdef CAN_COMPILE_MMXEXT
1909 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1910 uint8_t *src, int i_src,
1911 const int i_mbx, int i_modx )
1915 /* Reset current line */
1916 for( x = 0; x < i_mbx; x++ )
1919 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1921 if( x == 0 || x == i_mbx - 1 )
1922 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1924 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1928 XDeint8x8MergeMMXEXT( dst, i_dst,
1929 &src[0*i_src], 2*i_src,
1930 &src[1*i_src], 2*i_src );
1938 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1942 static void RenderX( vout_thread_t *p_vout,
1943 picture_t *p_outpic, picture_t *p_pic )
1947 /* Copy image and skip lines */
1948 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1950 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1951 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1953 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1954 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1956 const int i_dst = p_outpic->p[i_plane].i_pitch;
1957 const int i_src = p_pic->p[i_plane].i_pitch;
1961 for( y = 0; y < i_mby; y++ )
1963 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1964 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1966 #ifdef CAN_COMPILE_MMXEXT
1967 if( p_vout->p_libvlc_global->i_cpu & CPU_CAPABILITY_MMXEXT )
1968 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1971 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1974 /* Last line (C only)*/
1977 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1978 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1980 for( x = 0; x < i_mbx; x++ )
1982 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1989 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1993 #ifdef CAN_COMPILE_MMXEXT
1994 if( p_vout->p_libvlc_global->i_cpu & CPU_CAPABILITY_MMXEXT )
1999 /*****************************************************************************
2000 * SendEvents: forward mouse and keyboard events to the parent p_vout
2001 *****************************************************************************/
2002 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
2003 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
2005 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
2006 vlc_value_t sentval = newval;
2008 if( !strcmp( psz_var, "mouse-y" ) )
2010 switch( p_vout->p_sys->i_mode )
2012 case DEINTERLACE_MEAN:
2013 case DEINTERLACE_DISCARD:
2019 var_Set( p_vout, psz_var, sentval );
2024 /*****************************************************************************
2025 * FilterCallback: called when changing the deinterlace method on the fly.
2026 *****************************************************************************/
2027 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
2028 vlc_value_t oldval, vlc_value_t newval,
2031 vout_thread_t * p_vout = (vout_thread_t *)p_this;
2032 int i_old_mode = p_vout->p_sys->i_mode;
2034 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
2036 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
2038 SetFilterMethod( p_vout, newval.psz_string );
2040 switch( p_vout->render.i_chroma )
2042 case VLC_FOURCC('I','4','2','2'):
2043 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2047 case VLC_FOURCC('I','4','2','0'):
2048 case VLC_FOURCC('I','Y','U','V'):
2049 case VLC_FOURCC('Y','V','1','2'):
2050 switch( p_vout->p_sys->i_mode )
2052 case DEINTERLACE_MEAN:
2053 case DEINTERLACE_DISCARD:
2054 if( ( i_old_mode == DEINTERLACE_MEAN )
2055 || ( i_old_mode == DEINTERLACE_DISCARD ) )
2057 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2062 case DEINTERLACE_BOB:
2063 case DEINTERLACE_BLEND:
2064 case DEINTERLACE_LINEAR:
2065 if( ( i_old_mode == DEINTERLACE_BOB )
2066 || ( i_old_mode == DEINTERLACE_BLEND )
2067 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2069 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2080 /* We need to kill the old vout */
2082 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2084 vlc_object_detach( p_vout->p_sys->p_vout );
2085 vout_Destroy( p_vout->p_sys->p_vout );
2087 /* Try to open a new video output */
2088 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2090 if( p_vout->p_sys->p_vout == NULL )
2092 /* Everything failed */
2093 msg_Err( p_vout, "cannot open vout, aborting" );
2095 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2096 return VLC_EGENERIC;
2099 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2101 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2105 /*****************************************************************************
2106 * SendEventsToChild: forward events to the child/children vout
2107 *****************************************************************************/
2108 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2109 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2111 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2112 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2117 /*****************************************************************************
2118 * video filter2 functions
2119 *****************************************************************************/
2120 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2122 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2123 picture_t *p_pic_dst;
2125 /* Request output picture */
2126 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
2127 if( p_pic_dst == NULL )
2129 msg_Warn( p_filter, "can't get output picture" );
2133 switch( p_vout->p_sys->i_mode )
2135 case DEINTERLACE_DISCARD:
2137 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2139 msg_Err( p_vout, "discarding lines is not supported yet" );
2140 p_pic_dst->pf_release( p_pic_dst );
2144 case DEINTERLACE_BOB:
2146 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2147 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2151 case DEINTERLACE_LINEAR:
2153 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2154 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2156 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2157 p_pic_dst->pf_release( p_pic_dst );
2161 case DEINTERLACE_MEAN:
2162 RenderMean( p_vout, p_pic_dst, p_pic );
2165 case DEINTERLACE_BLEND:
2166 RenderBlend( p_vout, p_pic_dst, p_pic );
2170 RenderX( p_vout, p_pic_dst, p_pic );
2174 p_pic_dst->date = p_pic->date;
2175 p_pic_dst->b_force = p_pic->b_force;
2176 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2177 p_pic_dst->b_progressive = VLC_TRUE;
2178 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2180 p_pic->pf_release( p_pic );
2184 /*****************************************************************************
2186 *****************************************************************************/
2187 static int OpenFilter( vlc_object_t *p_this )
2189 filter_t *p_filter = (filter_t*)p_this;
2190 vout_thread_t *p_vout;
2193 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2194 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2195 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2196 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2198 return VLC_EGENERIC;
2201 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2203 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2204 vlc_object_attach( p_vout, p_filter );
2205 p_filter->p_sys = (filter_sys_t *)p_vout;
2206 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2208 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2210 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2211 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2212 var_Set( p_filter, "deinterlace-mode", val );
2214 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2216 vlc_object_detach( p_vout );
2217 vlc_object_release( p_vout );
2218 return VLC_EGENERIC;
2221 p_filter->pf_video_filter = Deinterlace;
2223 msg_Dbg( p_filter, "deinterlacing" );
2228 /*****************************************************************************
2229 * CloseFilter: clean up the filter
2230 *****************************************************************************/
2231 static void CloseFilter( vlc_object_t *p_this )
2233 filter_t *p_filter = (filter_t*)p_this;
2234 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2236 Destroy( VLC_OBJECT(p_vout) );
2237 vlc_object_detach( p_vout );
2238 vlc_object_release( p_vout );