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>
45 #ifdef CAN_COMPILE_MMXEXT
49 #include "filter_common.h"
51 #define DEINTERLACE_DISCARD 1
52 #define DEINTERLACE_MEAN 2
53 #define DEINTERLACE_BLEND 3
54 #define DEINTERLACE_BOB 4
55 #define DEINTERLACE_LINEAR 5
56 #define DEINTERLACE_X 6
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Create ( vlc_object_t * );
62 static void Destroy ( vlc_object_t * );
64 static int Init ( vout_thread_t * );
65 static void End ( vout_thread_t * );
66 static void Render ( vout_thread_t *, picture_t * );
68 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
69 vlc_value_t oldval, vlc_value_t newval, void *p_data );
71 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
72 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
73 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
74 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
75 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
76 static void RenderX ( picture_t *, picture_t * );
78 static void MergeGeneric ( void *, const void *, const void *, size_t );
79 #if defined(CAN_COMPILE_C_ALTIVEC)
80 static void MergeAltivec ( void *, const void *, const void *, size_t );
82 #if defined(CAN_COMPILE_MMXEXT)
83 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
85 #if defined(CAN_COMPILE_3DNOW)
86 static void Merge3DNow ( void *, const void *, const void *, size_t );
88 #if defined(CAN_COMPILE_SSE)
89 static void MergeSSE2 ( void *, const void *, const void *, size_t );
91 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
92 static void EndMMX ( void );
94 #if defined(CAN_COMPILE_3DNOW)
95 static void End3DNow ( void );
97 #if defined __ARM_NEON__
98 static void MergeNEON (void *, const void *, const void *, size_t);
101 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method );
102 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
104 static int OpenFilter( vlc_object_t *p_this );
105 static void CloseFilter( vlc_object_t *p_this );
107 /*****************************************************************************
108 * Callback prototypes
109 *****************************************************************************/
110 static int FilterCallback( vlc_object_t *, char const *,
111 vlc_value_t, vlc_value_t, void * );
113 /*****************************************************************************
115 *****************************************************************************/
116 #define MODE_TEXT N_("Deinterlace mode")
117 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
119 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
120 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
122 #define FILTER_CFG_PREFIX "sout-deinterlace-"
124 static const char *const mode_list[] = {
125 "discard", "blend", "mean", "bob", "linear", "x" };
126 static const char *const mode_list_text[] = {
127 N_("Discard"), N_("Blend"), N_("Mean"), N_("Bob"), N_("Linear"), "X" };
130 set_description( N_("Deinterlacing video filter") )
131 set_shortname( N_("Deinterlace" ))
132 set_capability( "video filter", 0 )
133 set_category( CAT_VIDEO )
134 set_subcategory( SUBCAT_VIDEO_VFILTER )
136 set_section( N_("Display"),NULL)
137 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
138 MODE_LONGTEXT, false )
139 change_string_list( mode_list, mode_list_text, 0 )
142 add_shortcut( "deinterlace" )
143 set_callbacks( Create, Destroy )
146 set_capability( "video filter2", 0 )
147 set_section( N_("Streaming"),NULL)
148 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
149 SOUT_MODE_LONGTEXT, false )
150 change_string_list( mode_list, mode_list_text, 0 )
151 add_shortcut( "deinterlace" )
152 set_callbacks( OpenFilter, CloseFilter )
155 static const char *const ppsz_filter_options[] = {
159 /*****************************************************************************
160 * vout_sys_t: Deinterlace video output method descriptor
161 *****************************************************************************
162 * This structure is part of the video output thread descriptor.
163 * It describes the Deinterlace specific properties of an output thread.
164 *****************************************************************************/
167 int i_mode; /* Deinterlace mode */
168 bool b_double_rate; /* Shall we double the framerate? */
169 bool b_half_height; /* Shall be devide the height by 2 */
174 vout_thread_t *p_vout;
176 vlc_mutex_t filter_lock;
178 void (*pf_merge) ( void *, const void *, const void *, size_t );
179 void (*pf_end_merge) ( void );
182 /*****************************************************************************
183 * Control: control facility for the vout (forwards to child vout)
184 *****************************************************************************/
185 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
187 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
190 /*****************************************************************************
191 * Create: allocates Deinterlace video thread output method
192 *****************************************************************************
193 * This function allocates and initializes a Deinterlace vout method.
194 *****************************************************************************/
195 static int Create( vlc_object_t *p_this )
197 vout_thread_t *p_vout = (vout_thread_t *)p_this;
201 /* Allocate structure */
202 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
203 if( p_vout->p_sys == NULL )
206 p_vout->pf_init = Init;
207 p_vout->pf_end = End;
208 p_vout->pf_manage = NULL;
209 p_vout->pf_render = Render;
210 p_vout->pf_display = NULL;
211 p_vout->pf_control = Control;
213 p_sys->i_mode = DEINTERLACE_DISCARD;
214 p_sys->b_double_rate = false;
215 p_sys->b_half_height = true;
216 p_sys->last_date = 0;
218 vlc_mutex_init( &p_sys->filter_lock );
220 #if defined(CAN_COMPILE_C_ALTIVEC)
221 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
223 p_sys->pf_merge = MergeAltivec;
224 p_sys->pf_end_merge = NULL;
228 #if defined(CAN_COMPILE_SSE)
229 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
231 p_sys->pf_merge = MergeSSE2;
232 p_sys->pf_end_merge = EndMMX;
236 #if defined(CAN_COMPILE_MMXEXT)
237 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
239 p_sys->pf_merge = MergeMMXEXT;
240 p_sys->pf_end_merge = EndMMX;
244 #if defined(CAN_COMPILE_3DNOW)
245 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
247 p_sys->pf_merge = Merge3DNow;
248 p_sys->pf_end_merge = End3DNow;
252 #if defined __ARM_NEON__
253 if( vlc_CPU() & CPU_CAPABILITY_NEON )
255 p_sys->pf_merge = MergeNEON;
256 p_sys->pf_end_merge = NULL;
261 p_sys->pf_merge = MergeGeneric;
262 p_sys->pf_end_merge = NULL;
265 /* Look what method was requested */
266 psz_mode = var_CreateGetString( p_vout, "deinterlace-mode" );
270 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
271 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
273 psz_mode = strdup( "discard" );
276 SetFilterMethod( p_vout, psz_mode );
283 /*****************************************************************************
284 * SetFilterMethod: setup the deinterlace method to use.
285 *****************************************************************************/
286 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method )
288 vout_sys_t *p_sys = p_vout->p_sys;
289 if( !strcmp( psz_method, "mean" ) )
291 p_sys->i_mode = DEINTERLACE_MEAN;
292 p_sys->b_double_rate = false;
293 p_sys->b_half_height = true;
295 else if( !strcmp( psz_method, "blend" )
296 || !strcmp( psz_method, "average" )
297 || !strcmp( psz_method, "combine-fields" ) )
299 p_sys->i_mode = DEINTERLACE_BLEND;
300 p_sys->b_double_rate = false;
301 p_sys->b_half_height = false;
303 else if( !strcmp( psz_method, "bob" )
304 || !strcmp( psz_method, "progressive-scan" ) )
306 p_sys->i_mode = DEINTERLACE_BOB;
307 p_sys->b_double_rate = true;
308 p_sys->b_half_height = false;
310 else if( !strcmp( psz_method, "linear" ) )
312 p_sys->i_mode = DEINTERLACE_LINEAR;
313 p_sys->b_double_rate = true;
314 p_sys->b_half_height = false;
316 else if( !strcmp( psz_method, "x" ) )
318 p_sys->i_mode = DEINTERLACE_X;
319 p_sys->b_double_rate = false;
320 p_sys->b_half_height = false;
324 const bool b_i422 = p_vout->render.i_chroma == VLC_CODEC_I422;
325 if( strcmp( psz_method, "discard" ) )
326 msg_Err( p_vout, "no valid deinterlace mode provided, "
327 "using \"discard\"" );
329 p_sys->i_mode = DEINTERLACE_DISCARD;
330 p_sys->b_double_rate = false;
331 p_sys->b_half_height = !b_i422;
334 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
337 static void GetOutputFormat( vout_thread_t *p_vout,
338 video_format_t *p_dst, const video_format_t *p_src )
342 if( p_vout->p_sys->b_half_height )
344 p_dst->i_height /= 2;
345 p_dst->i_visible_height /= 2;
346 p_dst->i_y_offset /= 2;
347 p_dst->i_sar_den *= 2;
350 if( p_src->i_chroma == VLC_CODEC_I422 )
352 switch( p_vout->p_sys->i_mode )
354 case DEINTERLACE_MEAN:
355 case DEINTERLACE_LINEAR:
357 p_dst->i_chroma = VLC_CODEC_I422;
360 p_dst->i_chroma = VLC_CODEC_I420;
366 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
368 return i_chroma == VLC_CODEC_I420 ||
369 i_chroma == VLC_CODEC_YV12 ||
370 i_chroma == VLC_CODEC_I422;
373 /*****************************************************************************
374 * Init: initialize Deinterlace video thread output method
375 *****************************************************************************/
376 static int Init( vout_thread_t *p_vout )
378 I_OUTPUTPICTURES = 0;
380 if( !IsChromaSupported( p_vout->render.i_chroma ) )
381 return VLC_EGENERIC; /* unknown chroma */
383 /* Initialize the output structure, full of directbuffers since we want
384 * the decoder to output directly to our structures. */
385 p_vout->output.i_chroma = p_vout->render.i_chroma;
386 p_vout->output.i_width = p_vout->render.i_width;
387 p_vout->output.i_height = p_vout->render.i_height;
388 p_vout->output.i_aspect = p_vout->render.i_aspect;
389 p_vout->fmt_out = p_vout->fmt_in;
391 /* Try to open the real video output */
392 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
394 if( p_vout->p_sys->p_vout == NULL )
396 /* Everything failed */
397 msg_Err( p_vout, "cannot open vout, aborting" );
402 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
404 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
406 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
411 /*****************************************************************************
412 * SpawnRealVout: spawn the real video output.
413 *****************************************************************************/
414 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
416 msg_Dbg( p_vout, "spawning the real video output" );
419 GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
421 return vout_Create( p_vout, &fmt );
424 /*****************************************************************************
425 * End: terminate Deinterlace video thread output method
426 *****************************************************************************/
427 static void End( vout_thread_t *p_vout )
429 vout_sys_t *p_sys = p_vout->p_sys;
431 var_DelCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
435 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
436 vout_CloseAndRelease( p_sys->p_vout );
439 vout_filter_ReleaseDirectBuffers( p_vout );
442 /*****************************************************************************
443 * Destroy: destroy Deinterlace video thread output method
444 *****************************************************************************
445 * Terminate an output method created by DeinterlaceCreateOutputMethod
446 *****************************************************************************/
447 static void Destroy( vlc_object_t *p_this )
449 vout_thread_t *p_vout = (vout_thread_t *)p_this;
450 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
451 free( p_vout->p_sys );
455 * Forward mouse event with proper conversion.
457 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
458 vlc_value_t oldval, vlc_value_t newval, void *p_data )
460 vout_thread_t *p_vout = p_data;
461 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
463 if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
466 return var_Set( p_vout, psz_var, newval );
469 /*****************************************************************************
470 * Render: displays previously rendered output
471 *****************************************************************************
472 * This function send the currently rendered image to Deinterlace image,
473 * waits until it is displayed and switch the two rendering buffers, preparing
475 *****************************************************************************/
476 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
478 vout_sys_t *p_sys = p_vout->p_sys;
479 picture_t *pp_outpic[2];
481 /* FIXME are they needed ? */
482 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
483 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
484 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
485 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
487 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
488 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
489 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
490 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
491 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
492 if( p_vout->p_sys->b_half_height )
494 p_sys->p_vout->fmt_in.i_y_offset /= 2;
495 p_sys->p_vout->fmt_in.i_visible_height /= 2;
498 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
500 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
502 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
503 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
504 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
506 video_format_t fmt = p_vout->fmt_out;
507 if( p_vout->p_sys->b_half_height )
509 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
513 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
518 pp_outpic[0] = pp_outpic[1] = NULL;
520 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
522 /* Get a new picture */
523 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
527 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
529 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
532 msleep( VOUT_OUTMEM_SLEEP );
535 pp_outpic[0]->date = p_pic->date;
537 /* If we are using double rate, get an additional new picture */
538 if( p_vout->p_sys->b_double_rate )
540 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
544 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
546 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
547 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
550 msleep( VOUT_OUTMEM_SLEEP );
553 /* 20ms is a bit arbitrary, but it's only for the first image we get */
554 if( !p_vout->p_sys->last_date )
555 pp_outpic[1]->date = p_pic->date + 20000;
557 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
558 p_vout->p_sys->last_date = p_pic->date;
561 switch( p_vout->p_sys->i_mode )
563 case DEINTERLACE_DISCARD:
564 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
565 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
568 case DEINTERLACE_BOB:
569 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
570 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
571 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
572 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
575 case DEINTERLACE_LINEAR:
576 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
577 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
578 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
579 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
582 case DEINTERLACE_MEAN:
583 RenderMean( p_vout, pp_outpic[0], p_pic );
584 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
587 case DEINTERLACE_BLEND:
588 RenderBlend( p_vout, pp_outpic[0], p_pic );
589 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
593 RenderX( pp_outpic[0], p_pic );
594 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
597 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
600 /*****************************************************************************
601 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
602 *****************************************************************************/
603 static void RenderDiscard( vout_thread_t *p_vout,
604 picture_t *p_outpic, picture_t *p_pic, int i_field )
608 /* Copy image and skip lines */
609 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
611 uint8_t *p_in, *p_out_end, *p_out;
614 p_in = p_pic->p[i_plane].p_pixels
615 + i_field * p_pic->p[i_plane].i_pitch;
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 )
626 for( ; p_out < p_out_end ; )
628 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
630 p_out += p_outpic->p[i_plane].i_pitch;
631 p_in += 2 * p_pic->p[i_plane].i_pitch;
637 i_increment = 2 * p_pic->p[i_plane].i_pitch;
639 if( i_plane == Y_PLANE )
641 for( ; p_out < p_out_end ; )
643 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
644 p_out += p_outpic->p[i_plane].i_pitch;
645 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
646 p_out += p_outpic->p[i_plane].i_pitch;
652 for( ; p_out < p_out_end ; )
654 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
655 p_out += p_outpic->p[i_plane].i_pitch;
667 /*****************************************************************************
668 * RenderBob: renders a BOB picture - simple copy
669 *****************************************************************************/
670 static void RenderBob( vout_thread_t *p_vout,
671 picture_t *p_outpic, picture_t *p_pic, int i_field )
675 /* Copy image and skip lines */
676 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
678 uint8_t *p_in, *p_out_end, *p_out;
680 p_in = p_pic->p[i_plane].p_pixels;
681 p_out = p_outpic->p[i_plane].p_pixels;
682 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
683 * p_outpic->p[i_plane].i_visible_lines;
685 switch( p_vout->render.i_chroma )
689 /* For BOTTOM field we need to add the first line */
692 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
693 p_in += p_pic->p[i_plane].i_pitch;
694 p_out += p_outpic->p[i_plane].i_pitch;
697 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
699 for( ; p_out < p_out_end ; )
701 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
703 p_out += p_outpic->p[i_plane].i_pitch;
705 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
707 p_in += 2 * p_pic->p[i_plane].i_pitch;
708 p_out += p_outpic->p[i_plane].i_pitch;
711 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
713 /* For TOP field we need to add the last line */
716 p_in += p_pic->p[i_plane].i_pitch;
717 p_out += p_outpic->p[i_plane].i_pitch;
718 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
723 /* For BOTTOM field we need to add the first line */
726 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
727 p_in += p_pic->p[i_plane].i_pitch;
728 p_out += p_outpic->p[i_plane].i_pitch;
731 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
733 if( i_plane == Y_PLANE )
735 for( ; p_out < p_out_end ; )
737 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
739 p_out += p_outpic->p[i_plane].i_pitch;
741 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
743 p_in += 2 * p_pic->p[i_plane].i_pitch;
744 p_out += p_outpic->p[i_plane].i_pitch;
749 for( ; p_out < p_out_end ; )
751 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
753 p_out += p_outpic->p[i_plane].i_pitch;
754 p_in += 2 * p_pic->p[i_plane].i_pitch;
758 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
760 /* For TOP field we need to add the last line */
763 p_in += p_pic->p[i_plane].i_pitch;
764 p_out += p_outpic->p[i_plane].i_pitch;
765 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
772 #define Merge p_vout->p_sys->pf_merge
773 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
775 /*****************************************************************************
776 * RenderLinear: BOB with linear interpolation
777 *****************************************************************************/
778 static void RenderLinear( vout_thread_t *p_vout,
779 picture_t *p_outpic, picture_t *p_pic, int i_field )
783 /* Copy image and skip lines */
784 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
786 uint8_t *p_in, *p_out_end, *p_out;
788 p_in = p_pic->p[i_plane].p_pixels;
789 p_out = p_outpic->p[i_plane].p_pixels;
790 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
791 * p_outpic->p[i_plane].i_visible_lines;
793 /* For BOTTOM field we need to add the first line */
796 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
797 p_in += p_pic->p[i_plane].i_pitch;
798 p_out += p_outpic->p[i_plane].i_pitch;
801 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
803 for( ; p_out < p_out_end ; )
805 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
807 p_out += p_outpic->p[i_plane].i_pitch;
809 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
810 p_pic->p[i_plane].i_pitch );
812 p_in += 2 * p_pic->p[i_plane].i_pitch;
813 p_out += p_outpic->p[i_plane].i_pitch;
816 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
818 /* For TOP field we need to add the last line */
821 p_in += p_pic->p[i_plane].i_pitch;
822 p_out += p_outpic->p[i_plane].i_pitch;
823 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
829 static void RenderMean( vout_thread_t *p_vout,
830 picture_t *p_outpic, picture_t *p_pic )
834 /* Copy image and skip lines */
835 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
837 uint8_t *p_in, *p_out_end, *p_out;
839 p_in = p_pic->p[i_plane].p_pixels;
841 p_out = p_outpic->p[i_plane].p_pixels;
842 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
843 * p_outpic->p[i_plane].i_visible_lines;
845 /* All lines: mean value */
846 for( ; p_out < p_out_end ; )
848 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
849 p_pic->p[i_plane].i_pitch );
851 p_out += p_outpic->p[i_plane].i_pitch;
852 p_in += 2 * p_pic->p[i_plane].i_pitch;
858 static void RenderBlend( vout_thread_t *p_vout,
859 picture_t *p_outpic, picture_t *p_pic )
863 /* Copy image and skip lines */
864 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
866 uint8_t *p_in, *p_out_end, *p_out;
868 p_in = p_pic->p[i_plane].p_pixels;
870 p_out = p_outpic->p[i_plane].p_pixels;
871 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
872 * p_outpic->p[i_plane].i_visible_lines;
874 switch( p_vout->render.i_chroma )
878 /* First line: simple copy */
879 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
880 p_out += p_outpic->p[i_plane].i_pitch;
882 /* Remaining lines: mean value */
883 for( ; p_out < p_out_end ; )
885 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
886 p_pic->p[i_plane].i_pitch );
888 p_out += p_outpic->p[i_plane].i_pitch;
889 p_in += p_pic->p[i_plane].i_pitch;
894 /* First line: simple copy */
895 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
896 p_out += p_outpic->p[i_plane].i_pitch;
898 /* Remaining lines: mean value */
899 if( i_plane == Y_PLANE )
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 += p_pic->p[i_plane].i_pitch;
913 for( ; p_out < p_out_end ; )
915 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
916 p_pic->p[i_plane].i_pitch );
918 p_out += p_outpic->p[i_plane].i_pitch;
919 p_in += 2*p_pic->p[i_plane].i_pitch;
930 static void MergeGeneric( void *_p_dest, const void *_p_s1,
931 const void *_p_s2, size_t i_bytes )
933 uint8_t* p_dest = (uint8_t*)_p_dest;
934 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
935 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
936 uint8_t* p_end = p_dest + i_bytes - 8;
938 while( p_dest < p_end )
940 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
941 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
942 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
943 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
944 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
945 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
946 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
947 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
952 while( p_dest < p_end )
954 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
958 #if defined(CAN_COMPILE_MMXEXT)
959 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
962 uint8_t* p_dest = (uint8_t*)_p_dest;
963 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
964 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
965 uint8_t* p_end = p_dest + i_bytes - 8;
966 while( p_dest < p_end )
968 __asm__ __volatile__( "movq %2,%%mm1;"
970 "movq %%mm1, %0" :"=m" (*p_dest):
980 while( p_dest < p_end )
982 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
987 #if defined(CAN_COMPILE_3DNOW)
988 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
991 uint8_t* p_dest = (uint8_t*)_p_dest;
992 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
993 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
994 uint8_t* p_end = p_dest + i_bytes - 8;
995 while( p_dest < p_end )
997 __asm__ __volatile__( "movq %2,%%mm1;"
999 "movq %%mm1, %0" :"=m" (*p_dest):
1009 while( p_dest < p_end )
1011 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1016 #if defined(CAN_COMPILE_SSE)
1017 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1020 uint8_t* p_dest = (uint8_t*)_p_dest;
1021 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1022 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1024 while( (uintptr_t)p_s1 % 16 )
1026 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1028 p_end = p_dest + i_bytes - 16;
1029 while( p_dest < p_end )
1031 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1033 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1043 while( p_dest < p_end )
1045 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1050 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1051 static void EndMMX( void )
1053 __asm__ __volatile__( "emms" :: );
1057 #if defined(CAN_COMPILE_3DNOW)
1058 static void End3DNow( void )
1060 __asm__ __volatile__( "femms" :: );
1064 #ifdef CAN_COMPILE_C_ALTIVEC
1065 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1066 const void *_p_s2, size_t i_bytes )
1068 uint8_t *p_dest = (uint8_t *)_p_dest;
1069 uint8_t *p_s1 = (uint8_t *)_p_s1;
1070 uint8_t *p_s2 = (uint8_t *)_p_s2;
1071 uint8_t *p_end = p_dest + i_bytes - 15;
1073 /* Use C until the first 16-bytes aligned destination pixel */
1074 while( (uintptr_t)p_dest & 0xF )
1076 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1079 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1081 /* Unaligned source */
1082 vector unsigned char s1v, s2v, destv;
1083 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1084 vector unsigned char perm1v, perm2v;
1086 perm1v = vec_lvsl( 0, p_s1 );
1087 perm2v = vec_lvsl( 0, p_s2 );
1088 s1oldv = vec_ld( 0, p_s1 );
1089 s2oldv = vec_ld( 0, p_s2 );
1091 while( p_dest < p_end )
1093 s1newv = vec_ld( 16, p_s1 );
1094 s2newv = vec_ld( 16, p_s2 );
1095 s1v = vec_perm( s1oldv, s1newv, perm1v );
1096 s2v = vec_perm( s2oldv, s2newv, perm2v );
1099 destv = vec_avg( s1v, s2v );
1100 vec_st( destv, 0, p_dest );
1109 /* Aligned source */
1110 vector unsigned char s1v, s2v, destv;
1112 while( p_dest < p_end )
1114 s1v = vec_ld( 0, p_s1 );
1115 s2v = vec_ld( 0, p_s2 );
1116 destv = vec_avg( s1v, s2v );
1117 vec_st( destv, 0, p_dest );
1127 while( p_dest < p_end )
1129 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1135 static void MergeNEON (void *restrict out, const void *in1,
1136 const void *in2, size_t n)
1138 uint8_t *outp = out;
1139 const uint8_t *in1p = in1;
1140 const uint8_t *in2p = in2;
1141 size_t mis = ((uintptr_t)outp) & 15;
1145 MergeGeneric (outp, in1p, in2p, mis);
1152 uint8_t *end = outp + (n & ~15);
1154 if ((((uintptr_t)in1p)|((uintptr_t)in2p)) & 15)
1157 "vld1.u8 {q0-q1}, [%[in1]]!\n"
1158 "vld1.u8 {q2-q3}, [%[in2]]!\n"
1159 "vhadd.u8 q4, q0, q2\n"
1160 "vld1.u8 {q6-q7}, [%[in1]]!\n"
1161 "vhadd.u8 q5, q1, q3\n"
1162 "vld1.u8 {q8-q9}, [%[in2]]!\n"
1163 "vhadd.u8 q10, q6, q8\n"
1164 "vhadd.u8 q11, q7, q9\n"
1165 "vst1.u8 {q4-q5}, [%[out],:128]!\n"
1166 "vst1.u8 {q10-q11}, [%[out],:128]!\n"
1167 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1169 : "q0", "q1", "q2", "memory");
1173 "vld1.u8 {q0-q1}, [%[in1],:128]!\n"
1174 "vld1.u8 {q2-q3}, [%[in2],:128]!\n"
1175 "vhadd.u8 q4, q0, q2\n"
1176 "vld1.u8 {q6-q7}, [%[in1],:128]!\n"
1177 "vhadd.u8 q5, q1, q3\n"
1178 "vld1.u8 {q8-q9}, [%[in2],:128]!\n"
1179 "vhadd.u8 q10, q6, q8\n"
1180 "vhadd.u8 q11, q7, q9\n"
1181 "vst1.u8 {q4-q5}, [%[out],:128]!\n"
1182 "vst1.u8 {q10-q11}, [%[out],:128]!\n"
1183 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1185 : "q0", "q1", "q2", "memory");
1188 MergeGeneric (outp, in1p, in2p, n);
1192 /*****************************************************************************
1193 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1194 * and apply a process to recreate the bottom field :
1195 * If a 8x8 block is classified as :
1196 * - progressive: it applies a small blend (1,6,1)
1198 * * in the MMX version: we do a ME between the 2 fields, if there is a
1199 * good match we use MC to recreate the bottom field (with a small
1201 * * otherwise: it recreates the bottom field by an edge oriented
1203 *****************************************************************************/
1205 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1206 * XXX: It need to access to 8x10
1207 * We use more than 8 lines to help with scrolling (text)
1208 * (and because XDeint8x8Frame use line 9)
1209 * XXX: smooth/uniform area with noise detection doesn't works well
1210 * but it's not really a problem because they don't have much details anyway
1212 static inline int ssd( int a ) { return a*a; }
1213 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1219 /* Detect interlacing */
1221 for( y = 0; y < 7; y += 2 )
1224 for( x = 0; x < 8; x++ )
1226 fr += ssd(src[ x] - src[1*i_src+x]) +
1227 ssd(src[i_src+x] - src[2*i_src+x]);
1228 ff += ssd(src[ x] - src[2*i_src+x]) +
1229 ssd(src[i_src+x] - src[3*i_src+x]);
1231 if( ff < 6*fr/8 && fr > 32 )
1237 return fc < 1 ? false : true;
1239 #ifdef CAN_COMPILE_MMXEXT
1240 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1247 /* Detect interlacing */
1249 pxor_r2r( mm7, mm7 );
1250 for( y = 0; y < 9; y += 2 )
1253 pxor_r2r( mm5, mm5 );
1254 pxor_r2r( mm6, mm6 );
1255 for( x = 0; x < 8; x+=4 )
1257 movd_m2r( src[ x], mm0 );
1258 movd_m2r( src[1*i_src+x], mm1 );
1259 movd_m2r( src[2*i_src+x], mm2 );
1260 movd_m2r( src[3*i_src+x], mm3 );
1262 punpcklbw_r2r( mm7, mm0 );
1263 punpcklbw_r2r( mm7, mm1 );
1264 punpcklbw_r2r( mm7, mm2 );
1265 punpcklbw_r2r( mm7, mm3 );
1267 movq_r2r( mm0, mm4 );
1269 psubw_r2r( mm1, mm0 );
1270 psubw_r2r( mm2, mm4 );
1272 psubw_r2r( mm1, mm2 );
1273 psubw_r2r( mm1, mm3 );
1275 pmaddwd_r2r( mm0, mm0 );
1276 pmaddwd_r2r( mm4, mm4 );
1277 pmaddwd_r2r( mm2, mm2 );
1278 pmaddwd_r2r( mm3, mm3 );
1279 paddd_r2r( mm0, mm2 );
1280 paddd_r2r( mm4, mm3 );
1281 paddd_r2r( mm2, mm5 );
1282 paddd_r2r( mm3, mm6 );
1285 movq_r2r( mm5, mm0 );
1286 psrlq_i2r( 32, mm0 );
1287 paddd_r2r( mm0, mm5 );
1288 movd_r2m( mm5, fr );
1290 movq_r2r( mm6, mm0 );
1291 psrlq_i2r( 32, mm0 );
1292 paddd_r2r( mm0, mm6 );
1293 movd_r2m( mm6, ff );
1295 if( ff < 6*fr/8 && fr > 32 )
1304 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1305 uint8_t *src1, int i_src1,
1306 uint8_t *src2, int i_src2 )
1311 for( y = 0; y < 8; y += 2 )
1313 memcpy( dst, src1, 8 );
1316 for( x = 0; x < 8; x++ )
1317 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1325 #ifdef CAN_COMPILE_MMXEXT
1326 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1327 uint8_t *src1, int i_src1,
1328 uint8_t *src2, int i_src2 )
1330 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1334 pxor_r2r( mm7, mm7 );
1335 for( y = 0; y < 8; y += 2 )
1337 for( x = 0; x < 8; x +=4 )
1339 movd_m2r( src1[x], mm0 );
1340 movd_r2m( mm0, dst[x] );
1342 movd_m2r( src2[x], mm1 );
1343 movd_m2r( src1[i_src1+x], mm2 );
1345 punpcklbw_r2r( mm7, mm0 );
1346 punpcklbw_r2r( mm7, mm1 );
1347 punpcklbw_r2r( mm7, mm2 );
1348 paddw_r2r( mm1, mm1 );
1349 movq_r2r( mm1, mm3 );
1350 paddw_r2r( mm3, mm3 );
1351 paddw_r2r( mm2, mm0 );
1352 paddw_r2r( mm3, mm1 );
1353 paddw_m2r( m_4, mm1 );
1354 paddw_r2r( mm1, mm0 );
1355 psraw_i2r( 3, mm0 );
1356 packuswb_r2r( mm7, mm0 );
1357 movd_r2m( mm0, dst[i_dst+x] );
1368 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1371 for( y = 0; y < 8; y++ )
1372 memset( &dst[y*i_dst], v, 8 );
1375 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1378 * TODO: a better one for the inner part.
1380 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1381 uint8_t *src, int i_src )
1386 for( y = 0; y < 8; y += 2 )
1388 memcpy( dst, src, 8 );
1391 for( x = 0; x < 8; x++ )
1392 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1397 #ifdef CAN_COMPILE_MMXEXT
1398 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1399 uint8_t *src, int i_src )
1404 for( y = 0; y < 8; y += 2 )
1406 movq_m2r( src[0], mm0 );
1407 movq_r2m( mm0, dst[0] );
1410 movq_m2r( src[2*i_src], mm1 );
1411 pavgb_r2r( mm1, mm0 );
1413 movq_r2m( mm0, dst[0] );
1421 /* XDeint8x8Field: Edge oriented interpolation
1422 * (Need -4 and +5 pixels H, +1 line)
1424 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1425 uint8_t *src, int i_src )
1430 for( y = 0; y < 8; y += 2 )
1432 memcpy( dst, src, 8 );
1435 for( x = 0; x < 8; x++ )
1437 uint8_t *src2 = &src[2*i_src];
1438 /* I use 8 pixels just to match the MMX version, but it's overkill
1439 * 5 would be enough (less isn't good) */
1440 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1441 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1442 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1443 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1445 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1446 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1447 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1448 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1450 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1451 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1452 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1453 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1455 if( c0 < c1 && c1 <= c2 )
1456 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1457 else if( c2 < c1 && c1 <= c0 )
1458 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1460 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1467 #ifdef CAN_COMPILE_MMXEXT
1468 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1469 uint8_t *src, int i_src )
1474 for( y = 0; y < 8; y += 2 )
1476 memcpy( dst, src, 8 );
1479 for( x = 0; x < 8; x++ )
1481 uint8_t *src2 = &src[2*i_src];
1484 movq_m2r( src[x-2], mm0 );
1485 movq_m2r( src[x-3], mm1 );
1486 movq_m2r( src[x-4], mm2 );
1488 psadbw_m2r( src2[x-4], mm0 );
1489 psadbw_m2r( src2[x-3], mm1 );
1490 psadbw_m2r( src2[x-2], mm2 );
1492 movd_r2m( mm0, c2 );
1493 movd_r2m( mm1, c1 );
1494 movd_r2m( mm2, c0 );
1496 if( c0 < c1 && c1 <= c2 )
1497 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1498 else if( c2 < c1 && c1 <= c0 )
1499 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1501 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1510 /* NxN arbitray size (and then only use pixel in the NxN block)
1512 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1513 int i_height, int i_width )
1520 /* Detect interlacing */
1521 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1524 for( y = 0; y < i_height - 2; y += 2 )
1526 const uint8_t *s = &src[y*i_src];
1527 for( x = 0; x < i_width; x++ )
1529 fr += ssd(s[ x] - s[1*i_src+x]);
1530 ff += ssd(s[ x] - s[2*i_src+x]);
1532 if( ff < fr && fr > i_width / 2 )
1536 return fc < 2 ? false : true;
1539 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1540 uint8_t *src, int i_src,
1541 int i_width, int i_height )
1546 for( y = 0; y < i_height; y += 2 )
1548 memcpy( dst, src, i_width );
1551 if( y < i_height - 2 )
1553 for( x = 0; x < i_width; x++ )
1554 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1558 /* Blend last line */
1559 for( x = 0; x < i_width; x++ )
1560 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1567 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1568 uint8_t *src, int i_src,
1569 int i_width, int i_height )
1574 for( y = 0; y < i_height; y += 2 )
1576 memcpy( dst, src, i_width );
1579 if( y < i_height - 2 )
1581 for( x = 0; x < i_width; x++ )
1582 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1586 /* Blend last line */
1587 for( x = 0; x < i_width; x++ )
1588 dst[x] = (src[x] + src[i_src+x]) >> 1;
1595 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1596 int i_width, int i_height )
1598 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1599 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1601 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1605 static inline int median( int a, int b, int c )
1607 int min = a, max =a;
1618 return a + b + c - min - max;
1624 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1625 uint8_t *src, int i_src,
1626 const int i_mbx, int i_modx )
1630 for( x = 0; x < i_mbx; x++ )
1633 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1635 if( x == 0 || x == i_mbx - 1 )
1636 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1638 XDeint8x8FieldC( dst, i_dst, src, i_src );
1642 XDeint8x8MergeC( dst, i_dst,
1643 &src[0*i_src], 2*i_src,
1644 &src[1*i_src], 2*i_src );
1652 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1654 #ifdef CAN_COMPILE_MMXEXT
1655 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1656 uint8_t *src, int i_src,
1657 const int i_mbx, int i_modx )
1661 /* Reset current line */
1662 for( x = 0; x < i_mbx; x++ )
1665 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1667 if( x == 0 || x == i_mbx - 1 )
1668 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1670 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1674 XDeint8x8MergeMMXEXT( dst, i_dst,
1675 &src[0*i_src], 2*i_src,
1676 &src[1*i_src], 2*i_src );
1684 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1688 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1692 /* Copy image and skip lines */
1693 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1695 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1696 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1698 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1699 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1701 const int i_dst = p_outpic->p[i_plane].i_pitch;
1702 const int i_src = p_pic->p[i_plane].i_pitch;
1706 for( y = 0; y < i_mby; y++ )
1708 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1709 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1711 #ifdef CAN_COMPILE_MMXEXT
1712 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1713 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1716 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1719 /* Last line (C only)*/
1722 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1723 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1725 for( x = 0; x < i_mbx; x++ )
1727 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1734 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1738 #ifdef CAN_COMPILE_MMXEXT
1739 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1744 /*****************************************************************************
1745 * FilterCallback: called when changing the deinterlace method on the fly.
1746 *****************************************************************************/
1747 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1748 vlc_value_t oldval, vlc_value_t newval,
1751 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1752 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1753 vout_sys_t *p_sys = p_vout->p_sys;
1755 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1757 vlc_mutex_lock( &p_sys->filter_lock );
1758 const bool b_old_half_height = p_sys->b_half_height;
1760 SetFilterMethod( p_vout, newval.psz_string );
1762 if( !b_old_half_height == !p_sys->b_half_height )
1764 vlc_mutex_unlock( &p_sys->filter_lock );
1768 /* We need to kill the old vout */
1771 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
1772 vout_CloseAndRelease( p_sys->p_vout );
1775 /* Try to open a new video output */
1776 p_sys->p_vout = SpawnRealVout( p_vout );
1778 if( p_sys->p_vout == NULL )
1780 /* Everything failed */
1781 msg_Err( p_vout, "cannot open vout, aborting" );
1783 vlc_mutex_unlock( &p_sys->filter_lock );
1784 return VLC_EGENERIC;
1787 vout_filter_AddChild( p_vout, p_sys->p_vout, MouseEvent );
1789 vlc_mutex_unlock( &p_sys->filter_lock );
1793 /*****************************************************************************
1794 * video filter2 functions
1795 *****************************************************************************/
1796 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1798 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1799 picture_t *p_pic_dst;
1801 /* Request output picture */
1802 p_pic_dst = filter_NewPicture( p_filter );
1803 if( p_pic_dst == NULL )
1805 picture_Release( p_pic );
1809 switch( p_vout->p_sys->i_mode )
1811 case DEINTERLACE_DISCARD:
1812 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1815 case DEINTERLACE_BOB:
1817 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1818 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1822 case DEINTERLACE_LINEAR:
1824 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1825 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1827 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1828 picture_Release( p_pic_dst );
1829 picture_Release( p_pic );
1832 case DEINTERLACE_MEAN:
1833 RenderMean( p_vout, p_pic_dst, p_pic );
1836 case DEINTERLACE_BLEND:
1837 RenderBlend( p_vout, p_pic_dst, p_pic );
1841 RenderX( p_pic_dst, p_pic );
1845 picture_CopyProperties( p_pic_dst, p_pic );
1846 p_pic_dst->b_progressive = true;
1848 picture_Release( p_pic );
1852 /*****************************************************************************
1854 *****************************************************************************/
1855 static int OpenFilter( vlc_object_t *p_this )
1857 filter_t *p_filter = (filter_t*)p_this;
1858 vout_thread_t *p_vout;
1861 if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
1862 return VLC_EGENERIC;
1864 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1866 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1867 vlc_object_attach( p_vout, p_filter );
1868 p_filter->p_sys = (filter_sys_t *)p_vout;
1869 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1871 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1873 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1875 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1876 var_Set( p_filter, "deinterlace-mode", val );
1877 free( val.psz_string );
1879 if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1881 vlc_object_detach( p_vout );
1882 vlc_object_release( p_vout );
1883 return VLC_EGENERIC;
1887 GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
1888 if( !p_filter->b_allow_fmt_out_change &&
1889 ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
1890 fmt.i_height != p_filter->fmt_in.video.i_height ) )
1892 CloseFilter( VLC_OBJECT(p_filter) );
1893 return VLC_EGENERIC;
1895 p_filter->fmt_out.video = fmt;
1896 p_filter->fmt_out.i_codec = fmt.i_chroma;
1897 p_filter->pf_video_filter = Deinterlace;
1899 msg_Dbg( p_filter, "deinterlacing" );
1904 /*****************************************************************************
1905 * CloseFilter: clean up the filter
1906 *****************************************************************************/
1907 static void CloseFilter( vlc_object_t *p_this )
1909 filter_t *p_filter = (filter_t*)p_this;
1910 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1912 Destroy( VLC_OBJECT(p_vout) );
1913 vlc_object_detach( p_vout );
1914 vlc_object_release( p_vout );