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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 MergeMMX ( void *, const void *, const void *, size_t );
77 #if defined(CAN_COMPILE_SSE)
78 static void MergeSSE2 ( void *, const void *, const void *, size_t );
80 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
81 static void EndMMX ( void );
84 static int SendEvents ( vlc_object_t *, char const *,
85 vlc_value_t, vlc_value_t, void * );
87 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
88 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
90 static int OpenFilter( vlc_object_t *p_this );
91 static void CloseFilter( vlc_object_t *p_this );
93 /*****************************************************************************
95 *****************************************************************************/
96 static int FilterCallback ( vlc_object_t *, char const *,
97 vlc_value_t, vlc_value_t, void * );
99 /*****************************************************************************
101 *****************************************************************************/
102 #define MODE_TEXT N_("Deinterlace mode")
103 #define MODE_LONGTEXT N_("You can choose the default deinterlace mode")
105 #define FILTER_CFG_PREFIX "sout-deinterlace-"
107 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", "x" };
108 static char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
109 N_("Bob"), N_("Linear"), "X" };
112 set_description( _("Deinterlacing video filter") );
113 set_shortname( N_("Deinterlace" ));
114 set_capability( "video filter", 0 );
115 set_category( CAT_VIDEO );
116 set_subcategory( SUBCAT_VIDEO_VFILTER );
118 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
119 MODE_LONGTEXT, VLC_FALSE );
120 change_string_list( mode_list, mode_list_text, 0 );
122 add_shortcut( "deinterlace" );
123 set_callbacks( Create, Destroy );
126 set_capability( "video filter2", 0 );
127 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, MODE_TEXT,
128 MODE_LONGTEXT, VLC_FALSE );
129 change_string_list( mode_list, mode_list_text, 0 );
130 set_callbacks( OpenFilter, CloseFilter );
133 static const char *ppsz_filter_options[] = {
137 /*****************************************************************************
138 * vout_sys_t: Deinterlace video output method descriptor
139 *****************************************************************************
140 * This structure is part of the video output thread descriptor.
141 * It describes the Deinterlace specific properties of an output thread.
142 *****************************************************************************/
145 int i_mode; /* Deinterlace mode */
146 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
151 vout_thread_t *p_vout;
153 vlc_mutex_t filter_lock;
155 void (*pf_merge) ( void *, const void *, const void *, size_t );
156 void (*pf_end_merge) ( void );
159 /*****************************************************************************
160 * Control: control facility for the vout (forwards to child vout)
161 *****************************************************************************/
162 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
164 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
167 /*****************************************************************************
168 * Create: allocates Deinterlace video thread output method
169 *****************************************************************************
170 * This function allocates and initializes a Deinterlace vout method.
171 *****************************************************************************/
172 static int Create( vlc_object_t *p_this )
174 vout_thread_t *p_vout = (vout_thread_t *)p_this;
177 /* Allocate structure */
178 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
179 if( p_vout->p_sys == NULL )
181 msg_Err( p_vout, "out of memory" );
185 p_vout->pf_init = Init;
186 p_vout->pf_end = End;
187 p_vout->pf_manage = NULL;
188 p_vout->pf_render = Render;
189 p_vout->pf_display = NULL;
190 p_vout->pf_control = Control;
192 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
193 p_vout->p_sys->b_double_rate = VLC_FALSE;
194 p_vout->p_sys->last_date = 0;
195 p_vout->p_sys->p_vout = 0;
196 vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
198 #if defined(CAN_COMPILE_C_ALTIVEC)
199 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
201 p_vout->p_sys->pf_merge = MergeAltivec;
202 p_vout->p_sys->pf_end_merge = NULL;
206 #if defined(CAN_COMPILE_SSE)
207 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2 )
209 p_vout->p_sys->pf_merge = MergeSSE2;
210 p_vout->p_sys->pf_end_merge = EndMMX;
214 #if defined(CAN_COMPILE_MMXEXT)
215 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
217 p_vout->p_sys->pf_merge = MergeMMX;
218 p_vout->p_sys->pf_end_merge = EndMMX;
223 p_vout->p_sys->pf_merge = MergeGeneric;
224 p_vout->p_sys->pf_end_merge = NULL;
227 /* Look what method was requested */
228 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
229 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
231 if( val.psz_string == NULL )
233 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
234 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
236 val.psz_string = strdup( "discard" );
239 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
241 SetFilterMethod( p_vout, val.psz_string );
243 free( val.psz_string );
248 /*****************************************************************************
249 * SetFilterMethod: setup the deinterlace method to use.
250 *****************************************************************************/
251 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
253 if( !strcmp( psz_method, "discard" ) )
255 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
256 p_vout->p_sys->b_double_rate = VLC_FALSE;
258 else if( !strcmp( psz_method, "mean" ) )
260 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
261 p_vout->p_sys->b_double_rate = VLC_FALSE;
263 else if( !strcmp( psz_method, "blend" )
264 || !strcmp( psz_method, "average" )
265 || !strcmp( psz_method, "combine-fields" ) )
267 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
268 p_vout->p_sys->b_double_rate = VLC_FALSE;
270 else if( !strcmp( psz_method, "bob" )
271 || !strcmp( psz_method, "progressive-scan" ) )
273 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
274 p_vout->p_sys->b_double_rate = VLC_TRUE;
276 else if( !strcmp( psz_method, "linear" ) )
278 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
279 p_vout->p_sys->b_double_rate = VLC_TRUE;
281 else if( !strcmp( psz_method, "x" ) )
283 p_vout->p_sys->i_mode = DEINTERLACE_X;
284 p_vout->p_sys->b_double_rate = VLC_FALSE;
288 msg_Err( p_vout, "no valid deinterlace mode provided, "
289 "using \"discard\"" );
292 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
295 /*****************************************************************************
296 * Init: initialize Deinterlace video thread output method
297 *****************************************************************************/
298 static int Init( vout_thread_t *p_vout )
303 I_OUTPUTPICTURES = 0;
305 /* Initialize the output structure, full of directbuffers since we want
306 * the decoder to output directly to our structures. */
307 switch( p_vout->render.i_chroma )
309 case VLC_FOURCC('I','4','2','0'):
310 case VLC_FOURCC('I','Y','U','V'):
311 case VLC_FOURCC('Y','V','1','2'):
312 case VLC_FOURCC('I','4','2','2'):
313 p_vout->output.i_chroma = p_vout->render.i_chroma;
314 p_vout->output.i_width = p_vout->render.i_width;
315 p_vout->output.i_height = p_vout->render.i_height;
316 p_vout->output.i_aspect = p_vout->render.i_aspect;
320 return VLC_EGENERIC; /* unknown chroma */
324 /* Try to open the real video output */
325 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
327 if( p_vout->p_sys->p_vout == NULL )
329 /* Everything failed */
330 msg_Err( p_vout, "cannot open vout, aborting" );
335 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
337 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
339 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
341 ADD_PARENT_CALLBACKS( SendEventsToChild );
346 /*****************************************************************************
347 * SpawnRealVout: spawn the real video output.
348 *****************************************************************************/
349 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
351 vout_thread_t *p_real_vout = NULL;
352 video_format_t fmt = {0};
354 msg_Dbg( p_vout, "spawning the real video output" );
356 fmt.i_width = fmt.i_visible_width = p_vout->output.i_width;
357 fmt.i_height = fmt.i_visible_height = p_vout->output.i_height;
358 fmt.i_x_offset = fmt.i_y_offset = 0;
359 fmt.i_chroma = p_vout->output.i_chroma;
360 fmt.i_aspect = p_vout->output.i_aspect;
361 fmt.i_sar_num = p_vout->output.i_aspect * fmt.i_height / fmt.i_width;
362 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
364 switch( p_vout->render.i_chroma )
366 case VLC_FOURCC('I','4','2','0'):
367 case VLC_FOURCC('I','Y','U','V'):
368 case VLC_FOURCC('Y','V','1','2'):
369 switch( p_vout->p_sys->i_mode )
371 case DEINTERLACE_MEAN:
372 case DEINTERLACE_DISCARD:
373 fmt.i_height = fmt.i_visible_height = p_vout->output.i_height / 2;
374 p_real_vout = vout_Create( p_vout, &fmt );
377 case DEINTERLACE_BOB:
378 case DEINTERLACE_BLEND:
379 case DEINTERLACE_LINEAR:
381 p_real_vout = vout_Create( p_vout, &fmt );
386 case VLC_FOURCC('I','4','2','2'):
387 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
388 p_real_vout = vout_Create( p_vout, &fmt );
398 /*****************************************************************************
399 * End: terminate Deinterlace video thread output method
400 *****************************************************************************/
401 static void End( vout_thread_t *p_vout )
405 /* Free the fake output buffers we allocated */
406 for( i_index = I_OUTPUTPICTURES ; i_index ; )
409 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
412 if( p_vout->p_sys->p_vout )
414 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
415 vlc_object_detach( p_vout->p_sys->p_vout );
416 vout_Destroy( p_vout->p_sys->p_vout );
419 DEL_PARENT_CALLBACKS( SendEventsToChild );
422 /*****************************************************************************
423 * Destroy: destroy Deinterlace video thread output method
424 *****************************************************************************
425 * Terminate an output method created by DeinterlaceCreateOutputMethod
426 *****************************************************************************/
427 static void Destroy( vlc_object_t *p_this )
429 vout_thread_t *p_vout = (vout_thread_t *)p_this;
430 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
431 free( p_vout->p_sys );
434 /*****************************************************************************
435 * Render: displays previously rendered output
436 *****************************************************************************
437 * This function send the currently rendered image to Deinterlace image,
438 * waits until it is displayed and switch the two rendering buffers, preparing
440 *****************************************************************************/
441 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
443 picture_t *pp_outpic[2];
445 pp_outpic[0] = pp_outpic[1] = NULL;
447 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
449 /* Get a new picture */
450 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
454 if( p_vout->b_die || p_vout->b_error )
456 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
459 msleep( VOUT_OUTMEM_SLEEP );
462 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
464 /* If we are using double rate, get an additional new picture */
465 if( p_vout->p_sys->b_double_rate )
467 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
471 if( p_vout->b_die || p_vout->b_error )
473 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
474 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
477 msleep( VOUT_OUTMEM_SLEEP );
480 /* 20ms is a bit arbitrary, but it's only for the first image we get */
481 if( !p_vout->p_sys->last_date )
483 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
484 p_pic->date + 20000 );
488 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
489 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
491 p_vout->p_sys->last_date = p_pic->date;
494 switch( p_vout->p_sys->i_mode )
496 case DEINTERLACE_DISCARD:
497 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
498 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
501 case DEINTERLACE_BOB:
502 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
503 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
504 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
505 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
508 case DEINTERLACE_LINEAR:
509 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
510 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
511 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
512 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
515 case DEINTERLACE_MEAN:
516 RenderMean( p_vout, pp_outpic[0], p_pic );
517 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
520 case DEINTERLACE_BLEND:
521 RenderBlend( p_vout, pp_outpic[0], p_pic );
522 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
526 RenderX( p_vout, pp_outpic[0], p_pic );
527 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
530 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
533 /*****************************************************************************
534 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
535 *****************************************************************************/
536 static void RenderDiscard( vout_thread_t *p_vout,
537 picture_t *p_outpic, picture_t *p_pic, int i_field )
541 /* Copy image and skip lines */
542 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
544 uint8_t *p_in, *p_out_end, *p_out;
547 p_in = p_pic->p[i_plane].p_pixels
548 + i_field * p_pic->p[i_plane].i_pitch;
550 p_out = p_outpic->p[i_plane].p_pixels;
551 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
552 * p_outpic->p[i_plane].i_visible_lines;
554 switch( p_vout->render.i_chroma )
556 case VLC_FOURCC('I','4','2','0'):
557 case VLC_FOURCC('I','Y','U','V'):
558 case VLC_FOURCC('Y','V','1','2'):
560 for( ; p_out < p_out_end ; )
562 p_vout->p_vlc->pf_memcpy( p_out, p_in,
563 p_pic->p[i_plane].i_pitch );
565 p_out += p_pic->p[i_plane].i_pitch;
566 p_in += 2 * p_pic->p[i_plane].i_pitch;
570 case VLC_FOURCC('I','4','2','2'):
572 i_increment = 2 * p_pic->p[i_plane].i_pitch;
574 if( i_plane == Y_PLANE )
576 for( ; p_out < p_out_end ; )
578 p_vout->p_vlc->pf_memcpy( p_out, p_in,
579 p_pic->p[i_plane].i_pitch );
580 p_out += p_pic->p[i_plane].i_pitch;
581 p_vout->p_vlc->pf_memcpy( p_out, p_in,
582 p_pic->p[i_plane].i_pitch );
583 p_out += p_pic->p[i_plane].i_pitch;
589 for( ; p_out < p_out_end ; )
591 p_vout->p_vlc->pf_memcpy( p_out, p_in,
592 p_pic->p[i_plane].i_pitch );
593 p_out += p_pic->p[i_plane].i_pitch;
605 /*****************************************************************************
606 * RenderBob: renders a BOB picture - simple copy
607 *****************************************************************************/
608 static void RenderBob( vout_thread_t *p_vout,
609 picture_t *p_outpic, picture_t *p_pic, int i_field )
613 /* Copy image and skip lines */
614 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
616 uint8_t *p_in, *p_out_end, *p_out;
618 p_in = p_pic->p[i_plane].p_pixels;
619 p_out = p_outpic->p[i_plane].p_pixels;
620 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
621 * p_outpic->p[i_plane].i_visible_lines;
623 switch( p_vout->render.i_chroma )
625 case VLC_FOURCC('I','4','2','0'):
626 case VLC_FOURCC('I','Y','U','V'):
627 case VLC_FOURCC('Y','V','1','2'):
628 /* For BOTTOM field we need to add the first line */
631 p_vout->p_vlc->pf_memcpy( p_out, p_in,
632 p_pic->p[i_plane].i_pitch );
633 p_in += p_pic->p[i_plane].i_pitch;
634 p_out += p_pic->p[i_plane].i_pitch;
637 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
639 for( ; p_out < p_out_end ; )
641 p_vout->p_vlc->pf_memcpy( p_out, p_in,
642 p_pic->p[i_plane].i_pitch );
644 p_out += p_pic->p[i_plane].i_pitch;
646 p_vout->p_vlc->pf_memcpy( p_out, p_in,
647 p_pic->p[i_plane].i_pitch );
649 p_in += 2 * p_pic->p[i_plane].i_pitch;
650 p_out += p_pic->p[i_plane].i_pitch;
653 p_vout->p_vlc->pf_memcpy( p_out, p_in,
654 p_pic->p[i_plane].i_pitch );
656 /* For TOP field we need to add the last line */
659 p_in += p_pic->p[i_plane].i_pitch;
660 p_out += p_pic->p[i_plane].i_pitch;
661 p_vout->p_vlc->pf_memcpy( p_out, p_in,
662 p_pic->p[i_plane].i_pitch );
666 case VLC_FOURCC('I','4','2','2'):
667 /* For BOTTOM field we need to add the first line */
670 p_vout->p_vlc->pf_memcpy( p_out, p_in,
671 p_pic->p[i_plane].i_pitch );
672 p_in += p_pic->p[i_plane].i_pitch;
673 p_out += p_pic->p[i_plane].i_pitch;
676 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
678 if( i_plane == Y_PLANE )
680 for( ; p_out < p_out_end ; )
682 p_vout->p_vlc->pf_memcpy( p_out, p_in,
683 p_pic->p[i_plane].i_pitch );
685 p_out += p_pic->p[i_plane].i_pitch;
687 p_vout->p_vlc->pf_memcpy( p_out, p_in,
688 p_pic->p[i_plane].i_pitch );
690 p_in += 2 * p_pic->p[i_plane].i_pitch;
691 p_out += p_pic->p[i_plane].i_pitch;
696 for( ; p_out < p_out_end ; )
698 p_vout->p_vlc->pf_memcpy( p_out, p_in,
699 p_pic->p[i_plane].i_pitch );
701 p_out += p_pic->p[i_plane].i_pitch;
702 p_in += 2 * p_pic->p[i_plane].i_pitch;
706 p_vout->p_vlc->pf_memcpy( p_out, p_in,
707 p_pic->p[i_plane].i_pitch );
709 /* For TOP field we need to add the last line */
712 p_in += p_pic->p[i_plane].i_pitch;
713 p_out += p_pic->p[i_plane].i_pitch;
714 p_vout->p_vlc->pf_memcpy( p_out, p_in,
715 p_pic->p[i_plane].i_pitch );
722 #define Merge p_vout->p_sys->pf_merge
723 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
725 /*****************************************************************************
726 * RenderLinear: BOB with linear interpolation
727 *****************************************************************************/
728 static void RenderLinear( vout_thread_t *p_vout,
729 picture_t *p_outpic, picture_t *p_pic, int i_field )
733 /* Copy image and skip lines */
734 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
736 uint8_t *p_in, *p_out_end, *p_out;
738 p_in = p_pic->p[i_plane].p_pixels;
739 p_out = p_outpic->p[i_plane].p_pixels;
740 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
741 * p_outpic->p[i_plane].i_visible_lines;
743 /* For BOTTOM field we need to add the first line */
746 p_vout->p_vlc->pf_memcpy( p_out, p_in,
747 p_pic->p[i_plane].i_pitch );
748 p_in += p_pic->p[i_plane].i_pitch;
749 p_out += p_pic->p[i_plane].i_pitch;
752 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
754 for( ; p_out < p_out_end ; )
756 p_vout->p_vlc->pf_memcpy( p_out, p_in,
757 p_pic->p[i_plane].i_pitch );
759 p_out += p_pic->p[i_plane].i_pitch;
761 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
762 p_pic->p[i_plane].i_pitch );
764 p_in += 2 * p_pic->p[i_plane].i_pitch;
765 p_out += p_pic->p[i_plane].i_pitch;
768 p_vout->p_vlc->pf_memcpy( p_out, p_in,
769 p_pic->p[i_plane].i_pitch );
771 /* For TOP field we need to add the last line */
774 p_in += p_pic->p[i_plane].i_pitch;
775 p_out += p_pic->p[i_plane].i_pitch;
776 p_vout->p_vlc->pf_memcpy( p_out, p_in,
777 p_pic->p[i_plane].i_pitch );
783 static void RenderMean( vout_thread_t *p_vout,
784 picture_t *p_outpic, picture_t *p_pic )
788 /* Copy image and skip lines */
789 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
791 uint8_t *p_in, *p_out_end, *p_out;
793 p_in = p_pic->p[i_plane].p_pixels;
795 p_out = p_outpic->p[i_plane].p_pixels;
796 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
797 * p_outpic->p[i_plane].i_visible_lines;
799 /* All lines: mean value */
800 for( ; p_out < p_out_end ; )
802 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
803 p_pic->p[i_plane].i_pitch );
805 p_out += p_pic->p[i_plane].i_pitch;
806 p_in += 2 * p_pic->p[i_plane].i_pitch;
812 static void RenderBlend( vout_thread_t *p_vout,
813 picture_t *p_outpic, picture_t *p_pic )
817 /* Copy image and skip lines */
818 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
820 uint8_t *p_in, *p_out_end, *p_out;
822 p_in = p_pic->p[i_plane].p_pixels;
824 p_out = p_outpic->p[i_plane].p_pixels;
825 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
826 * p_outpic->p[i_plane].i_visible_lines;
828 switch( p_vout->render.i_chroma )
830 case VLC_FOURCC('I','4','2','0'):
831 case VLC_FOURCC('I','Y','U','V'):
832 case VLC_FOURCC('Y','V','1','2'):
833 /* First line: simple copy */
834 p_vout->p_vlc->pf_memcpy( p_out, p_in,
835 p_pic->p[i_plane].i_pitch );
836 p_out += p_pic->p[i_plane].i_pitch;
838 /* Remaining lines: mean value */
839 for( ; p_out < p_out_end ; )
841 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
842 p_pic->p[i_plane].i_pitch );
844 p_out += p_pic->p[i_plane].i_pitch;
845 p_in += p_pic->p[i_plane].i_pitch;
849 case VLC_FOURCC('I','4','2','2'):
850 /* First line: simple copy */
851 p_vout->p_vlc->pf_memcpy( p_out, p_in,
852 p_pic->p[i_plane].i_pitch );
853 p_out += p_pic->p[i_plane].i_pitch;
855 /* Remaining lines: mean value */
856 if( i_plane == Y_PLANE )
858 for( ; p_out < p_out_end ; )
860 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
861 p_pic->p[i_plane].i_pitch );
863 p_out += p_pic->p[i_plane].i_pitch;
864 p_in += p_pic->p[i_plane].i_pitch;
870 for( ; p_out < p_out_end ; )
872 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
873 p_pic->p[i_plane].i_pitch );
875 p_out += p_pic->p[i_plane].i_pitch;
876 p_in += 2*p_pic->p[i_plane].i_pitch;
887 static void MergeGeneric( void *_p_dest, const void *_p_s1,
888 const void *_p_s2, size_t i_bytes )
890 uint8_t* p_dest = (uint8_t*)_p_dest;
891 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
892 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
893 uint8_t* p_end = p_dest + i_bytes - 8;
895 while( p_dest < p_end )
897 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
898 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
899 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
900 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
901 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
902 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
903 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
904 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
909 while( p_dest < p_end )
911 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
915 #if defined(CAN_COMPILE_MMXEXT)
916 static void MergeMMX( void *_p_dest, const void *_p_s1, const void *_p_s2,
919 uint8_t* p_dest = (uint8_t*)_p_dest;
920 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
921 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
922 uint8_t* p_end = p_dest + i_bytes - 8;
923 while( p_dest < p_end )
925 __asm__ __volatile__( "movq %2,%%mm1;"
927 "movq %%mm1, %0" :"=m" (*p_dest):
937 while( p_dest < p_end )
939 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
944 #if defined(CAN_COMPILE_SSE)
945 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
948 uint8_t* p_dest = (uint8_t*)_p_dest;
949 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
950 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
952 while( (ptrdiff_t)p_s1 % 16 )
954 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
956 p_end = p_dest + i_bytes - 16;
957 while( p_dest < p_end )
959 __asm__ __volatile__( "movdqu %2,%%xmm1;"
961 "movdqu %%xmm1, %0" :"=m" (*p_dest):
971 while( p_dest < p_end )
973 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
978 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
979 static void EndMMX( void )
981 __asm__ __volatile__( "emms" :: );
985 #ifdef CAN_COMPILE_C_ALTIVEC
986 static void MergeAltivec( void *_p_dest, const void *_p_s1,
987 const void *_p_s2, size_t i_bytes )
989 uint8_t *p_dest = (uint8_t *)_p_dest;
990 uint8_t *p_s1 = (uint8_t *)_p_s1;
991 uint8_t *p_s2 = (uint8_t *)_p_s2;
992 uint8_t *p_end = p_dest + i_bytes - 15;
994 /* Use C until the first 16-bytes aligned destination pixel */
995 while( (int)p_dest & 0xF )
997 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1000 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1002 /* Unaligned source */
1003 vector unsigned char s1v, s2v, destv;
1004 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1005 vector unsigned char perm1v, perm2v;
1007 perm1v = vec_lvsl( 0, p_s1 );
1008 perm2v = vec_lvsl( 0, p_s2 );
1009 s1oldv = vec_ld( 0, p_s1 );
1010 s2oldv = vec_ld( 0, p_s2 );
1012 while( p_dest < p_end )
1014 s1newv = vec_ld( 16, p_s1 );
1015 s2newv = vec_ld( 16, p_s2 );
1016 s1v = vec_perm( s1oldv, s1newv, perm1v );
1017 s2v = vec_perm( s2oldv, s2newv, perm2v );
1020 destv = vec_avg( s1v, s2v );
1021 vec_st( destv, 0, p_dest );
1030 /* Aligned source */
1031 vector unsigned char s1v, s2v, destv;
1033 while( p_dest < p_end )
1035 s1v = vec_ld( 0, p_s1 );
1036 s2v = vec_ld( 0, p_s2 );
1037 destv = vec_avg( s1v, s2v );
1038 vec_st( destv, 0, p_dest );
1048 while( p_dest < p_end )
1050 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1055 /*****************************************************************************
1056 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1057 * and apply a process to recreate the bottom field :
1058 * If a 8x8 block is classified as :
1059 * - progressive: it applies a small blend (1,6,1)
1061 * * in the MMX version: we do a ME between the 2 fields, if there is a
1062 * good match we use MC to recreate the bottom field (with a small
1064 * * otherwise: it recreates the bottom field by an edge oriented
1066 *****************************************************************************/
1068 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1069 * XXX: It need to access to 8x10
1070 * We use more than 8 lines to help with scrolling (text)
1071 * (and because XDeint8x8Frame use line 9)
1072 * XXX: smooth/uniform area with noise detection doesn't works well
1073 * but it's not really a problem because they don't have much details anyway
1075 static inline int ssd( int a ) { return a*a; }
1076 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1082 /* Detect interlacing */
1084 for( y = 0; y < 7; y += 2 )
1087 for( x = 0; x < 8; x++ )
1089 fr += ssd(src[ x] - src[1*i_src+x]) +
1090 ssd(src[i_src+x] - src[2*i_src+x]);
1091 ff += ssd(src[ x] - src[2*i_src+x]) +
1092 ssd(src[i_src+x] - src[3*i_src+x]);
1094 if( ff < 6*fr/8 && fr > 32 )
1100 return fc < 1 ? VLC_FALSE : VLC_TRUE;
1102 #ifdef CAN_COMPILE_MMXEXT
1103 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1110 /* Detect interlacing */
1112 pxor_r2r( mm7, mm7 );
1113 for( y = 0; y < 9; y += 2 )
1116 pxor_r2r( mm5, mm5 );
1117 pxor_r2r( mm6, mm6 );
1118 for( x = 0; x < 8; x+=4 )
1120 movd_m2r( src[ x], mm0 );
1121 movd_m2r( src[1*i_src+x], mm1 );
1122 movd_m2r( src[2*i_src+x], mm2 );
1123 movd_m2r( src[3*i_src+x], mm3 );
1125 punpcklbw_r2r( mm7, mm0 );
1126 punpcklbw_r2r( mm7, mm1 );
1127 punpcklbw_r2r( mm7, mm2 );
1128 punpcklbw_r2r( mm7, mm3 );
1130 movq_r2r( mm0, mm4 );
1132 psubw_r2r( mm1, mm0 );
1133 psubw_r2r( mm2, mm4 );
1135 psubw_r2r( mm1, mm2 );
1136 psubw_r2r( mm1, mm3 );
1138 pmaddwd_r2r( mm0, mm0 );
1139 pmaddwd_r2r( mm4, mm4 );
1140 pmaddwd_r2r( mm2, mm2 );
1141 pmaddwd_r2r( mm3, mm3 );
1142 paddd_r2r( mm0, mm2 );
1143 paddd_r2r( mm4, mm3 );
1144 paddd_r2r( mm2, mm5 );
1145 paddd_r2r( mm3, mm6 );
1148 movq_r2r( mm5, mm0 );
1149 psrlq_i2r( 32, mm0 );
1150 paddd_r2r( mm0, mm5 );
1151 movd_r2m( mm5, fr );
1153 movq_r2r( mm6, mm0 );
1154 psrlq_i2r( 32, mm0 );
1155 paddd_r2r( mm0, mm6 );
1156 movd_r2m( mm6, ff );
1158 if( ff < 6*fr/8 && fr > 32 )
1167 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1168 * This won't destroy details, and help if there is a bit of interlacing.
1169 * (It helps with paning to avoid flickers)
1173 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1174 uint8_t *src, int i_src )
1179 for( y = 0; y < 8; y += 2 )
1181 memcpy( dst, src, 8 );
1184 for( x = 0; x < 8; x++ )
1185 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1191 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1192 uint8_t *src1, int i_src1,
1193 uint8_t *src2, int i_src2 )
1198 for( y = 0; y < 8; y += 2 )
1200 memcpy( dst, src1, 8 );
1203 for( x = 0; x < 8; x++ )
1204 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1212 #ifdef CAN_COMPILE_MMXEXT
1213 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1214 uint8_t *src1, int i_src1,
1215 uint8_t *src2, int i_src2 )
1217 static const uint64_t m_4 = I64C(0x0004000400040004);
1221 pxor_r2r( mm7, mm7 );
1222 for( y = 0; y < 8; y += 2 )
1224 for( x = 0; x < 8; x +=4 )
1226 movd_m2r( src1[x], mm0 );
1227 movd_r2m( mm0, dst[x] );
1229 movd_m2r( src2[x], mm1 );
1230 movd_m2r( src1[i_src1+x], mm2 );
1232 punpcklbw_r2r( mm7, mm0 );
1233 punpcklbw_r2r( mm7, mm1 );
1234 punpcklbw_r2r( mm7, mm2 );
1235 paddw_r2r( mm1, mm1 );
1236 movq_r2r( mm1, mm3 );
1237 paddw_r2r( mm3, mm3 );
1238 paddw_r2r( mm2, mm0 );
1239 paddw_r2r( mm3, mm1 );
1240 paddw_m2r( m_4, mm1 );
1241 paddw_r2r( mm1, mm0 );
1242 psraw_i2r( 3, mm0 );
1243 packuswb_r2r( mm7, mm0 );
1244 movd_r2m( mm0, dst[i_dst+x] );
1255 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1258 for( y = 0; y < 8; y++ )
1259 memset( &dst[y*i_dst], v, 8 );
1262 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1265 * TODO: a better one for the inner part.
1267 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1268 uint8_t *src, int i_src )
1273 for( y = 0; y < 8; y += 2 )
1275 memcpy( dst, src, 8 );
1278 for( x = 0; x < 8; x++ )
1279 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1284 #ifdef CAN_COMPILE_MMXEXT
1285 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1286 uint8_t *src, int i_src )
1291 for( y = 0; y < 8; y += 2 )
1293 movq_m2r( src[0], mm0 );
1294 movq_r2m( mm0, dst[0] );
1297 movq_m2r( src[2*i_src], mm1 );
1298 pavgb_r2r( mm1, mm0 );
1300 movq_r2m( mm0, dst[0] );
1308 /* XDeint8x8Field: Edge oriented interpolation
1309 * (Need -4 and +5 pixels H, +1 line)
1311 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1312 uint8_t *src, int i_src )
1317 for( y = 0; y < 8; y += 2 )
1319 memcpy( dst, src, 8 );
1322 for( x = 0; x < 8; x++ )
1324 uint8_t *src2 = &src[2*i_src];
1325 /* I use 8 pixels just to match the MMX version, but it's overkill
1326 * 5 would be enough (less isn't good) */
1327 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1328 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1329 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1330 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1332 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1333 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1334 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1335 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1337 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1338 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1339 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1340 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1342 if( c0 < c1 && c1 <= c2 )
1343 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1344 else if( c2 < c1 && c1 <= c0 )
1345 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1347 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1354 #ifdef CAN_COMPILE_MMXEXT
1355 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1356 uint8_t *src, int i_src )
1361 for( y = 0; y < 8; y += 2 )
1363 memcpy( dst, src, 8 );
1366 for( x = 0; x < 8; x++ )
1368 uint8_t *src2 = &src[2*i_src];
1371 movq_m2r( src[x-2], mm0 );
1372 movq_m2r( src[x-3], mm1 );
1373 movq_m2r( src[x-4], mm2 );
1375 psadbw_m2r( src2[x-4], mm0 );
1376 psadbw_m2r( src2[x-3], mm1 );
1377 psadbw_m2r( src2[x-2], mm2 );
1379 movd_r2m( mm0, c2 );
1380 movd_r2m( mm1, c1 );
1381 movd_r2m( mm2, c0 );
1383 if( c0 < c1 && c1 <= c2 )
1384 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1385 else if( c2 < c1 && c1 <= c0 )
1386 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1388 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1398 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1399 uint8_t *pix2, int i_pix2 )
1404 for( y = 0; y < 8; y++ )
1405 for( x = 0; x < 8; x++ )
1406 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1410 #ifdef CAN_COMPILE_MMXEXT
1411 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1412 uint8_t *pix2, int i_pix2 )
1417 pxor_r2r( mm7, mm7 );
1418 pxor_r2r( mm6, mm6 );
1420 for( y = 0; y < 8; y++ )
1422 movq_m2r( pix1[0], mm0 );
1423 movq_m2r( pix2[0], mm1 );
1425 movq_r2r( mm0, mm2 );
1426 movq_r2r( mm1, mm3 );
1428 punpcklbw_r2r( mm7, mm0 );
1429 punpckhbw_r2r( mm7, mm2 );
1430 punpcklbw_r2r( mm7, mm1 );
1431 punpckhbw_r2r( mm7, mm3 );
1433 psubw_r2r( mm1, mm0 );
1434 psubw_r2r( mm3, mm2 );
1436 pmaddwd_r2r( mm0, mm0 );
1437 pmaddwd_r2r( mm2, mm2 );
1439 paddd_r2r( mm2, mm0 );
1440 paddd_r2r( mm0, mm6 );
1446 movq_r2r( mm6, mm7 );
1447 psrlq_i2r( 32, mm7 );
1448 paddd_r2r( mm6, mm7 );
1457 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1458 #ifdef CAN_COMPILE_MMXEXT
1461 * TODO: mmx version (easier in sse2)
1463 static inline void XDeintMC( uint8_t *dst, int i_dst,
1464 uint8_t *src, int i_src,
1466 int i_width, int i_height )
1468 const int d4x = mvx&0x03;
1469 const int d4y = mvy&0x03;
1471 const int cA = (4-d4x)*(4-d4y);
1472 const int cB = d4x *(4-d4y);
1473 const int cC = (4-d4x)*d4y;
1474 const int cD = d4x *d4y;
1480 src += (mvy >> 2) * i_src + (mvx >> 2);
1483 for( y = 0; y < i_height; y++ )
1485 for( x = 0; x < i_width; x++ )
1487 dst[x] = ( cA*src[x] + cB*src[x+1] +
1488 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1496 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1497 uint8_t *pix2, int i_pix2 )
1501 movq_m2r( pix1[0*i_pix1], mm0 );
1502 movq_m2r( pix1[1*i_pix1], mm1 );
1504 psadbw_m2r( pix2[0*i_pix2], mm0 );
1505 psadbw_m2r( pix2[1*i_pix2], mm1 );
1507 movq_m2r( pix1[2*i_pix1], mm2 );
1508 movq_m2r( pix1[3*i_pix1], mm3 );
1509 psadbw_m2r( pix2[2*i_pix2], mm2 );
1510 psadbw_m2r( pix2[3*i_pix2], mm3 );
1512 paddd_r2r( mm1, mm0 );
1513 paddd_r2r( mm3, mm2 );
1514 paddd_r2r( mm2, mm0 );
1520 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1521 uint8_t *ref, int i_stride,
1523 int xmax, int ymax )
1525 uint8_t buffer[8*4];
1527 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1530 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1531 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1533 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1534 uint8_t *ref, int i_stride,
1536 int xmax, int ymax )
1538 if( abs(mx) >= xmax || abs(my) >= ymax )
1541 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1544 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1545 uint8_t *src, int i_src,
1547 int xmax, int ymax )
1549 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1550 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1551 uint8_t *next = &src[i_src];
1552 const int i_src2 = 2*i_src;
1557 uint8_t *rec = &dst[i_dst];
1559 /* We construct with intra method the missing field */
1560 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1562 /* Now we will try to find a match with ME with the other field */
1564 /* ME: A small/partial EPZS
1565 * We search only for small MV (with high motion intra will be perfect */
1566 if( xmax > 4 ) xmax = 4;
1567 if( ymax > 4 ) ymax = 4;
1569 /* Init with NULL Mv */
1571 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1573 /* Try predicted Mv */
1574 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1580 /* Search interger pel (small mv) */
1581 for( i_step = 0; i_step < 4; i_step++ )
1587 for( i = 0; i < 4; i++ )
1589 s = XDeint8x4TestInt( rec, i_src2,
1590 next, i_src2, mvx+dx[i], mvy+dy[i],
1610 if( mvs > 4 && mvs < 256 )
1613 /* XXX: for now only HPEL (too slow) */
1614 for( i_step = 0; i_step < 4; i_step++ )
1620 for( i = 0; i < 8; i++ )
1622 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1623 mvx+dx[i], mvy+dy[i],
1641 uint8_t buffer[8*4];
1642 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1643 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1645 //XDeint8x8Set( dst, i_dst, 0 );
1652 /* Kernel interpolation (1,-5,20,20,-5,1)
1653 * Loose a bit more details+add aliasing than edge interpol but avoid
1656 static inline uint8_t clip1( int a )
1665 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1666 uint8_t *src, int i_src )
1671 for( y = 0; y < 8; y += 2 )
1673 const int i_src2 = i_src*2;
1675 memcpy( dst, src, 8 );
1678 for( x = 0; x < 8; x++ )
1682 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1683 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1684 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1686 dst[x] = clip1( ( pix + 16 ) >> 5 );
1696 /* NxN arbitray size (and then only use pixel in the NxN block)
1698 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1699 int i_height, int i_width )
1706 /* Detect interlacing */
1707 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1710 for( y = 0; y < i_height - 2; y += 2 )
1712 const uint8_t *s = &src[y*i_src];
1713 for( x = 0; x < i_width; x++ )
1715 fr += ssd(s[ x] - s[1*i_src+x]);
1716 ff += ssd(s[ x] - s[2*i_src+x]);
1718 if( ff < fr && fr > i_width / 2 )
1722 return fc < 2 ? VLC_FALSE : VLC_TRUE;
1725 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1726 uint8_t *src, int i_src,
1727 int i_width, int i_height )
1732 for( y = 0; y < i_height; y += 2 )
1734 memcpy( dst, src, i_width );
1737 if( y < i_height - 2 )
1739 for( x = 0; x < i_width; x++ )
1740 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1744 /* Blend last line */
1745 for( x = 0; x < i_width; x++ )
1746 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1753 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1754 uint8_t *src, int i_src,
1755 int i_width, int i_height )
1760 for( y = 0; y < i_height; y += 2 )
1762 memcpy( dst, src, i_width );
1765 if( y < i_height - 2 )
1767 for( x = 0; x < i_width; x++ )
1768 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1772 /* Blend last line */
1773 for( x = 0; x < i_width; x++ )
1774 dst[x] = (src[x] + src[i_src+x]) >> 1;
1781 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1782 int i_width, int i_height )
1784 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1785 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1787 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1791 static inline int median( int a, int b, int c )
1793 int min = a, max =a;
1804 return a + b + c - min - max;
1810 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1811 uint8_t *src, int i_src,
1812 const int i_mbx, int i_modx )
1816 for( x = 0; x < i_mbx; x++ )
1819 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1821 if( x == 0 || x == i_mbx - 1 )
1822 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1824 XDeint8x8FieldC( dst, i_dst, src, i_src );
1828 XDeint8x8MergeC( dst, i_dst,
1829 &src[0*i_src], 2*i_src,
1830 &src[1*i_src], 2*i_src );
1838 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1840 #ifdef CAN_COMPILE_MMXEXT
1841 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1842 uint8_t *src, int i_src,
1843 const int i_mbx, int i_modx )
1847 /* Reset current line */
1848 for( x = 0; x < i_mbx; x++ )
1851 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1853 if( x == 0 || x == i_mbx - 1 )
1854 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1856 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1860 XDeint8x8MergeMMXEXT( dst, i_dst,
1861 &src[0*i_src], 2*i_src,
1862 &src[1*i_src], 2*i_src );
1870 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1874 static void RenderX( vout_thread_t *p_vout,
1875 picture_t *p_outpic, picture_t *p_pic )
1879 /* Copy image and skip lines */
1880 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1882 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1883 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1885 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1886 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1888 const int i_dst = p_outpic->p[i_plane].i_pitch;
1889 const int i_src = p_pic->p[i_plane].i_pitch;
1893 for( y = 0; y < i_mby; y++ )
1895 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1896 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1898 #ifdef CAN_COMPILE_MMXEXT
1899 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
1900 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1903 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1906 /* Last line (C only)*/
1909 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1910 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1912 for( x = 0; x < i_mbx; x++ )
1914 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1921 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1925 #ifdef CAN_COMPILE_MMXEXT
1926 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
1931 /*****************************************************************************
1932 * SendEvents: forward mouse and keyboard events to the parent p_vout
1933 *****************************************************************************/
1934 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1935 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1937 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1938 vlc_value_t sentval = newval;
1940 if( !strcmp( psz_var, "mouse-y" ) )
1942 switch( p_vout->p_sys->i_mode )
1944 case DEINTERLACE_MEAN:
1945 case DEINTERLACE_DISCARD:
1951 var_Set( p_vout, psz_var, sentval );
1956 /*****************************************************************************
1957 * FilterCallback: called when changing the deinterlace method on the fly.
1958 *****************************************************************************/
1959 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1960 vlc_value_t oldval, vlc_value_t newval,
1963 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1964 int i_old_mode = p_vout->p_sys->i_mode;
1966 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1968 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1970 SetFilterMethod( p_vout, newval.psz_string );
1972 switch( p_vout->render.i_chroma )
1974 case VLC_FOURCC('I','4','2','2'):
1975 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1979 case VLC_FOURCC('I','4','2','0'):
1980 case VLC_FOURCC('I','Y','U','V'):
1981 case VLC_FOURCC('Y','V','1','2'):
1982 switch( p_vout->p_sys->i_mode )
1984 case DEINTERLACE_MEAN:
1985 case DEINTERLACE_DISCARD:
1986 if( ( i_old_mode == DEINTERLACE_MEAN )
1987 || ( i_old_mode == DEINTERLACE_DISCARD ) )
1989 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1994 case DEINTERLACE_BOB:
1995 case DEINTERLACE_BLEND:
1996 case DEINTERLACE_LINEAR:
1997 if( ( i_old_mode == DEINTERLACE_BOB )
1998 || ( i_old_mode == DEINTERLACE_BLEND )
1999 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2001 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2012 /* We need to kill the old vout */
2014 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2016 vlc_object_detach( p_vout->p_sys->p_vout );
2017 vout_Destroy( p_vout->p_sys->p_vout );
2019 /* Try to open a new video output */
2020 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2022 if( p_vout->p_sys->p_vout == NULL )
2024 /* Everything failed */
2025 msg_Err( p_vout, "cannot open vout, aborting" );
2027 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2028 return VLC_EGENERIC;
2031 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2033 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2037 /*****************************************************************************
2038 * SendEventsToChild: forward events to the child/children vout
2039 *****************************************************************************/
2040 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2041 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2043 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2044 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2049 /*****************************************************************************
2050 * video filter2 functions
2051 *****************************************************************************/
2052 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2054 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2055 picture_t *p_pic_dst;
2057 /* Request output picture */
2058 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
2059 if( p_pic_dst == NULL )
2061 msg_Warn( p_filter, "can't get output picture" );
2065 switch( p_vout->p_sys->i_mode )
2067 case DEINTERLACE_DISCARD:
2069 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2071 msg_Err( p_vout, "discarding lines is not supported yet" );
2072 p_pic_dst->pf_release( p_pic_dst );
2076 case DEINTERLACE_BOB:
2078 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2079 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2083 case DEINTERLACE_LINEAR:
2085 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2086 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2088 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2089 p_pic_dst->pf_release( p_pic_dst );
2093 case DEINTERLACE_MEAN:
2094 RenderMean( p_vout, p_pic_dst, p_pic );
2097 case DEINTERLACE_BLEND:
2098 RenderBlend( p_vout, p_pic_dst, p_pic );
2102 RenderX( p_vout, p_pic_dst, p_pic );
2106 p_pic_dst->date = p_pic->date;
2107 p_pic_dst->b_force = p_pic->b_force;
2108 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2109 p_pic_dst->b_progressive = VLC_TRUE;
2110 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2112 p_pic->pf_release( p_pic );
2116 /*****************************************************************************
2118 *****************************************************************************/
2119 static int OpenFilter( vlc_object_t *p_this )
2121 filter_t *p_filter = (filter_t*)p_this;
2122 vout_thread_t *p_vout;
2125 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2126 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2127 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2128 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2130 return VLC_EGENERIC;
2133 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2135 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2136 vlc_object_attach( p_vout, p_filter );
2137 p_filter->p_sys = (filter_sys_t *)p_vout;
2138 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2140 sout_CfgParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2142 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2143 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2144 var_Set( p_filter, "deinterlace-mode", val );
2146 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2148 vlc_object_detach( p_vout );
2149 vlc_object_release( p_vout );
2150 return VLC_EGENERIC;
2153 p_filter->pf_video_filter = Deinterlace;
2155 msg_Dbg( p_filter, "deinterlacing" );
2160 /*****************************************************************************
2161 * CloseFilter: clean up the filter
2162 *****************************************************************************/
2163 static void CloseFilter( vlc_object_t *p_this )
2165 filter_t *p_filter = (filter_t*)p_this;
2166 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2168 Destroy( VLC_OBJECT(p_vout) );
2169 vlc_object_detach( p_vout );
2170 vlc_object_release( p_vout );