1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
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"), N_("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 = 0;
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 = 0;
258 else if( !strcmp( psz_method, "mean" ) )
260 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
261 p_vout->p_sys->b_double_rate = 0;
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 = 0;
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 = 1;
276 else if( !strcmp( psz_method, "linear" ) )
278 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
279 p_vout->p_sys->b_double_rate = 1;
281 else if( !strcmp( psz_method, "x" ) )
283 p_vout->p_sys->i_mode = DEINTERLACE_X;
284 p_vout->p_sys->b_double_rate = 0;
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 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
447 /* Get a new picture */
448 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
452 if( p_vout->b_die || p_vout->b_error )
454 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
457 msleep( VOUT_OUTMEM_SLEEP );
460 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
462 /* If we are using double rate, get an additional new picture */
463 if( p_vout->p_sys->b_double_rate )
465 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
469 if( p_vout->b_die || p_vout->b_error )
471 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
472 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
475 msleep( VOUT_OUTMEM_SLEEP );
478 /* 20ms is a bit arbitrary, but it's only for the first image we get */
479 if( !p_vout->p_sys->last_date )
481 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
482 p_pic->date + 20000 );
486 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
487 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
489 p_vout->p_sys->last_date = p_pic->date;
492 switch( p_vout->p_sys->i_mode )
494 case DEINTERLACE_DISCARD:
495 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
496 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
499 case DEINTERLACE_BOB:
500 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
501 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
502 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
503 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
506 case DEINTERLACE_LINEAR:
507 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
508 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
509 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
510 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
513 case DEINTERLACE_MEAN:
514 RenderMean( p_vout, pp_outpic[0], p_pic );
515 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
518 case DEINTERLACE_BLEND:
519 RenderBlend( p_vout, pp_outpic[0], p_pic );
520 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
524 RenderX( p_vout, pp_outpic[0], p_pic );
525 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
528 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
531 /*****************************************************************************
532 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
533 *****************************************************************************/
534 static void RenderDiscard( vout_thread_t *p_vout,
535 picture_t *p_outpic, picture_t *p_pic, int i_field )
539 /* Copy image and skip lines */
540 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
542 uint8_t *p_in, *p_out_end, *p_out;
545 p_in = p_pic->p[i_plane].p_pixels
546 + i_field * p_pic->p[i_plane].i_pitch;
548 p_out = p_outpic->p[i_plane].p_pixels;
549 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
550 * p_outpic->p[i_plane].i_visible_lines;
552 switch( p_vout->render.i_chroma )
554 case VLC_FOURCC('I','4','2','0'):
555 case VLC_FOURCC('I','Y','U','V'):
556 case VLC_FOURCC('Y','V','1','2'):
558 for( ; p_out < p_out_end ; )
560 p_vout->p_vlc->pf_memcpy( p_out, p_in,
561 p_pic->p[i_plane].i_pitch );
563 p_out += p_pic->p[i_plane].i_pitch;
564 p_in += 2 * p_pic->p[i_plane].i_pitch;
568 case VLC_FOURCC('I','4','2','2'):
570 i_increment = 2 * p_pic->p[i_plane].i_pitch;
572 if( i_plane == Y_PLANE )
574 for( ; p_out < p_out_end ; )
576 p_vout->p_vlc->pf_memcpy( p_out, p_in,
577 p_pic->p[i_plane].i_pitch );
578 p_out += p_pic->p[i_plane].i_pitch;
579 p_vout->p_vlc->pf_memcpy( p_out, p_in,
580 p_pic->p[i_plane].i_pitch );
581 p_out += p_pic->p[i_plane].i_pitch;
587 for( ; p_out < p_out_end ; )
589 p_vout->p_vlc->pf_memcpy( p_out, p_in,
590 p_pic->p[i_plane].i_pitch );
591 p_out += p_pic->p[i_plane].i_pitch;
603 /*****************************************************************************
604 * RenderBob: renders a BOB picture - simple copy
605 *****************************************************************************/
606 static void RenderBob( vout_thread_t *p_vout,
607 picture_t *p_outpic, picture_t *p_pic, int i_field )
611 /* Copy image and skip lines */
612 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
614 uint8_t *p_in, *p_out_end, *p_out;
616 p_in = p_pic->p[i_plane].p_pixels;
617 p_out = p_outpic->p[i_plane].p_pixels;
618 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
619 * p_outpic->p[i_plane].i_visible_lines;
621 switch( p_vout->render.i_chroma )
623 case VLC_FOURCC('I','4','2','0'):
624 case VLC_FOURCC('I','Y','U','V'):
625 case VLC_FOURCC('Y','V','1','2'):
626 /* For BOTTOM field we need to add the first line */
629 p_vout->p_vlc->pf_memcpy( p_out, p_in,
630 p_pic->p[i_plane].i_pitch );
631 p_in += p_pic->p[i_plane].i_pitch;
632 p_out += p_pic->p[i_plane].i_pitch;
635 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
637 for( ; p_out < p_out_end ; )
639 p_vout->p_vlc->pf_memcpy( p_out, p_in,
640 p_pic->p[i_plane].i_pitch );
642 p_out += p_pic->p[i_plane].i_pitch;
644 p_vout->p_vlc->pf_memcpy( p_out, p_in,
645 p_pic->p[i_plane].i_pitch );
647 p_in += 2 * p_pic->p[i_plane].i_pitch;
648 p_out += p_pic->p[i_plane].i_pitch;
651 p_vout->p_vlc->pf_memcpy( p_out, p_in,
652 p_pic->p[i_plane].i_pitch );
654 /* For TOP field we need to add the last line */
657 p_in += p_pic->p[i_plane].i_pitch;
658 p_out += p_pic->p[i_plane].i_pitch;
659 p_vout->p_vlc->pf_memcpy( p_out, p_in,
660 p_pic->p[i_plane].i_pitch );
664 case VLC_FOURCC('I','4','2','2'):
665 /* For BOTTOM field we need to add the first line */
668 p_vout->p_vlc->pf_memcpy( p_out, p_in,
669 p_pic->p[i_plane].i_pitch );
670 p_in += p_pic->p[i_plane].i_pitch;
671 p_out += p_pic->p[i_plane].i_pitch;
674 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
676 if( i_plane == Y_PLANE )
678 for( ; p_out < p_out_end ; )
680 p_vout->p_vlc->pf_memcpy( p_out, p_in,
681 p_pic->p[i_plane].i_pitch );
683 p_out += p_pic->p[i_plane].i_pitch;
685 p_vout->p_vlc->pf_memcpy( p_out, p_in,
686 p_pic->p[i_plane].i_pitch );
688 p_in += 2 * p_pic->p[i_plane].i_pitch;
689 p_out += p_pic->p[i_plane].i_pitch;
694 for( ; p_out < p_out_end ; )
696 p_vout->p_vlc->pf_memcpy( p_out, p_in,
697 p_pic->p[i_plane].i_pitch );
699 p_out += p_pic->p[i_plane].i_pitch;
700 p_in += 2 * p_pic->p[i_plane].i_pitch;
704 p_vout->p_vlc->pf_memcpy( p_out, p_in,
705 p_pic->p[i_plane].i_pitch );
707 /* For TOP field we need to add the last line */
710 p_in += p_pic->p[i_plane].i_pitch;
711 p_out += p_pic->p[i_plane].i_pitch;
712 p_vout->p_vlc->pf_memcpy( p_out, p_in,
713 p_pic->p[i_plane].i_pitch );
720 #define Merge p_vout->p_sys->pf_merge
721 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
723 /*****************************************************************************
724 * RenderLinear: BOB with linear interpolation
725 *****************************************************************************/
726 static void RenderLinear( vout_thread_t *p_vout,
727 picture_t *p_outpic, picture_t *p_pic, int i_field )
731 /* Copy image and skip lines */
732 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
734 uint8_t *p_in, *p_out_end, *p_out;
736 p_in = p_pic->p[i_plane].p_pixels;
737 p_out = p_outpic->p[i_plane].p_pixels;
738 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
739 * p_outpic->p[i_plane].i_visible_lines;
741 /* For BOTTOM field we need to add the first line */
744 p_vout->p_vlc->pf_memcpy( p_out, p_in,
745 p_pic->p[i_plane].i_pitch );
746 p_in += p_pic->p[i_plane].i_pitch;
747 p_out += p_pic->p[i_plane].i_pitch;
750 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
752 for( ; p_out < p_out_end ; )
754 p_vout->p_vlc->pf_memcpy( p_out, p_in,
755 p_pic->p[i_plane].i_pitch );
757 p_out += p_pic->p[i_plane].i_pitch;
759 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
760 p_pic->p[i_plane].i_pitch );
762 p_in += 2 * p_pic->p[i_plane].i_pitch;
763 p_out += p_pic->p[i_plane].i_pitch;
766 p_vout->p_vlc->pf_memcpy( p_out, p_in,
767 p_pic->p[i_plane].i_pitch );
769 /* For TOP field we need to add the last line */
772 p_in += p_pic->p[i_plane].i_pitch;
773 p_out += p_pic->p[i_plane].i_pitch;
774 p_vout->p_vlc->pf_memcpy( p_out, p_in,
775 p_pic->p[i_plane].i_pitch );
781 static void RenderMean( vout_thread_t *p_vout,
782 picture_t *p_outpic, picture_t *p_pic )
786 /* Copy image and skip lines */
787 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
789 uint8_t *p_in, *p_out_end, *p_out;
791 p_in = p_pic->p[i_plane].p_pixels;
793 p_out = p_outpic->p[i_plane].p_pixels;
794 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
795 * p_outpic->p[i_plane].i_visible_lines;
797 /* All lines: mean value */
798 for( ; p_out < p_out_end ; )
800 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
801 p_pic->p[i_plane].i_pitch );
803 p_out += p_pic->p[i_plane].i_pitch;
804 p_in += 2 * p_pic->p[i_plane].i_pitch;
810 static void RenderBlend( vout_thread_t *p_vout,
811 picture_t *p_outpic, picture_t *p_pic )
815 /* Copy image and skip lines */
816 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
818 uint8_t *p_in, *p_out_end, *p_out;
820 p_in = p_pic->p[i_plane].p_pixels;
822 p_out = p_outpic->p[i_plane].p_pixels;
823 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
824 * p_outpic->p[i_plane].i_visible_lines;
826 switch( p_vout->render.i_chroma )
828 case VLC_FOURCC('I','4','2','0'):
829 case VLC_FOURCC('I','Y','U','V'):
830 case VLC_FOURCC('Y','V','1','2'):
831 /* First line: simple copy */
832 p_vout->p_vlc->pf_memcpy( p_out, p_in,
833 p_pic->p[i_plane].i_pitch );
834 p_out += p_pic->p[i_plane].i_pitch;
836 /* Remaining lines: mean value */
837 for( ; p_out < p_out_end ; )
839 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
840 p_pic->p[i_plane].i_pitch );
842 p_out += p_pic->p[i_plane].i_pitch;
843 p_in += p_pic->p[i_plane].i_pitch;
847 case VLC_FOURCC('I','4','2','2'):
848 /* First line: simple copy */
849 p_vout->p_vlc->pf_memcpy( p_out, p_in,
850 p_pic->p[i_plane].i_pitch );
851 p_out += p_pic->p[i_plane].i_pitch;
853 /* Remaining lines: mean value */
854 if( i_plane == Y_PLANE )
856 for( ; p_out < p_out_end ; )
858 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
859 p_pic->p[i_plane].i_pitch );
861 p_out += p_pic->p[i_plane].i_pitch;
862 p_in += p_pic->p[i_plane].i_pitch;
868 for( ; p_out < p_out_end ; )
870 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
871 p_pic->p[i_plane].i_pitch );
873 p_out += p_pic->p[i_plane].i_pitch;
874 p_in += 2*p_pic->p[i_plane].i_pitch;
885 static void MergeGeneric( void *_p_dest, const void *_p_s1,
886 const void *_p_s2, size_t i_bytes )
888 uint8_t* p_dest = (uint8_t*)_p_dest;
889 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
890 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
891 uint8_t* p_end = p_dest + i_bytes - 8;
893 while( p_dest < p_end )
895 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
896 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
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;
907 while( p_dest < p_end )
909 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
913 #if defined(CAN_COMPILE_MMXEXT)
914 static void MergeMMX( void *_p_dest, const void *_p_s1, const void *_p_s2,
917 uint8_t* p_dest = (uint8_t*)_p_dest;
918 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
919 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
920 uint8_t* p_end = p_dest + i_bytes - 8;
921 while( p_dest < p_end )
923 __asm__ __volatile__( "movq %2,%%mm1;"
925 "movq %%mm1, %0" :"=m" (*p_dest):
935 while( p_dest < p_end )
937 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
942 #if defined(CAN_COMPILE_SSE)
943 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
946 uint8_t* p_dest = (uint8_t*)_p_dest;
947 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
948 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
950 while( (int)p_s1 % 16 )
952 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
954 p_end = p_dest + i_bytes - 16;
955 while( p_dest < p_end )
957 __asm__ __volatile__( "movdqu %2,%%xmm1;"
959 "movdqu %%xmm1, %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_MMXEXT) || defined(CAN_COMPILE_SSE)
977 static void EndMMX( void )
979 __asm__ __volatile__( "emms" :: );
983 #ifdef CAN_COMPILE_C_ALTIVEC
984 static void MergeAltivec( void *_p_dest, const void *_p_s1,
985 const void *_p_s2, size_t i_bytes )
987 uint8_t *p_dest = (uint8_t *)_p_dest;
988 uint8_t *p_s1 = (uint8_t *)_p_s1;
989 uint8_t *p_s2 = (uint8_t *)_p_s2;
990 uint8_t *p_end = p_dest + i_bytes - 15;
992 /* Use C until the first 16-bytes aligned destination pixel */
993 while( (int)p_dest & 0xF )
995 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
998 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1000 /* Unaligned source */
1001 vector unsigned char s1v, s2v, destv;
1002 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1003 vector unsigned char perm1v, perm2v;
1005 perm1v = vec_lvsl( 0, p_s1 );
1006 perm2v = vec_lvsl( 0, p_s2 );
1007 s1oldv = vec_ld( 0, p_s1 );
1008 s2oldv = vec_ld( 0, p_s2 );
1010 while( p_dest < p_end )
1012 s1newv = vec_ld( 16, p_s1 );
1013 s2newv = vec_ld( 16, p_s2 );
1014 s1v = vec_perm( s1oldv, s1newv, perm1v );
1015 s2v = vec_perm( s2oldv, s2newv, perm2v );
1018 destv = vec_avg( s1v, s2v );
1019 vec_st( destv, 0, p_dest );
1028 /* Aligned source */
1029 vector unsigned char s1v, s2v, destv;
1031 while( p_dest < p_end )
1033 s1v = vec_ld( 0, p_s1 );
1034 s2v = vec_ld( 0, p_s2 );
1035 destv = vec_avg( s1v, s2v );
1036 vec_st( destv, 0, p_dest );
1046 while( p_dest < p_end )
1048 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1053 /*****************************************************************************
1054 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1055 * and apply a process to recreate the bottom field :
1056 * If a 8x8 block is classified as :
1057 * - progressive: it applies a small blend (1,6,1)
1059 * * in the MMX version: we do a ME between the 2 fields, if there is a
1060 * good match we use MC to recreate the bottom field (with a small
1062 * * otherwise: it recreates the bottom field by an edge oriented
1064 *****************************************************************************/
1066 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1067 * XXX: It need to access to 8x10
1068 * We use more than 8 lines to help with scrolling (text)
1069 * (and because XDeint8x8Frame use line 9)
1070 * XXX: smooth/uniform area with noise detection doesn't works well
1071 * but it's not really a problem because they don't have much details anyway
1073 static inline int ssd( int a ) { return a*a; }
1074 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1080 /* Detect interlacing */
1082 for( y = 0; y < 7; y += 2 )
1085 for( x = 0; x < 8; x++ )
1087 fr += ssd(src[ x] - src[1*i_src+x]) +
1088 ssd(src[i_src+x] - src[2*i_src+x]);
1089 ff += ssd(src[ x] - src[2*i_src+x]) +
1090 ssd(src[i_src+x] - src[3*i_src+x]);
1092 if( ff < 6*fr/8 && fr > 32 )
1098 return fc < 1 ? VLC_FALSE : VLC_TRUE;
1100 #ifdef CAN_COMPILE_MMXEXT
1101 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1108 /* Detect interlacing */
1110 pxor_r2r( mm7, mm7 );
1111 for( y = 0; y < 9; y += 2 )
1114 pxor_r2r( mm5, mm5 );
1115 pxor_r2r( mm6, mm6 );
1116 for( x = 0; x < 8; x+=4 )
1118 movd_m2r( src[ x], mm0 );
1119 movd_m2r( src[1*i_src+x], mm1 );
1120 movd_m2r( src[2*i_src+x], mm2 );
1121 movd_m2r( src[3*i_src+x], mm3 );
1123 punpcklbw_r2r( mm7, mm0 );
1124 punpcklbw_r2r( mm7, mm1 );
1125 punpcklbw_r2r( mm7, mm2 );
1126 punpcklbw_r2r( mm7, mm3 );
1128 movq_r2r( mm0, mm4 );
1130 psubw_r2r( mm1, mm0 );
1131 psubw_r2r( mm2, mm4 );
1133 psubw_r2r( mm1, mm2 );
1134 psubw_r2r( mm1, mm3 );
1136 pmaddwd_r2r( mm0, mm0 );
1137 pmaddwd_r2r( mm4, mm4 );
1138 pmaddwd_r2r( mm2, mm2 );
1139 pmaddwd_r2r( mm3, mm3 );
1140 paddd_r2r( mm0, mm2 );
1141 paddd_r2r( mm4, mm3 );
1142 paddd_r2r( mm2, mm5 );
1143 paddd_r2r( mm3, mm6 );
1146 movq_r2r( mm5, mm0 );
1147 psrlq_i2r( 32, mm0 );
1148 paddd_r2r( mm0, mm5 );
1149 movd_r2m( mm5, fr );
1151 movq_r2r( mm6, mm0 );
1152 psrlq_i2r( 32, mm0 );
1153 paddd_r2r( mm0, mm6 );
1154 movd_r2m( mm6, ff );
1156 if( ff < 6*fr/8 && fr > 32 )
1165 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1166 * This won't destroy details, and help if there is a bit of interlacing.
1167 * (It helps with paning to avoid flickers)
1171 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1172 uint8_t *src, int i_src )
1177 for( y = 0; y < 8; y += 2 )
1179 memcpy( dst, src, 8 );
1182 for( x = 0; x < 8; x++ )
1183 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1189 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1190 uint8_t *src1, int i_src1,
1191 uint8_t *src2, int i_src2 )
1196 for( y = 0; y < 8; y += 2 )
1198 memcpy( dst, src1, 8 );
1201 for( x = 0; x < 8; x++ )
1202 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1210 #ifdef CAN_COMPILE_MMXEXT
1211 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1212 uint8_t *src1, int i_src1,
1213 uint8_t *src2, int i_src2 )
1215 static const uint64_t m_4 = I64C(0x0004000400040004);
1219 pxor_r2r( mm7, mm7 );
1220 for( y = 0; y < 8; y += 2 )
1222 for( x = 0; x < 8; x +=4 )
1224 movd_m2r( src1[x], mm0 );
1225 movd_r2m( mm0, dst[x] );
1227 movd_m2r( src2[x], mm1 );
1228 movd_m2r( src1[i_src1+x], mm2 );
1230 punpcklbw_r2r( mm7, mm0 );
1231 punpcklbw_r2r( mm7, mm1 );
1232 punpcklbw_r2r( mm7, mm2 );
1233 paddw_r2r( mm1, mm1 );
1234 movq_r2r( mm1, mm3 );
1235 paddw_r2r( mm3, mm3 );
1236 paddw_r2r( mm2, mm0 );
1237 paddw_r2r( mm3, mm1 );
1238 paddw_m2r( m_4, mm1 );
1239 paddw_r2r( mm1, mm0 );
1240 psraw_i2r( 3, mm0 );
1241 packuswb_r2r( mm7, mm0 );
1242 movd_r2m( mm0, dst[i_dst+x] );
1253 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1256 for( y = 0; y < 8; y++ )
1257 memset( &dst[y*i_dst], v, 8 );
1260 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1263 * TODO: a better one for the inner part.
1265 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1266 uint8_t *src, int i_src )
1271 for( y = 0; y < 8; y += 2 )
1273 memcpy( dst, src, 8 );
1276 for( x = 0; x < 8; x++ )
1277 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1282 #ifdef CAN_COMPILE_MMXEXT
1283 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1284 uint8_t *src, int i_src )
1289 for( y = 0; y < 8; y += 2 )
1291 movq_m2r( src[0], mm0 );
1292 movq_r2m( mm0, dst[0] );
1295 movq_m2r( src[2*i_src], mm1 );
1296 pavgb_r2r( mm1, mm0 );
1298 movq_r2m( mm0, dst[0] );
1306 /* XDeint8x8Field: Edge oriented interpolation
1307 * (Need -4 and +5 pixels H, +1 line)
1309 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1310 uint8_t *src, int i_src )
1315 for( y = 0; y < 8; y += 2 )
1317 memcpy( dst, src, 8 );
1320 for( x = 0; x < 8; x++ )
1322 uint8_t *src2 = &src[2*i_src];
1323 /* I use 8 pixels just to match the MMX version, but it's overkill
1324 * 5 would be enough (less isn't good) */
1325 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1326 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1327 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1328 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1330 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1331 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1332 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1333 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1335 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1336 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1337 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1338 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1340 if( c0 < c1 && c1 <= c2 )
1341 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1342 else if( c2 < c1 && c1 <= c0 )
1343 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1345 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1352 #ifdef CAN_COMPILE_MMXEXT
1353 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1354 uint8_t *src, int i_src )
1359 for( y = 0; y < 8; y += 2 )
1361 memcpy( dst, src, 8 );
1364 for( x = 0; x < 8; x++ )
1366 uint8_t *src2 = &src[2*i_src];
1369 movq_m2r( src[x-2], mm0 );
1370 movq_m2r( src[x-3], mm1 );
1371 movq_m2r( src[x-4], mm2 );
1373 psadbw_m2r( src2[x-4], mm0 );
1374 psadbw_m2r( src2[x-3], mm1 );
1375 psadbw_m2r( src2[x-2], mm2 );
1377 movd_r2m( mm0, c2 );
1378 movd_r2m( mm1, c1 );
1379 movd_r2m( mm2, c0 );
1381 if( c0 < c1 && c1 <= c2 )
1382 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1383 else if( c2 < c1 && c1 <= c0 )
1384 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1386 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1396 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1397 uint8_t *pix2, int i_pix2 )
1402 for( y = 0; y < 8; y++ )
1403 for( x = 0; x < 8; x++ )
1404 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1408 #ifdef CAN_COMPILE_MMXEXT
1409 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1410 uint8_t *pix2, int i_pix2 )
1415 pxor_r2r( mm7, mm7 );
1416 pxor_r2r( mm6, mm6 );
1418 for( y = 0; y < 8; y++ )
1420 movq_m2r( pix1[0], mm0 );
1421 movq_m2r( pix2[0], mm1 );
1423 movq_r2r( mm0, mm2 );
1424 movq_r2r( mm1, mm3 );
1426 punpcklbw_r2r( mm7, mm0 );
1427 punpckhbw_r2r( mm7, mm2 );
1428 punpcklbw_r2r( mm7, mm1 );
1429 punpckhbw_r2r( mm7, mm3 );
1431 psubw_r2r( mm1, mm0 );
1432 psubw_r2r( mm3, mm2 );
1434 pmaddwd_r2r( mm0, mm0 );
1435 pmaddwd_r2r( mm2, mm2 );
1437 paddd_r2r( mm2, mm0 );
1438 paddd_r2r( mm0, mm6 );
1444 movq_r2r( mm6, mm7 );
1445 psrlq_i2r( 32, mm7 );
1446 paddd_r2r( mm6, mm7 );
1455 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1456 #ifdef CAN_COMPILE_MMXEXT
1459 * TODO: mmx version (easier in sse2)
1461 static inline void XDeintMC( uint8_t *dst, int i_dst,
1462 uint8_t *src, int i_src,
1464 int i_width, int i_height )
1466 const int d4x = mvx&0x03;
1467 const int d4y = mvy&0x03;
1469 const int cA = (4-d4x)*(4-d4y);
1470 const int cB = d4x *(4-d4y);
1471 const int cC = (4-d4x)*d4y;
1472 const int cD = d4x *d4y;
1478 src += (mvy >> 2) * i_src + (mvx >> 2);
1481 for( y = 0; y < i_height; y++ )
1483 for( x = 0; x < i_width; x++ )
1485 dst[x] = ( cA*src[x] + cB*src[x+1] +
1486 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1494 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1495 uint8_t *pix2, int i_pix2 )
1499 movq_m2r( pix1[0*i_pix1], mm0 );
1500 movq_m2r( pix1[1*i_pix1], mm1 );
1502 psadbw_m2r( pix2[0*i_pix2], mm0 );
1503 psadbw_m2r( pix2[1*i_pix2], mm1 );
1505 movq_m2r( pix1[2*i_pix1], mm2 );
1506 movq_m2r( pix1[3*i_pix1], mm3 );
1507 psadbw_m2r( pix2[2*i_pix2], mm2 );
1508 psadbw_m2r( pix2[3*i_pix2], mm3 );
1510 paddd_r2r( mm1, mm0 );
1511 paddd_r2r( mm3, mm2 );
1512 paddd_r2r( mm2, mm0 );
1518 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1519 uint8_t *ref, int i_stride,
1521 int xmax, int ymax )
1523 uint8_t buffer[8*4];
1525 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1528 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1529 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1531 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1532 uint8_t *ref, int i_stride,
1534 int xmax, int ymax )
1536 if( abs(mx) >= xmax || abs(my) >= ymax )
1539 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1542 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1543 uint8_t *src, int i_src,
1545 int xmax, int ymax )
1547 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1548 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1549 uint8_t *next = &src[i_src];
1550 const int i_src2 = 2*i_src;
1555 uint8_t *rec = &dst[i_dst];
1557 /* We construct with intra method the missing field */
1558 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1560 /* Now we will try to find a match with ME with the other field */
1562 /* ME: A small/partial EPZS
1563 * We search only for small MV (with high motion intra will be perfect */
1564 if( xmax > 4 ) xmax = 4;
1565 if( ymax > 4 ) ymax = 4;
1567 /* Init with NULL Mv */
1569 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1571 /* Try predicted Mv */
1572 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1578 /* Search interger pel (small mv) */
1579 for( i_step = 0; i_step < 4; i_step++ )
1585 for( i = 0; i < 4; i++ )
1587 s = XDeint8x4TestInt( rec, i_src2,
1588 next, i_src2, mvx+dx[i], mvy+dy[i],
1608 if( mvs > 4 && mvs < 256 )
1611 /* XXX: for now only HPEL (too slow) */
1612 for( i_step = 0; i_step < 4; i_step++ )
1618 for( i = 0; i < 8; i++ )
1620 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1621 mvx+dx[i], mvy+dy[i],
1639 uint8_t buffer[8*4];
1640 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1641 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1643 //XDeint8x8Set( dst, i_dst, 0 );
1650 /* Kernel interpolation (1,-5,20,20,-5,1)
1651 * Loose a bit more details+add aliasing than edge interpol but avoid
1654 static inline uint8_t clip1( int a )
1663 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1664 uint8_t *src, int i_src )
1669 for( y = 0; y < 8; y += 2 )
1671 const int i_src2 = i_src*2;
1673 memcpy( dst, src, 8 );
1676 for( x = 0; x < 8; x++ )
1680 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1681 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1682 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1684 dst[x] = clip1( ( pix + 16 ) >> 5 );
1694 /* NxN arbitray size (and then only use pixel in the NxN block)
1696 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1697 int i_height, int i_width )
1704 /* Detect interlacing */
1705 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1708 for( y = 0; y < i_height - 2; y += 2 )
1710 const uint8_t *s = &src[y*i_src];
1711 for( x = 0; x < i_width; x++ )
1713 fr += ssd(s[ x] - s[1*i_src+x]);
1714 ff += ssd(s[ x] - s[2*i_src+x]);
1716 if( ff < fr && fr > i_width / 2 )
1720 return fc < 2 ? VLC_FALSE : VLC_TRUE;
1723 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1724 uint8_t *src, int i_src,
1725 int i_width, int i_height )
1730 for( y = 0; y < i_height; y += 2 )
1732 memcpy( dst, src, i_width );
1735 if( y < i_height - 2 )
1737 for( x = 0; x < i_width; x++ )
1738 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1742 /* Blend last line */
1743 for( x = 0; x < i_width; x++ )
1744 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1751 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1752 uint8_t *src, int i_src,
1753 int i_width, int i_height )
1758 for( y = 0; y < i_height; y += 2 )
1760 memcpy( dst, src, i_width );
1763 if( y < i_height - 2 )
1765 for( x = 0; x < i_width; x++ )
1766 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1770 /* Blend last line */
1771 for( x = 0; x < i_width; x++ )
1772 dst[x] = (src[x] + src[i_src+x]) >> 1;
1779 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1780 int i_width, int i_height )
1782 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1783 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1785 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1789 static inline int median( int a, int b, int c )
1791 int min = a, max =a;
1802 return a + b + c - min - max;
1808 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1809 uint8_t *src, int i_src,
1810 const int i_mbx, int i_modx )
1814 for( x = 0; x < i_mbx; x++ )
1817 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1819 if( x == 0 || x == i_mbx - 1 )
1820 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1822 XDeint8x8FieldC( dst, i_dst, src, i_src );
1826 XDeint8x8MergeC( dst, i_dst,
1827 &src[0*i_src], 2*i_src,
1828 &src[1*i_src], 2*i_src );
1836 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1838 #ifdef CAN_COMPILE_MMXEXT
1839 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1840 uint8_t *src, int i_src,
1841 const int i_mbx, int i_modx )
1845 /* Reset current line */
1846 for( x = 0; x < i_mbx; x++ )
1849 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1851 if( x == 0 || x == i_mbx - 1 )
1852 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1854 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1858 XDeint8x8MergeMMXEXT( dst, i_dst,
1859 &src[0*i_src], 2*i_src,
1860 &src[1*i_src], 2*i_src );
1868 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1872 static void RenderX( vout_thread_t *p_vout,
1873 picture_t *p_outpic, picture_t *p_pic )
1875 vout_sys_t *p_sys = p_vout->p_sys;
1878 /* Copy image and skip lines */
1879 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1881 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1882 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1884 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1885 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1887 const int i_dst = p_outpic->p[i_plane].i_pitch;
1888 const int i_src = p_pic->p[i_plane].i_pitch;
1892 for( y = 0; y < i_mby; y++ )
1894 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1895 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1897 #ifdef CAN_COMPILE_MMXEXT
1898 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
1899 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1902 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1905 /* Last line (C only)*/
1908 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1909 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1911 for( x = 0; x < i_mbx; x++ )
1913 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1920 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1924 #ifdef CAN_COMPILE_MMXEXT
1925 if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
1930 /*****************************************************************************
1931 * SendEvents: forward mouse and keyboard events to the parent p_vout
1932 *****************************************************************************/
1933 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1934 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1936 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1937 vlc_value_t sentval = newval;
1939 if( !strcmp( psz_var, "mouse-y" ) )
1941 switch( p_vout->p_sys->i_mode )
1943 case DEINTERLACE_MEAN:
1944 case DEINTERLACE_DISCARD:
1950 var_Set( p_vout, psz_var, sentval );
1955 /*****************************************************************************
1956 * FilterCallback: called when changing the deinterlace method on the fly.
1957 *****************************************************************************/
1958 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1959 vlc_value_t oldval, vlc_value_t newval,
1962 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1963 int i_old_mode = p_vout->p_sys->i_mode;
1965 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1967 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1969 SetFilterMethod( p_vout, newval.psz_string );
1971 switch( p_vout->render.i_chroma )
1973 case VLC_FOURCC('I','4','2','2'):
1974 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1978 case VLC_FOURCC('I','4','2','0'):
1979 case VLC_FOURCC('I','Y','U','V'):
1980 case VLC_FOURCC('Y','V','1','2'):
1981 switch( p_vout->p_sys->i_mode )
1983 case DEINTERLACE_MEAN:
1984 case DEINTERLACE_DISCARD:
1985 if( ( i_old_mode == DEINTERLACE_MEAN )
1986 || ( i_old_mode == DEINTERLACE_DISCARD ) )
1988 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1993 case DEINTERLACE_BOB:
1994 case DEINTERLACE_BLEND:
1995 case DEINTERLACE_LINEAR:
1996 if( ( i_old_mode == DEINTERLACE_BOB )
1997 || ( i_old_mode == DEINTERLACE_BLEND )
1998 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2000 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2011 /* We need to kill the old vout */
2013 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2015 vlc_object_detach( p_vout->p_sys->p_vout );
2016 vout_Destroy( p_vout->p_sys->p_vout );
2018 /* Try to open a new video output */
2019 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2021 if( p_vout->p_sys->p_vout == NULL )
2023 /* Everything failed */
2024 msg_Err( p_vout, "cannot open vout, aborting" );
2026 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2027 return VLC_EGENERIC;
2030 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2032 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2036 /*****************************************************************************
2037 * SendEventsToChild: forward events to the child/children vout
2038 *****************************************************************************/
2039 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2040 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2042 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2043 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2048 /*****************************************************************************
2049 * video filter2 functions
2050 *****************************************************************************/
2051 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2053 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2054 picture_t *p_pic_dst;
2056 /* Request output picture */
2057 p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
2058 if( p_pic_dst == NULL )
2060 msg_Warn( p_filter, "can't get output picture" );
2064 switch( p_vout->p_sys->i_mode )
2066 case DEINTERLACE_DISCARD:
2068 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2070 msg_Err( p_vout, "discarding lines is not supported yet" );
2071 p_pic_dst->pf_release( p_pic_dst );
2075 case DEINTERLACE_BOB:
2077 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2078 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2082 case DEINTERLACE_LINEAR:
2084 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2085 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2087 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2088 p_pic_dst->pf_release( p_pic_dst );
2092 case DEINTERLACE_MEAN:
2093 RenderMean( p_vout, p_pic_dst, p_pic );
2096 case DEINTERLACE_BLEND:
2097 RenderBlend( p_vout, p_pic_dst, p_pic );
2101 RenderX( p_vout, p_pic_dst, p_pic );
2105 p_pic_dst->date = p_pic->date;
2106 p_pic_dst->b_force = p_pic->b_force;
2107 p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
2108 p_pic_dst->b_progressive = VLC_TRUE;
2109 p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
2111 p_pic->pf_release( p_pic );
2115 /*****************************************************************************
2117 *****************************************************************************/
2118 static int OpenFilter( vlc_object_t *p_this )
2120 filter_t *p_filter = (filter_t*)p_this;
2121 vout_thread_t *p_vout;
2124 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2125 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2126 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2127 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2129 return VLC_EGENERIC;
2132 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2134 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2135 vlc_object_attach( p_vout, p_filter );
2136 p_filter->p_sys = (filter_sys_t *)p_vout;
2137 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2139 sout_CfgParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2141 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2142 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2143 var_Set( p_filter, "deinterlace-mode", val );
2145 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2147 vlc_object_detach( p_vout );
2148 vlc_object_release( p_vout );
2149 return VLC_EGENERIC;
2152 p_filter->pf_video_filter = Deinterlace;
2154 msg_Dbg( p_filter, "deinterlacing" );
2159 /*****************************************************************************
2160 * CloseFilter: clean up the filter
2161 *****************************************************************************/
2162 static void CloseFilter( vlc_object_t *p_this )
2164 filter_t *p_filter = (filter_t*)p_this;
2165 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2167 Destroy( VLC_OBJECT(p_vout) );
2168 vlc_object_detach( p_vout );
2169 vlc_object_release( p_vout );