1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2009 the VideoLAN team
7 * Author: Sam Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
42 #include <vlc_filter.h>
44 #ifdef CAN_COMPILE_MMXEXT
48 #include "filter_common.h"
50 #define DEINTERLACE_DISCARD 1
51 #define DEINTERLACE_MEAN 2
52 #define DEINTERLACE_BLEND 3
53 #define DEINTERLACE_BOB 4
54 #define DEINTERLACE_LINEAR 5
55 #define DEINTERLACE_X 6
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Create ( vlc_object_t * );
61 static void Destroy ( vlc_object_t * );
63 static int Init ( vout_thread_t * );
64 static void End ( vout_thread_t * );
65 static void Render ( vout_thread_t *, picture_t * );
67 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
68 vlc_value_t oldval, vlc_value_t newval, void *p_data );
70 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
71 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
72 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
73 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
74 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
75 static void RenderX ( picture_t *, picture_t * );
77 static void MergeGeneric ( void *, const void *, const void *, size_t );
78 #if defined(CAN_COMPILE_C_ALTIVEC)
79 static void MergeAltivec ( void *, const void *, const void *, size_t );
81 #if defined(CAN_COMPILE_MMXEXT)
82 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
84 #if defined(CAN_COMPILE_3DNOW)
85 static void Merge3DNow ( void *, const void *, const void *, size_t );
87 #if defined(CAN_COMPILE_SSE)
88 static void MergeSSE2 ( void *, const void *, const void *, size_t );
90 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
91 static void EndMMX ( void );
93 #if defined(CAN_COMPILE_3DNOW)
94 static void End3DNow ( void );
97 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method );
98 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
100 static int OpenFilter( vlc_object_t *p_this );
101 static void CloseFilter( vlc_object_t *p_this );
103 /*****************************************************************************
104 * Callback prototypes
105 *****************************************************************************/
106 static int FilterCallback( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 /*****************************************************************************
111 *****************************************************************************/
112 #define MODE_TEXT N_("Deinterlace mode")
113 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
115 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
116 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
118 #define FILTER_CFG_PREFIX "sout-deinterlace-"
120 static const char *const mode_list[] = {
121 "discard", "blend", "mean", "bob", "linear", "x" };
122 static const char *const mode_list_text[] = {
123 N_("Discard"), N_("Blend"), N_("Mean"), N_("Bob"), N_("Linear"), "X" };
126 set_description( N_("Deinterlacing video filter") )
127 set_shortname( N_("Deinterlace" ))
128 set_capability( "video filter", 0 )
129 set_category( CAT_VIDEO )
130 set_subcategory( SUBCAT_VIDEO_VFILTER )
132 set_section( N_("Display"),NULL)
133 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
134 MODE_LONGTEXT, false )
135 change_string_list( mode_list, mode_list_text, 0 )
138 add_shortcut( "deinterlace" )
139 set_callbacks( Create, Destroy )
142 set_capability( "video filter2", 0 )
143 set_section( N_("Streaming"),NULL)
144 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
145 SOUT_MODE_LONGTEXT, false )
146 change_string_list( mode_list, mode_list_text, 0 )
147 add_shortcut( "deinterlace" )
148 set_callbacks( OpenFilter, CloseFilter )
151 static const char *const ppsz_filter_options[] = {
155 /*****************************************************************************
156 * vout_sys_t: Deinterlace video output method descriptor
157 *****************************************************************************
158 * This structure is part of the video output thread descriptor.
159 * It describes the Deinterlace specific properties of an output thread.
160 *****************************************************************************/
163 int i_mode; /* Deinterlace mode */
164 bool b_double_rate; /* Shall we double the framerate? */
165 bool b_half_height; /* Shall be devide the height by 2 */
170 vout_thread_t *p_vout;
172 vlc_mutex_t filter_lock;
174 void (*pf_merge) ( void *, const void *, const void *, size_t );
175 void (*pf_end_merge) ( void );
178 /*****************************************************************************
179 * Control: control facility for the vout (forwards to child vout)
180 *****************************************************************************/
181 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
183 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
186 /*****************************************************************************
187 * Create: allocates Deinterlace video thread output method
188 *****************************************************************************
189 * This function allocates and initializes a Deinterlace vout method.
190 *****************************************************************************/
191 static int Create( vlc_object_t *p_this )
193 vout_thread_t *p_vout = (vout_thread_t *)p_this;
197 /* Allocate structure */
198 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
199 if( p_vout->p_sys == NULL )
202 p_vout->pf_init = Init;
203 p_vout->pf_end = End;
204 p_vout->pf_manage = NULL;
205 p_vout->pf_render = Render;
206 p_vout->pf_display = NULL;
207 p_vout->pf_control = Control;
209 p_sys->i_mode = DEINTERLACE_DISCARD;
210 p_sys->b_double_rate = false;
211 p_sys->b_half_height = true;
212 p_sys->last_date = 0;
214 vlc_mutex_init( &p_sys->filter_lock );
216 #if defined(CAN_COMPILE_C_ALTIVEC)
217 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
219 p_sys->pf_merge = MergeAltivec;
220 p_sys->pf_end_merge = NULL;
224 #if defined(CAN_COMPILE_SSE)
225 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
227 p_sys->pf_merge = MergeSSE2;
228 p_sys->pf_end_merge = EndMMX;
232 #if defined(CAN_COMPILE_MMXEXT)
233 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
235 p_sys->pf_merge = MergeMMXEXT;
236 p_sys->pf_end_merge = EndMMX;
240 #if defined(CAN_COMPILE_3DNOW)
241 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
243 p_sys->pf_merge = Merge3DNow;
244 p_sys->pf_end_merge = End3DNow;
249 p_sys->pf_merge = MergeGeneric;
250 p_sys->pf_end_merge = NULL;
253 /* Look what method was requested */
254 psz_mode = var_CreateGetString( p_vout, "deinterlace-mode" );
258 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
259 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
261 psz_mode = strdup( "discard" );
264 SetFilterMethod( p_vout, psz_mode );
271 /*****************************************************************************
272 * SetFilterMethod: setup the deinterlace method to use.
273 *****************************************************************************/
274 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method )
276 vout_sys_t *p_sys = p_vout->p_sys;
277 if( !strcmp( psz_method, "mean" ) )
279 p_sys->i_mode = DEINTERLACE_MEAN;
280 p_sys->b_double_rate = false;
281 p_sys->b_half_height = true;
283 else if( !strcmp( psz_method, "blend" )
284 || !strcmp( psz_method, "average" )
285 || !strcmp( psz_method, "combine-fields" ) )
287 p_sys->i_mode = DEINTERLACE_BLEND;
288 p_sys->b_double_rate = false;
289 p_sys->b_half_height = false;
291 else if( !strcmp( psz_method, "bob" )
292 || !strcmp( psz_method, "progressive-scan" ) )
294 p_sys->i_mode = DEINTERLACE_BOB;
295 p_sys->b_double_rate = true;
296 p_sys->b_half_height = false;
298 else if( !strcmp( psz_method, "linear" ) )
300 p_sys->i_mode = DEINTERLACE_LINEAR;
301 p_sys->b_double_rate = true;
302 p_sys->b_half_height = false;
304 else if( !strcmp( psz_method, "x" ) )
306 p_sys->i_mode = DEINTERLACE_X;
307 p_sys->b_double_rate = false;
308 p_sys->b_half_height = false;
312 const bool b_i422 = p_vout->render.i_chroma == VLC_CODEC_I422;
313 if( strcmp( psz_method, "discard" ) )
314 msg_Err( p_vout, "no valid deinterlace mode provided, "
315 "using \"discard\"" );
317 p_sys->i_mode = DEINTERLACE_DISCARD;
318 p_sys->b_double_rate = false;
319 p_sys->b_half_height = !b_i422;
322 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
325 static void GetOutputFormat( vout_thread_t *p_vout,
326 video_format_t *p_dst, const video_format_t *p_src )
330 if( p_vout->p_sys->b_half_height )
332 p_dst->i_height /= 2;
333 p_dst->i_visible_height /= 2;
334 p_dst->i_y_offset /= 2;
335 p_dst->i_sar_den *= 2;
338 if( p_src->i_chroma == VLC_CODEC_I422 )
340 switch( p_vout->p_sys->i_mode )
342 case DEINTERLACE_MEAN:
343 case DEINTERLACE_LINEAR:
345 p_dst->i_chroma = VLC_CODEC_I422;
348 p_dst->i_chroma = VLC_CODEC_I420;
354 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
356 return i_chroma == VLC_CODEC_I420 ||
357 i_chroma == VLC_CODEC_YV12 ||
358 i_chroma == VLC_CODEC_I422;
361 /*****************************************************************************
362 * Init: initialize Deinterlace video thread output method
363 *****************************************************************************/
364 static int Init( vout_thread_t *p_vout )
366 I_OUTPUTPICTURES = 0;
368 if( !IsChromaSupported( p_vout->render.i_chroma ) )
369 return VLC_EGENERIC; /* unknown chroma */
371 /* Initialize the output structure, full of directbuffers since we want
372 * the decoder to output directly to our structures. */
373 p_vout->output.i_chroma = p_vout->render.i_chroma;
374 p_vout->output.i_width = p_vout->render.i_width;
375 p_vout->output.i_height = p_vout->render.i_height;
376 p_vout->output.i_aspect = p_vout->render.i_aspect;
377 p_vout->fmt_out = p_vout->fmt_in;
379 /* Try to open the real video output */
380 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
382 if( p_vout->p_sys->p_vout == NULL )
384 /* Everything failed */
385 msg_Err( p_vout, "cannot open vout, aborting" );
390 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
392 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
394 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
399 /*****************************************************************************
400 * SpawnRealVout: spawn the real video output.
401 *****************************************************************************/
402 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
404 msg_Dbg( p_vout, "spawning the real video output" );
407 GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
409 return vout_Create( p_vout, &fmt );
412 /*****************************************************************************
413 * End: terminate Deinterlace video thread output method
414 *****************************************************************************/
415 static void End( vout_thread_t *p_vout )
417 vout_sys_t *p_sys = p_vout->p_sys;
419 var_DelCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
423 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
424 vout_CloseAndRelease( p_sys->p_vout );
427 vout_filter_ReleaseDirectBuffers( p_vout );
430 /*****************************************************************************
431 * Destroy: destroy Deinterlace video thread output method
432 *****************************************************************************
433 * Terminate an output method created by DeinterlaceCreateOutputMethod
434 *****************************************************************************/
435 static void Destroy( vlc_object_t *p_this )
437 vout_thread_t *p_vout = (vout_thread_t *)p_this;
438 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
439 free( p_vout->p_sys );
443 * Forward mouse event with proper conversion.
445 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
446 vlc_value_t oldval, vlc_value_t newval, void *p_data )
448 vout_thread_t *p_vout = p_data;
449 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
451 if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
454 return var_Set( p_vout, psz_var, newval );
457 /*****************************************************************************
458 * Render: displays previously rendered output
459 *****************************************************************************
460 * This function send the currently rendered image to Deinterlace image,
461 * waits until it is displayed and switch the two rendering buffers, preparing
463 *****************************************************************************/
464 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
466 vout_sys_t *p_sys = p_vout->p_sys;
467 picture_t *pp_outpic[2];
469 /* FIXME are they needed ? */
470 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
471 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
472 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
473 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
475 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
476 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
477 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
478 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
479 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
480 if( p_vout->p_sys->b_half_height )
482 p_sys->p_vout->fmt_in.i_y_offset /= 2;
483 p_sys->p_vout->fmt_in.i_visible_height /= 2;
486 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
488 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
490 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
491 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
492 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
494 video_format_t fmt = p_vout->fmt_out;
495 if( p_vout->p_sys->b_half_height )
497 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
501 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
506 pp_outpic[0] = pp_outpic[1] = NULL;
508 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
510 /* Get a new picture */
511 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
515 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
517 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
520 msleep( VOUT_OUTMEM_SLEEP );
523 pp_outpic[0]->date = p_pic->date;
525 /* If we are using double rate, get an additional new picture */
526 if( p_vout->p_sys->b_double_rate )
528 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
532 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
534 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
535 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
538 msleep( VOUT_OUTMEM_SLEEP );
541 /* 20ms is a bit arbitrary, but it's only for the first image we get */
542 if( !p_vout->p_sys->last_date )
543 pp_outpic[1]->date = p_pic->date + 20000;
545 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
546 p_vout->p_sys->last_date = p_pic->date;
549 switch( p_vout->p_sys->i_mode )
551 case DEINTERLACE_DISCARD:
552 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
553 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
556 case DEINTERLACE_BOB:
557 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
558 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
559 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
560 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
563 case DEINTERLACE_LINEAR:
564 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
565 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
566 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
567 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
570 case DEINTERLACE_MEAN:
571 RenderMean( p_vout, pp_outpic[0], p_pic );
572 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
575 case DEINTERLACE_BLEND:
576 RenderBlend( p_vout, pp_outpic[0], p_pic );
577 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
581 RenderX( pp_outpic[0], p_pic );
582 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
585 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
588 /*****************************************************************************
589 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
590 *****************************************************************************/
591 static void RenderDiscard( vout_thread_t *p_vout,
592 picture_t *p_outpic, picture_t *p_pic, int i_field )
596 /* Copy image and skip lines */
597 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
599 uint8_t *p_in, *p_out_end, *p_out;
602 p_in = p_pic->p[i_plane].p_pixels
603 + i_field * p_pic->p[i_plane].i_pitch;
605 p_out = p_outpic->p[i_plane].p_pixels;
606 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
607 * p_outpic->p[i_plane].i_visible_lines;
609 switch( p_vout->render.i_chroma )
614 for( ; p_out < p_out_end ; )
616 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
618 p_out += p_outpic->p[i_plane].i_pitch;
619 p_in += 2 * p_pic->p[i_plane].i_pitch;
625 i_increment = 2 * p_pic->p[i_plane].i_pitch;
627 if( i_plane == Y_PLANE )
629 for( ; p_out < p_out_end ; )
631 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
632 p_out += p_outpic->p[i_plane].i_pitch;
633 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
634 p_out += p_outpic->p[i_plane].i_pitch;
640 for( ; p_out < p_out_end ; )
642 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
643 p_out += p_outpic->p[i_plane].i_pitch;
655 /*****************************************************************************
656 * RenderBob: renders a BOB picture - simple copy
657 *****************************************************************************/
658 static void RenderBob( vout_thread_t *p_vout,
659 picture_t *p_outpic, picture_t *p_pic, int i_field )
663 /* Copy image and skip lines */
664 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
666 uint8_t *p_in, *p_out_end, *p_out;
668 p_in = p_pic->p[i_plane].p_pixels;
669 p_out = p_outpic->p[i_plane].p_pixels;
670 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
671 * p_outpic->p[i_plane].i_visible_lines;
673 switch( p_vout->render.i_chroma )
677 /* For BOTTOM field we need to add the first line */
680 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
681 p_in += p_pic->p[i_plane].i_pitch;
682 p_out += p_outpic->p[i_plane].i_pitch;
685 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
687 for( ; p_out < p_out_end ; )
689 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
691 p_out += p_outpic->p[i_plane].i_pitch;
693 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
695 p_in += 2 * p_pic->p[i_plane].i_pitch;
696 p_out += p_outpic->p[i_plane].i_pitch;
699 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
701 /* For TOP field we need to add the last line */
704 p_in += p_pic->p[i_plane].i_pitch;
705 p_out += p_outpic->p[i_plane].i_pitch;
706 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
711 /* For BOTTOM field we need to add the first line */
714 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
715 p_in += p_pic->p[i_plane].i_pitch;
716 p_out += p_outpic->p[i_plane].i_pitch;
719 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
721 if( i_plane == Y_PLANE )
723 for( ; p_out < p_out_end ; )
725 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
727 p_out += p_outpic->p[i_plane].i_pitch;
729 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
731 p_in += 2 * p_pic->p[i_plane].i_pitch;
732 p_out += p_outpic->p[i_plane].i_pitch;
737 for( ; p_out < p_out_end ; )
739 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
741 p_out += p_outpic->p[i_plane].i_pitch;
742 p_in += 2 * p_pic->p[i_plane].i_pitch;
746 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
748 /* For TOP field we need to add the last line */
751 p_in += p_pic->p[i_plane].i_pitch;
752 p_out += p_outpic->p[i_plane].i_pitch;
753 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
760 #define Merge p_vout->p_sys->pf_merge
761 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
763 /*****************************************************************************
764 * RenderLinear: BOB with linear interpolation
765 *****************************************************************************/
766 static void RenderLinear( vout_thread_t *p_vout,
767 picture_t *p_outpic, picture_t *p_pic, int i_field )
771 /* Copy image and skip lines */
772 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
774 uint8_t *p_in, *p_out_end, *p_out;
776 p_in = p_pic->p[i_plane].p_pixels;
777 p_out = p_outpic->p[i_plane].p_pixels;
778 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
779 * p_outpic->p[i_plane].i_visible_lines;
781 /* For BOTTOM field we need to add the first line */
784 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
785 p_in += p_pic->p[i_plane].i_pitch;
786 p_out += p_outpic->p[i_plane].i_pitch;
789 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
791 for( ; p_out < p_out_end ; )
793 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
795 p_out += p_outpic->p[i_plane].i_pitch;
797 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
798 p_pic->p[i_plane].i_pitch );
800 p_in += 2 * p_pic->p[i_plane].i_pitch;
801 p_out += p_outpic->p[i_plane].i_pitch;
804 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
806 /* For TOP field we need to add the last line */
809 p_in += p_pic->p[i_plane].i_pitch;
810 p_out += p_outpic->p[i_plane].i_pitch;
811 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
817 static void RenderMean( vout_thread_t *p_vout,
818 picture_t *p_outpic, picture_t *p_pic )
822 /* Copy image and skip lines */
823 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
825 uint8_t *p_in, *p_out_end, *p_out;
827 p_in = p_pic->p[i_plane].p_pixels;
829 p_out = p_outpic->p[i_plane].p_pixels;
830 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
831 * p_outpic->p[i_plane].i_visible_lines;
833 /* All lines: mean value */
834 for( ; p_out < p_out_end ; )
836 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
837 p_pic->p[i_plane].i_pitch );
839 p_out += p_outpic->p[i_plane].i_pitch;
840 p_in += 2 * p_pic->p[i_plane].i_pitch;
846 static void RenderBlend( vout_thread_t *p_vout,
847 picture_t *p_outpic, picture_t *p_pic )
851 /* Copy image and skip lines */
852 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
854 uint8_t *p_in, *p_out_end, *p_out;
856 p_in = p_pic->p[i_plane].p_pixels;
858 p_out = p_outpic->p[i_plane].p_pixels;
859 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
860 * p_outpic->p[i_plane].i_visible_lines;
862 switch( p_vout->render.i_chroma )
866 /* First line: simple copy */
867 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
868 p_out += p_outpic->p[i_plane].i_pitch;
870 /* Remaining lines: mean value */
871 for( ; p_out < p_out_end ; )
873 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
874 p_pic->p[i_plane].i_pitch );
876 p_out += p_outpic->p[i_plane].i_pitch;
877 p_in += p_pic->p[i_plane].i_pitch;
882 /* First line: simple copy */
883 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
884 p_out += p_outpic->p[i_plane].i_pitch;
886 /* Remaining lines: mean value */
887 if( i_plane == Y_PLANE )
889 for( ; p_out < p_out_end ; )
891 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
892 p_pic->p[i_plane].i_pitch );
894 p_out += p_outpic->p[i_plane].i_pitch;
895 p_in += p_pic->p[i_plane].i_pitch;
901 for( ; p_out < p_out_end ; )
903 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
904 p_pic->p[i_plane].i_pitch );
906 p_out += p_outpic->p[i_plane].i_pitch;
907 p_in += 2*p_pic->p[i_plane].i_pitch;
918 static void MergeGeneric( void *_p_dest, const void *_p_s1,
919 const void *_p_s2, size_t i_bytes )
921 uint8_t* p_dest = (uint8_t*)_p_dest;
922 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
923 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
924 uint8_t* p_end = p_dest + i_bytes - 8;
926 while( p_dest < p_end )
928 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
929 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
930 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
931 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
932 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
933 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
934 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
935 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
940 while( p_dest < p_end )
942 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
946 #if defined(CAN_COMPILE_MMXEXT)
947 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
950 uint8_t* p_dest = (uint8_t*)_p_dest;
951 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
952 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
953 uint8_t* p_end = p_dest + i_bytes - 8;
954 while( p_dest < p_end )
956 __asm__ __volatile__( "movq %2,%%mm1;"
958 "movq %%mm1, %0" :"=m" (*p_dest):
968 while( p_dest < p_end )
970 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
975 #if defined(CAN_COMPILE_3DNOW)
976 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
979 uint8_t* p_dest = (uint8_t*)_p_dest;
980 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
981 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
982 uint8_t* p_end = p_dest + i_bytes - 8;
983 while( p_dest < p_end )
985 __asm__ __volatile__( "movq %2,%%mm1;"
987 "movq %%mm1, %0" :"=m" (*p_dest):
997 while( p_dest < p_end )
999 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1004 #if defined(CAN_COMPILE_SSE)
1005 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1008 uint8_t* p_dest = (uint8_t*)_p_dest;
1009 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1010 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1012 while( (uintptr_t)p_s1 % 16 )
1014 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1016 p_end = p_dest + i_bytes - 16;
1017 while( p_dest < p_end )
1019 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1021 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1031 while( p_dest < p_end )
1033 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1038 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1039 static void EndMMX( void )
1041 __asm__ __volatile__( "emms" :: );
1045 #if defined(CAN_COMPILE_3DNOW)
1046 static void End3DNow( void )
1048 __asm__ __volatile__( "femms" :: );
1052 #ifdef CAN_COMPILE_C_ALTIVEC
1053 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1054 const void *_p_s2, size_t i_bytes )
1056 uint8_t *p_dest = (uint8_t *)_p_dest;
1057 uint8_t *p_s1 = (uint8_t *)_p_s1;
1058 uint8_t *p_s2 = (uint8_t *)_p_s2;
1059 uint8_t *p_end = p_dest + i_bytes - 15;
1061 /* Use C until the first 16-bytes aligned destination pixel */
1062 while( (int)p_dest & 0xF )
1064 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1067 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1069 /* Unaligned source */
1070 vector unsigned char s1v, s2v, destv;
1071 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1072 vector unsigned char perm1v, perm2v;
1074 perm1v = vec_lvsl( 0, p_s1 );
1075 perm2v = vec_lvsl( 0, p_s2 );
1076 s1oldv = vec_ld( 0, p_s1 );
1077 s2oldv = vec_ld( 0, p_s2 );
1079 while( p_dest < p_end )
1081 s1newv = vec_ld( 16, p_s1 );
1082 s2newv = vec_ld( 16, p_s2 );
1083 s1v = vec_perm( s1oldv, s1newv, perm1v );
1084 s2v = vec_perm( s2oldv, s2newv, perm2v );
1087 destv = vec_avg( s1v, s2v );
1088 vec_st( destv, 0, p_dest );
1097 /* Aligned source */
1098 vector unsigned char s1v, s2v, destv;
1100 while( p_dest < p_end )
1102 s1v = vec_ld( 0, p_s1 );
1103 s2v = vec_ld( 0, p_s2 );
1104 destv = vec_avg( s1v, s2v );
1105 vec_st( destv, 0, p_dest );
1115 while( p_dest < p_end )
1117 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1122 /*****************************************************************************
1123 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1124 * and apply a process to recreate the bottom field :
1125 * If a 8x8 block is classified as :
1126 * - progressive: it applies a small blend (1,6,1)
1128 * * in the MMX version: we do a ME between the 2 fields, if there is a
1129 * good match we use MC to recreate the bottom field (with a small
1131 * * otherwise: it recreates the bottom field by an edge oriented
1133 *****************************************************************************/
1135 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1136 * XXX: It need to access to 8x10
1137 * We use more than 8 lines to help with scrolling (text)
1138 * (and because XDeint8x8Frame use line 9)
1139 * XXX: smooth/uniform area with noise detection doesn't works well
1140 * but it's not really a problem because they don't have much details anyway
1142 static inline int ssd( int a ) { return a*a; }
1143 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1149 /* Detect interlacing */
1151 for( y = 0; y < 7; y += 2 )
1154 for( x = 0; x < 8; x++ )
1156 fr += ssd(src[ x] - src[1*i_src+x]) +
1157 ssd(src[i_src+x] - src[2*i_src+x]);
1158 ff += ssd(src[ x] - src[2*i_src+x]) +
1159 ssd(src[i_src+x] - src[3*i_src+x]);
1161 if( ff < 6*fr/8 && fr > 32 )
1167 return fc < 1 ? false : true;
1169 #ifdef CAN_COMPILE_MMXEXT
1170 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1177 /* Detect interlacing */
1179 pxor_r2r( mm7, mm7 );
1180 for( y = 0; y < 9; y += 2 )
1183 pxor_r2r( mm5, mm5 );
1184 pxor_r2r( mm6, mm6 );
1185 for( x = 0; x < 8; x+=4 )
1187 movd_m2r( src[ x], mm0 );
1188 movd_m2r( src[1*i_src+x], mm1 );
1189 movd_m2r( src[2*i_src+x], mm2 );
1190 movd_m2r( src[3*i_src+x], mm3 );
1192 punpcklbw_r2r( mm7, mm0 );
1193 punpcklbw_r2r( mm7, mm1 );
1194 punpcklbw_r2r( mm7, mm2 );
1195 punpcklbw_r2r( mm7, mm3 );
1197 movq_r2r( mm0, mm4 );
1199 psubw_r2r( mm1, mm0 );
1200 psubw_r2r( mm2, mm4 );
1202 psubw_r2r( mm1, mm2 );
1203 psubw_r2r( mm1, mm3 );
1205 pmaddwd_r2r( mm0, mm0 );
1206 pmaddwd_r2r( mm4, mm4 );
1207 pmaddwd_r2r( mm2, mm2 );
1208 pmaddwd_r2r( mm3, mm3 );
1209 paddd_r2r( mm0, mm2 );
1210 paddd_r2r( mm4, mm3 );
1211 paddd_r2r( mm2, mm5 );
1212 paddd_r2r( mm3, mm6 );
1215 movq_r2r( mm5, mm0 );
1216 psrlq_i2r( 32, mm0 );
1217 paddd_r2r( mm0, mm5 );
1218 movd_r2m( mm5, fr );
1220 movq_r2r( mm6, mm0 );
1221 psrlq_i2r( 32, mm0 );
1222 paddd_r2r( mm0, mm6 );
1223 movd_r2m( mm6, ff );
1225 if( ff < 6*fr/8 && fr > 32 )
1234 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1235 uint8_t *src1, int i_src1,
1236 uint8_t *src2, int i_src2 )
1241 for( y = 0; y < 8; y += 2 )
1243 memcpy( dst, src1, 8 );
1246 for( x = 0; x < 8; x++ )
1247 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1255 #ifdef CAN_COMPILE_MMXEXT
1256 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1257 uint8_t *src1, int i_src1,
1258 uint8_t *src2, int i_src2 )
1260 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1264 pxor_r2r( mm7, mm7 );
1265 for( y = 0; y < 8; y += 2 )
1267 for( x = 0; x < 8; x +=4 )
1269 movd_m2r( src1[x], mm0 );
1270 movd_r2m( mm0, dst[x] );
1272 movd_m2r( src2[x], mm1 );
1273 movd_m2r( src1[i_src1+x], mm2 );
1275 punpcklbw_r2r( mm7, mm0 );
1276 punpcklbw_r2r( mm7, mm1 );
1277 punpcklbw_r2r( mm7, mm2 );
1278 paddw_r2r( mm1, mm1 );
1279 movq_r2r( mm1, mm3 );
1280 paddw_r2r( mm3, mm3 );
1281 paddw_r2r( mm2, mm0 );
1282 paddw_r2r( mm3, mm1 );
1283 paddw_m2r( m_4, mm1 );
1284 paddw_r2r( mm1, mm0 );
1285 psraw_i2r( 3, mm0 );
1286 packuswb_r2r( mm7, mm0 );
1287 movd_r2m( mm0, dst[i_dst+x] );
1298 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1301 for( y = 0; y < 8; y++ )
1302 memset( &dst[y*i_dst], v, 8 );
1305 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1308 * TODO: a better one for the inner part.
1310 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1311 uint8_t *src, int i_src )
1316 for( y = 0; y < 8; y += 2 )
1318 memcpy( dst, src, 8 );
1321 for( x = 0; x < 8; x++ )
1322 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1327 #ifdef CAN_COMPILE_MMXEXT
1328 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1329 uint8_t *src, int i_src )
1334 for( y = 0; y < 8; y += 2 )
1336 movq_m2r( src[0], mm0 );
1337 movq_r2m( mm0, dst[0] );
1340 movq_m2r( src[2*i_src], mm1 );
1341 pavgb_r2r( mm1, mm0 );
1343 movq_r2m( mm0, dst[0] );
1351 /* XDeint8x8Field: Edge oriented interpolation
1352 * (Need -4 and +5 pixels H, +1 line)
1354 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1355 uint8_t *src, int i_src )
1360 for( y = 0; y < 8; y += 2 )
1362 memcpy( dst, src, 8 );
1365 for( x = 0; x < 8; x++ )
1367 uint8_t *src2 = &src[2*i_src];
1368 /* I use 8 pixels just to match the MMX version, but it's overkill
1369 * 5 would be enough (less isn't good) */
1370 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1371 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1372 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1373 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1375 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1376 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1377 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1378 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1380 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1381 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1382 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1383 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1385 if( c0 < c1 && c1 <= c2 )
1386 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1387 else if( c2 < c1 && c1 <= c0 )
1388 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1390 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1397 #ifdef CAN_COMPILE_MMXEXT
1398 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1399 uint8_t *src, int i_src )
1404 for( y = 0; y < 8; y += 2 )
1406 memcpy( dst, src, 8 );
1409 for( x = 0; x < 8; x++ )
1411 uint8_t *src2 = &src[2*i_src];
1414 movq_m2r( src[x-2], mm0 );
1415 movq_m2r( src[x-3], mm1 );
1416 movq_m2r( src[x-4], mm2 );
1418 psadbw_m2r( src2[x-4], mm0 );
1419 psadbw_m2r( src2[x-3], mm1 );
1420 psadbw_m2r( src2[x-2], mm2 );
1422 movd_r2m( mm0, c2 );
1423 movd_r2m( mm1, c1 );
1424 movd_r2m( mm2, c0 );
1426 if( c0 < c1 && c1 <= c2 )
1427 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1428 else if( c2 < c1 && c1 <= c0 )
1429 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1431 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1440 /* NxN arbitray size (and then only use pixel in the NxN block)
1442 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1443 int i_height, int i_width )
1450 /* Detect interlacing */
1451 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1454 for( y = 0; y < i_height - 2; y += 2 )
1456 const uint8_t *s = &src[y*i_src];
1457 for( x = 0; x < i_width; x++ )
1459 fr += ssd(s[ x] - s[1*i_src+x]);
1460 ff += ssd(s[ x] - s[2*i_src+x]);
1462 if( ff < fr && fr > i_width / 2 )
1466 return fc < 2 ? false : true;
1469 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1470 uint8_t *src, int i_src,
1471 int i_width, int i_height )
1476 for( y = 0; y < i_height; y += 2 )
1478 memcpy( dst, src, i_width );
1481 if( y < i_height - 2 )
1483 for( x = 0; x < i_width; x++ )
1484 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1488 /* Blend last line */
1489 for( x = 0; x < i_width; x++ )
1490 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1497 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1498 uint8_t *src, int i_src,
1499 int i_width, int i_height )
1504 for( y = 0; y < i_height; y += 2 )
1506 memcpy( dst, src, i_width );
1509 if( y < i_height - 2 )
1511 for( x = 0; x < i_width; x++ )
1512 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1516 /* Blend last line */
1517 for( x = 0; x < i_width; x++ )
1518 dst[x] = (src[x] + src[i_src+x]) >> 1;
1525 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1526 int i_width, int i_height )
1528 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1529 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1531 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1535 static inline int median( int a, int b, int c )
1537 int min = a, max =a;
1548 return a + b + c - min - max;
1554 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1555 uint8_t *src, int i_src,
1556 const int i_mbx, int i_modx )
1560 for( x = 0; x < i_mbx; x++ )
1563 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1565 if( x == 0 || x == i_mbx - 1 )
1566 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1568 XDeint8x8FieldC( dst, i_dst, src, i_src );
1572 XDeint8x8MergeC( dst, i_dst,
1573 &src[0*i_src], 2*i_src,
1574 &src[1*i_src], 2*i_src );
1582 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1584 #ifdef CAN_COMPILE_MMXEXT
1585 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1586 uint8_t *src, int i_src,
1587 const int i_mbx, int i_modx )
1591 /* Reset current line */
1592 for( x = 0; x < i_mbx; x++ )
1595 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1597 if( x == 0 || x == i_mbx - 1 )
1598 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1600 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1604 XDeint8x8MergeMMXEXT( dst, i_dst,
1605 &src[0*i_src], 2*i_src,
1606 &src[1*i_src], 2*i_src );
1614 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1618 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1622 /* Copy image and skip lines */
1623 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1625 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1626 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1628 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1629 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1631 const int i_dst = p_outpic->p[i_plane].i_pitch;
1632 const int i_src = p_pic->p[i_plane].i_pitch;
1636 for( y = 0; y < i_mby; y++ )
1638 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1639 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1641 #ifdef CAN_COMPILE_MMXEXT
1642 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1643 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1646 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1649 /* Last line (C only)*/
1652 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1653 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1655 for( x = 0; x < i_mbx; x++ )
1657 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1664 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1668 #ifdef CAN_COMPILE_MMXEXT
1669 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1674 /*****************************************************************************
1675 * FilterCallback: called when changing the deinterlace method on the fly.
1676 *****************************************************************************/
1677 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1678 vlc_value_t oldval, vlc_value_t newval,
1681 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1682 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1683 vout_sys_t *p_sys = p_vout->p_sys;
1685 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1687 vlc_mutex_lock( &p_sys->filter_lock );
1688 const bool b_old_half_height = p_sys->b_half_height;
1690 SetFilterMethod( p_vout, newval.psz_string );
1692 if( !b_old_half_height == !p_sys->b_half_height )
1694 vlc_mutex_unlock( &p_sys->filter_lock );
1698 /* We need to kill the old vout */
1701 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
1702 vout_CloseAndRelease( p_sys->p_vout );
1705 /* Try to open a new video output */
1706 p_sys->p_vout = SpawnRealVout( p_vout );
1708 if( p_sys->p_vout == NULL )
1710 /* Everything failed */
1711 msg_Err( p_vout, "cannot open vout, aborting" );
1713 vlc_mutex_unlock( &p_sys->filter_lock );
1714 return VLC_EGENERIC;
1717 vout_filter_AddChild( p_vout, p_sys->p_vout, MouseEvent );
1719 vlc_mutex_unlock( &p_sys->filter_lock );
1723 /*****************************************************************************
1724 * video filter2 functions
1725 *****************************************************************************/
1726 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1728 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1729 picture_t *p_pic_dst;
1731 /* Request output picture */
1732 p_pic_dst = filter_NewPicture( p_filter );
1733 if( p_pic_dst == NULL )
1735 picture_Release( p_pic );
1739 switch( p_vout->p_sys->i_mode )
1741 case DEINTERLACE_DISCARD:
1742 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1745 case DEINTERLACE_BOB:
1747 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1748 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1752 case DEINTERLACE_LINEAR:
1754 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1755 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1757 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1758 picture_Release( p_pic_dst );
1759 picture_Release( p_pic );
1762 case DEINTERLACE_MEAN:
1763 RenderMean( p_vout, p_pic_dst, p_pic );
1766 case DEINTERLACE_BLEND:
1767 RenderBlend( p_vout, p_pic_dst, p_pic );
1771 RenderX( p_pic_dst, p_pic );
1775 picture_CopyProperties( p_pic_dst, p_pic );
1776 p_pic_dst->b_progressive = true;
1778 picture_Release( p_pic );
1782 /*****************************************************************************
1784 *****************************************************************************/
1785 static int OpenFilter( vlc_object_t *p_this )
1787 filter_t *p_filter = (filter_t*)p_this;
1788 vout_thread_t *p_vout;
1791 if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
1792 return VLC_EGENERIC;
1794 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1796 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1797 vlc_object_attach( p_vout, p_filter );
1798 p_filter->p_sys = (filter_sys_t *)p_vout;
1799 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1801 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1803 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1805 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1806 var_Set( p_filter, "deinterlace-mode", val );
1807 free( val.psz_string );
1809 if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1811 vlc_object_detach( p_vout );
1812 vlc_object_release( p_vout );
1813 return VLC_EGENERIC;
1817 GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
1818 if( !p_filter->b_allow_fmt_out_change &&
1819 ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
1820 fmt.i_height != p_filter->fmt_in.video.i_height ) )
1822 CloseFilter( VLC_OBJECT(p_filter) );
1823 return VLC_EGENERIC;
1825 p_filter->fmt_out.video = fmt;
1826 p_filter->fmt_out.i_codec = fmt.i_chroma;
1827 p_filter->pf_video_filter = Deinterlace;
1829 msg_Dbg( p_filter, "deinterlacing" );
1834 /*****************************************************************************
1835 * CloseFilter: clean up the filter
1836 *****************************************************************************/
1837 static void CloseFilter( vlc_object_t *p_this )
1839 filter_t *p_filter = (filter_t*)p_this;
1840 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1842 Destroy( VLC_OBJECT(p_vout) );
1843 vlc_object_detach( p_vout );
1844 vlc_object_release( p_vout );