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 );
96 #if defined __ARM_NEON__
97 static void MergeNEON (void *, const void *, const void *, size_t);
100 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method );
101 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
103 static int OpenFilter( vlc_object_t *p_this );
104 static void CloseFilter( vlc_object_t *p_this );
106 /*****************************************************************************
107 * Callback prototypes
108 *****************************************************************************/
109 static int FilterCallback( vlc_object_t *, char const *,
110 vlc_value_t, vlc_value_t, void * );
112 /*****************************************************************************
114 *****************************************************************************/
115 #define MODE_TEXT N_("Deinterlace mode")
116 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
118 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
119 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
121 #define FILTER_CFG_PREFIX "sout-deinterlace-"
123 static const char *const mode_list[] = {
124 "discard", "blend", "mean", "bob", "linear", "x" };
125 static const char *const mode_list_text[] = {
126 N_("Discard"), N_("Blend"), N_("Mean"), N_("Bob"), N_("Linear"), "X" };
129 set_description( N_("Deinterlacing video filter") )
130 set_shortname( N_("Deinterlace" ))
131 set_capability( "video filter", 0 )
132 set_category( CAT_VIDEO )
133 set_subcategory( SUBCAT_VIDEO_VFILTER )
135 set_section( N_("Display"),NULL)
136 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
137 MODE_LONGTEXT, false )
138 change_string_list( mode_list, mode_list_text, 0 )
141 add_shortcut( "deinterlace" )
142 set_callbacks( Create, Destroy )
145 set_capability( "video filter2", 0 )
146 set_section( N_("Streaming"),NULL)
147 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
148 SOUT_MODE_LONGTEXT, false )
149 change_string_list( mode_list, mode_list_text, 0 )
150 add_shortcut( "deinterlace" )
151 set_callbacks( OpenFilter, CloseFilter )
154 static const char *const ppsz_filter_options[] = {
158 /*****************************************************************************
159 * vout_sys_t: Deinterlace video output method descriptor
160 *****************************************************************************
161 * This structure is part of the video output thread descriptor.
162 * It describes the Deinterlace specific properties of an output thread.
163 *****************************************************************************/
166 int i_mode; /* Deinterlace mode */
167 bool b_double_rate; /* Shall we double the framerate? */
168 bool b_half_height; /* Shall be devide the height by 2 */
173 vout_thread_t *p_vout;
175 vlc_mutex_t filter_lock;
177 void (*pf_merge) ( void *, const void *, const void *, size_t );
178 void (*pf_end_merge) ( void );
181 /*****************************************************************************
182 * Control: control facility for the vout (forwards to child vout)
183 *****************************************************************************/
184 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
186 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
189 /*****************************************************************************
190 * Create: allocates Deinterlace video thread output method
191 *****************************************************************************
192 * This function allocates and initializes a Deinterlace vout method.
193 *****************************************************************************/
194 static int Create( vlc_object_t *p_this )
196 vout_thread_t *p_vout = (vout_thread_t *)p_this;
200 /* Allocate structure */
201 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
202 if( p_vout->p_sys == NULL )
205 p_vout->pf_init = Init;
206 p_vout->pf_end = End;
207 p_vout->pf_manage = NULL;
208 p_vout->pf_render = Render;
209 p_vout->pf_display = NULL;
210 p_vout->pf_control = Control;
212 p_sys->i_mode = DEINTERLACE_DISCARD;
213 p_sys->b_double_rate = false;
214 p_sys->b_half_height = true;
215 p_sys->last_date = 0;
217 vlc_mutex_init( &p_sys->filter_lock );
219 #if defined(CAN_COMPILE_C_ALTIVEC)
220 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
222 p_sys->pf_merge = MergeAltivec;
223 p_sys->pf_end_merge = NULL;
227 #if defined(CAN_COMPILE_SSE)
228 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
230 p_sys->pf_merge = MergeSSE2;
231 p_sys->pf_end_merge = EndMMX;
235 #if defined(CAN_COMPILE_MMXEXT)
236 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
238 p_sys->pf_merge = MergeMMXEXT;
239 p_sys->pf_end_merge = EndMMX;
243 #if defined(CAN_COMPILE_3DNOW)
244 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
246 p_sys->pf_merge = Merge3DNow;
247 p_sys->pf_end_merge = End3DNow;
251 #if defined __ARM_NEON__
252 if( vlc_CPU() & CPU_CAPABILITY_NEON )
254 p_sys->pf_merge = MergeNEON;
255 p_sys->pf_end_merge = NULL;
260 p_sys->pf_merge = MergeGeneric;
261 p_sys->pf_end_merge = NULL;
264 /* Look what method was requested */
265 psz_mode = var_CreateGetString( p_vout, "deinterlace-mode" );
269 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
270 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
272 psz_mode = strdup( "discard" );
275 SetFilterMethod( p_vout, psz_mode );
282 /*****************************************************************************
283 * SetFilterMethod: setup the deinterlace method to use.
284 *****************************************************************************/
285 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method )
287 vout_sys_t *p_sys = p_vout->p_sys;
288 if( !strcmp( psz_method, "mean" ) )
290 p_sys->i_mode = DEINTERLACE_MEAN;
291 p_sys->b_double_rate = false;
292 p_sys->b_half_height = true;
294 else if( !strcmp( psz_method, "blend" )
295 || !strcmp( psz_method, "average" )
296 || !strcmp( psz_method, "combine-fields" ) )
298 p_sys->i_mode = DEINTERLACE_BLEND;
299 p_sys->b_double_rate = false;
300 p_sys->b_half_height = false;
302 else if( !strcmp( psz_method, "bob" )
303 || !strcmp( psz_method, "progressive-scan" ) )
305 p_sys->i_mode = DEINTERLACE_BOB;
306 p_sys->b_double_rate = true;
307 p_sys->b_half_height = false;
309 else if( !strcmp( psz_method, "linear" ) )
311 p_sys->i_mode = DEINTERLACE_LINEAR;
312 p_sys->b_double_rate = true;
313 p_sys->b_half_height = false;
315 else if( !strcmp( psz_method, "x" ) )
317 p_sys->i_mode = DEINTERLACE_X;
318 p_sys->b_double_rate = false;
319 p_sys->b_half_height = false;
323 const bool b_i422 = p_vout->render.i_chroma == VLC_CODEC_I422;
324 if( strcmp( psz_method, "discard" ) )
325 msg_Err( p_vout, "no valid deinterlace mode provided, "
326 "using \"discard\"" );
328 p_sys->i_mode = DEINTERLACE_DISCARD;
329 p_sys->b_double_rate = false;
330 p_sys->b_half_height = !b_i422;
333 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
336 static void GetOutputFormat( vout_thread_t *p_vout,
337 video_format_t *p_dst, const video_format_t *p_src )
341 if( p_vout->p_sys->b_half_height )
343 p_dst->i_height /= 2;
344 p_dst->i_visible_height /= 2;
345 p_dst->i_y_offset /= 2;
346 p_dst->i_sar_den *= 2;
349 if( p_src->i_chroma == VLC_CODEC_I422 )
351 switch( p_vout->p_sys->i_mode )
353 case DEINTERLACE_MEAN:
354 case DEINTERLACE_LINEAR:
356 p_dst->i_chroma = VLC_CODEC_I422;
359 p_dst->i_chroma = VLC_CODEC_I420;
365 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
367 return i_chroma == VLC_CODEC_I420 ||
368 i_chroma == VLC_CODEC_YV12 ||
369 i_chroma == VLC_CODEC_I422;
372 /*****************************************************************************
373 * Init: initialize Deinterlace video thread output method
374 *****************************************************************************/
375 static int Init( vout_thread_t *p_vout )
377 I_OUTPUTPICTURES = 0;
379 if( !IsChromaSupported( p_vout->render.i_chroma ) )
380 return VLC_EGENERIC; /* unknown chroma */
382 /* Initialize the output structure, full of directbuffers since we want
383 * the decoder to output directly to our structures. */
384 p_vout->output.i_chroma = p_vout->render.i_chroma;
385 p_vout->output.i_width = p_vout->render.i_width;
386 p_vout->output.i_height = p_vout->render.i_height;
387 p_vout->output.i_aspect = p_vout->render.i_aspect;
388 p_vout->fmt_out = p_vout->fmt_in;
390 /* Try to open the real video output */
391 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
393 if( p_vout->p_sys->p_vout == NULL )
395 /* Everything failed */
396 msg_Err( p_vout, "cannot open vout, aborting" );
401 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
403 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
405 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
410 /*****************************************************************************
411 * SpawnRealVout: spawn the real video output.
412 *****************************************************************************/
413 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
415 msg_Dbg( p_vout, "spawning the real video output" );
418 GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
420 return vout_Create( p_vout, &fmt );
423 /*****************************************************************************
424 * End: terminate Deinterlace video thread output method
425 *****************************************************************************/
426 static void End( vout_thread_t *p_vout )
428 vout_sys_t *p_sys = p_vout->p_sys;
430 var_DelCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
434 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
435 vout_CloseAndRelease( p_sys->p_vout );
438 vout_filter_ReleaseDirectBuffers( p_vout );
441 /*****************************************************************************
442 * Destroy: destroy Deinterlace video thread output method
443 *****************************************************************************
444 * Terminate an output method created by DeinterlaceCreateOutputMethod
445 *****************************************************************************/
446 static void Destroy( vlc_object_t *p_this )
448 vout_thread_t *p_vout = (vout_thread_t *)p_this;
449 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
450 free( p_vout->p_sys );
454 * Forward mouse event with proper conversion.
456 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
457 vlc_value_t oldval, vlc_value_t newval, void *p_data )
459 vout_thread_t *p_vout = p_data;
460 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
462 if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
465 return var_Set( p_vout, psz_var, newval );
468 /*****************************************************************************
469 * Render: displays previously rendered output
470 *****************************************************************************
471 * This function send the currently rendered image to Deinterlace image,
472 * waits until it is displayed and switch the two rendering buffers, preparing
474 *****************************************************************************/
475 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
477 vout_sys_t *p_sys = p_vout->p_sys;
478 picture_t *pp_outpic[2];
480 /* FIXME are they needed ? */
481 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
482 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
483 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
484 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
486 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
487 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
488 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
489 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
490 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
491 if( p_vout->p_sys->b_half_height )
493 p_sys->p_vout->fmt_in.i_y_offset /= 2;
494 p_sys->p_vout->fmt_in.i_visible_height /= 2;
497 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
499 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
501 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
502 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
503 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
505 video_format_t fmt = p_vout->fmt_out;
506 if( p_vout->p_sys->b_half_height )
508 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
512 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
517 pp_outpic[0] = pp_outpic[1] = NULL;
519 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
521 /* Get a new picture */
522 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
526 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
528 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
531 msleep( VOUT_OUTMEM_SLEEP );
534 pp_outpic[0]->date = p_pic->date;
536 /* If we are using double rate, get an additional new picture */
537 if( p_vout->p_sys->b_double_rate )
539 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
543 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
545 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
546 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
549 msleep( VOUT_OUTMEM_SLEEP );
552 /* 20ms is a bit arbitrary, but it's only for the first image we get */
553 if( !p_vout->p_sys->last_date )
554 pp_outpic[1]->date = p_pic->date + 20000;
556 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
557 p_vout->p_sys->last_date = p_pic->date;
560 switch( p_vout->p_sys->i_mode )
562 case DEINTERLACE_DISCARD:
563 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
564 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
567 case DEINTERLACE_BOB:
568 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
569 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
570 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
571 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
574 case DEINTERLACE_LINEAR:
575 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
576 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
577 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
578 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
581 case DEINTERLACE_MEAN:
582 RenderMean( p_vout, pp_outpic[0], p_pic );
583 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
586 case DEINTERLACE_BLEND:
587 RenderBlend( p_vout, pp_outpic[0], p_pic );
588 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
592 RenderX( pp_outpic[0], p_pic );
593 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
596 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
599 /*****************************************************************************
600 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
601 *****************************************************************************/
602 static void RenderDiscard( vout_thread_t *p_vout,
603 picture_t *p_outpic, picture_t *p_pic, int i_field )
607 /* Copy image and skip lines */
608 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
610 uint8_t *p_in, *p_out_end, *p_out;
613 p_in = p_pic->p[i_plane].p_pixels
614 + i_field * p_pic->p[i_plane].i_pitch;
616 p_out = p_outpic->p[i_plane].p_pixels;
617 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
618 * p_outpic->p[i_plane].i_visible_lines;
620 switch( p_vout->render.i_chroma )
625 for( ; p_out < p_out_end ; )
627 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
629 p_out += p_outpic->p[i_plane].i_pitch;
630 p_in += 2 * p_pic->p[i_plane].i_pitch;
636 i_increment = 2 * p_pic->p[i_plane].i_pitch;
638 if( i_plane == Y_PLANE )
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;
644 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
645 p_out += p_outpic->p[i_plane].i_pitch;
651 for( ; p_out < p_out_end ; )
653 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
654 p_out += p_outpic->p[i_plane].i_pitch;
666 /*****************************************************************************
667 * RenderBob: renders a BOB picture - simple copy
668 *****************************************************************************/
669 static void RenderBob( vout_thread_t *p_vout,
670 picture_t *p_outpic, picture_t *p_pic, int i_field )
674 /* Copy image and skip lines */
675 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
677 uint8_t *p_in, *p_out_end, *p_out;
679 p_in = p_pic->p[i_plane].p_pixels;
680 p_out = p_outpic->p[i_plane].p_pixels;
681 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
682 * p_outpic->p[i_plane].i_visible_lines;
684 switch( p_vout->render.i_chroma )
688 /* For BOTTOM field we need to add the first line */
691 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
692 p_in += p_pic->p[i_plane].i_pitch;
693 p_out += p_outpic->p[i_plane].i_pitch;
696 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
698 for( ; p_out < p_out_end ; )
700 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
702 p_out += p_outpic->p[i_plane].i_pitch;
704 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
706 p_in += 2 * p_pic->p[i_plane].i_pitch;
707 p_out += p_outpic->p[i_plane].i_pitch;
710 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
712 /* For TOP field we need to add the last line */
715 p_in += p_pic->p[i_plane].i_pitch;
716 p_out += p_outpic->p[i_plane].i_pitch;
717 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
722 /* For BOTTOM field we need to add the first line */
725 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
726 p_in += p_pic->p[i_plane].i_pitch;
727 p_out += p_outpic->p[i_plane].i_pitch;
730 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
732 if( i_plane == Y_PLANE )
734 for( ; p_out < p_out_end ; )
736 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
738 p_out += p_outpic->p[i_plane].i_pitch;
740 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
742 p_in += 2 * p_pic->p[i_plane].i_pitch;
743 p_out += p_outpic->p[i_plane].i_pitch;
748 for( ; p_out < p_out_end ; )
750 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
752 p_out += p_outpic->p[i_plane].i_pitch;
753 p_in += 2 * p_pic->p[i_plane].i_pitch;
757 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
759 /* For TOP field we need to add the last line */
762 p_in += p_pic->p[i_plane].i_pitch;
763 p_out += p_outpic->p[i_plane].i_pitch;
764 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
771 #define Merge p_vout->p_sys->pf_merge
772 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
774 /*****************************************************************************
775 * RenderLinear: BOB with linear interpolation
776 *****************************************************************************/
777 static void RenderLinear( vout_thread_t *p_vout,
778 picture_t *p_outpic, picture_t *p_pic, int i_field )
782 /* Copy image and skip lines */
783 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
785 uint8_t *p_in, *p_out_end, *p_out;
787 p_in = p_pic->p[i_plane].p_pixels;
788 p_out = p_outpic->p[i_plane].p_pixels;
789 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
790 * p_outpic->p[i_plane].i_visible_lines;
792 /* For BOTTOM field we need to add the first line */
795 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
796 p_in += p_pic->p[i_plane].i_pitch;
797 p_out += p_outpic->p[i_plane].i_pitch;
800 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
802 for( ; p_out < p_out_end ; )
804 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
806 p_out += p_outpic->p[i_plane].i_pitch;
808 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
809 p_pic->p[i_plane].i_pitch );
811 p_in += 2 * p_pic->p[i_plane].i_pitch;
812 p_out += p_outpic->p[i_plane].i_pitch;
815 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
817 /* For TOP field we need to add the last line */
820 p_in += p_pic->p[i_plane].i_pitch;
821 p_out += p_outpic->p[i_plane].i_pitch;
822 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
828 static void RenderMean( vout_thread_t *p_vout,
829 picture_t *p_outpic, picture_t *p_pic )
833 /* Copy image and skip lines */
834 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
836 uint8_t *p_in, *p_out_end, *p_out;
838 p_in = p_pic->p[i_plane].p_pixels;
840 p_out = p_outpic->p[i_plane].p_pixels;
841 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
842 * p_outpic->p[i_plane].i_visible_lines;
844 /* All lines: mean value */
845 for( ; p_out < p_out_end ; )
847 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
848 p_pic->p[i_plane].i_pitch );
850 p_out += p_outpic->p[i_plane].i_pitch;
851 p_in += 2 * p_pic->p[i_plane].i_pitch;
857 static void RenderBlend( vout_thread_t *p_vout,
858 picture_t *p_outpic, picture_t *p_pic )
862 /* Copy image and skip lines */
863 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
865 uint8_t *p_in, *p_out_end, *p_out;
867 p_in = p_pic->p[i_plane].p_pixels;
869 p_out = p_outpic->p[i_plane].p_pixels;
870 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
871 * p_outpic->p[i_plane].i_visible_lines;
873 switch( p_vout->render.i_chroma )
877 /* First line: simple copy */
878 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
879 p_out += p_outpic->p[i_plane].i_pitch;
881 /* Remaining lines: mean value */
882 for( ; p_out < p_out_end ; )
884 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
885 p_pic->p[i_plane].i_pitch );
887 p_out += p_outpic->p[i_plane].i_pitch;
888 p_in += p_pic->p[i_plane].i_pitch;
893 /* First line: simple copy */
894 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
895 p_out += p_outpic->p[i_plane].i_pitch;
897 /* Remaining lines: mean value */
898 if( i_plane == Y_PLANE )
900 for( ; p_out < p_out_end ; )
902 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
903 p_pic->p[i_plane].i_pitch );
905 p_out += p_outpic->p[i_plane].i_pitch;
906 p_in += p_pic->p[i_plane].i_pitch;
912 for( ; p_out < p_out_end ; )
914 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
915 p_pic->p[i_plane].i_pitch );
917 p_out += p_outpic->p[i_plane].i_pitch;
918 p_in += 2*p_pic->p[i_plane].i_pitch;
929 static void MergeGeneric( void *_p_dest, const void *_p_s1,
930 const void *_p_s2, size_t i_bytes )
932 uint8_t* p_dest = (uint8_t*)_p_dest;
933 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
934 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
935 uint8_t* p_end = p_dest + i_bytes - 8;
937 while( p_dest < p_end )
939 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
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;
951 while( p_dest < p_end )
953 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
957 #if defined(CAN_COMPILE_MMXEXT)
958 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
961 uint8_t* p_dest = (uint8_t*)_p_dest;
962 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
963 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
964 uint8_t* p_end = p_dest + i_bytes - 8;
965 while( p_dest < p_end )
967 __asm__ __volatile__( "movq %2,%%mm1;"
969 "movq %%mm1, %0" :"=m" (*p_dest):
979 while( p_dest < p_end )
981 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
986 #if defined(CAN_COMPILE_3DNOW)
987 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
990 uint8_t* p_dest = (uint8_t*)_p_dest;
991 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
992 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
993 uint8_t* p_end = p_dest + i_bytes - 8;
994 while( p_dest < p_end )
996 __asm__ __volatile__( "movq %2,%%mm1;"
998 "movq %%mm1, %0" :"=m" (*p_dest):
1008 while( p_dest < p_end )
1010 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1015 #if defined(CAN_COMPILE_SSE)
1016 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1019 uint8_t* p_dest = (uint8_t*)_p_dest;
1020 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1021 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1023 while( (uintptr_t)p_s1 % 16 )
1025 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1027 p_end = p_dest + i_bytes - 16;
1028 while( p_dest < p_end )
1030 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1032 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1042 while( p_dest < p_end )
1044 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1049 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1050 static void EndMMX( void )
1052 __asm__ __volatile__( "emms" :: );
1056 #if defined(CAN_COMPILE_3DNOW)
1057 static void End3DNow( void )
1059 __asm__ __volatile__( "femms" :: );
1063 #ifdef CAN_COMPILE_C_ALTIVEC
1064 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1065 const void *_p_s2, size_t i_bytes )
1067 uint8_t *p_dest = (uint8_t *)_p_dest;
1068 uint8_t *p_s1 = (uint8_t *)_p_s1;
1069 uint8_t *p_s2 = (uint8_t *)_p_s2;
1070 uint8_t *p_end = p_dest + i_bytes - 15;
1072 /* Use C until the first 16-bytes aligned destination pixel */
1073 while( (uintptr_t)p_dest & 0xF )
1075 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1078 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1080 /* Unaligned source */
1081 vector unsigned char s1v, s2v, destv;
1082 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1083 vector unsigned char perm1v, perm2v;
1085 perm1v = vec_lvsl( 0, p_s1 );
1086 perm2v = vec_lvsl( 0, p_s2 );
1087 s1oldv = vec_ld( 0, p_s1 );
1088 s2oldv = vec_ld( 0, p_s2 );
1090 while( p_dest < p_end )
1092 s1newv = vec_ld( 16, p_s1 );
1093 s2newv = vec_ld( 16, p_s2 );
1094 s1v = vec_perm( s1oldv, s1newv, perm1v );
1095 s2v = vec_perm( s2oldv, s2newv, perm2v );
1098 destv = vec_avg( s1v, s2v );
1099 vec_st( destv, 0, p_dest );
1108 /* Aligned source */
1109 vector unsigned char s1v, s2v, destv;
1111 while( p_dest < p_end )
1113 s1v = vec_ld( 0, p_s1 );
1114 s2v = vec_ld( 0, p_s2 );
1115 destv = vec_avg( s1v, s2v );
1116 vec_st( destv, 0, p_dest );
1126 while( p_dest < p_end )
1128 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1134 static void MergeNEON (void *restrict out, const void *in1,
1135 const void *in2, size_t n)
1137 uint8_t *outp = out;
1138 const uint8_t *in1p = in1;
1139 const uint8_t *in2p = in2;
1140 size_t mis = ((uintptr_t)outp) & 15;
1144 MergeGeneric (outp, in1p, in2p, mis);
1151 uint8_t *end = outp + (n & ~15);
1153 if ((((uintptr_t)in1p)|((uintptr_t)in2p)) & 15)
1156 "vld1.u8 {q0-q1}, [%[in1]]!\n"
1157 "vld1.u8 {q2-q3}, [%[in2]]!\n"
1158 "vhadd.u8 q4, q0, q2\n"
1159 "vld1.u8 {q6-q7}, [%[in1]]!\n"
1160 "vhadd.u8 q5, q1, q3\n"
1161 "vld1.u8 {q8-q9}, [%[in2]]!\n"
1162 "vhadd.u8 q10, q6, q8\n"
1163 "vhadd.u8 q11, q7, q9\n"
1164 "vst1.u8 {q4-q5}, [%[out],:128]!\n"
1165 "vst1.u8 {q10-q11}, [%[out],:128]!\n"
1166 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1168 : "q0", "q1", "q2", "memory");
1172 "vld1.u8 {q0-q1}, [%[in1],:128]!\n"
1173 "vld1.u8 {q2-q3}, [%[in2],:128]!\n"
1174 "vhadd.u8 q4, q0, q2\n"
1175 "vld1.u8 {q6-q7}, [%[in1],:128]!\n"
1176 "vhadd.u8 q5, q1, q3\n"
1177 "vld1.u8 {q8-q9}, [%[in2],:128]!\n"
1178 "vhadd.u8 q10, q6, q8\n"
1179 "vhadd.u8 q11, q7, q9\n"
1180 "vst1.u8 {q4-q5}, [%[out],:128]!\n"
1181 "vst1.u8 {q10-q11}, [%[out],:128]!\n"
1182 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1184 : "q0", "q1", "q2", "memory");
1187 MergeGeneric (outp, in1p, in2p, n);
1191 /*****************************************************************************
1192 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1193 * and apply a process to recreate the bottom field :
1194 * If a 8x8 block is classified as :
1195 * - progressive: it applies a small blend (1,6,1)
1197 * * in the MMX version: we do a ME between the 2 fields, if there is a
1198 * good match we use MC to recreate the bottom field (with a small
1200 * * otherwise: it recreates the bottom field by an edge oriented
1202 *****************************************************************************/
1204 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1205 * XXX: It need to access to 8x10
1206 * We use more than 8 lines to help with scrolling (text)
1207 * (and because XDeint8x8Frame use line 9)
1208 * XXX: smooth/uniform area with noise detection doesn't works well
1209 * but it's not really a problem because they don't have much details anyway
1211 static inline int ssd( int a ) { return a*a; }
1212 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1218 /* Detect interlacing */
1220 for( y = 0; y < 7; y += 2 )
1223 for( x = 0; x < 8; x++ )
1225 fr += ssd(src[ x] - src[1*i_src+x]) +
1226 ssd(src[i_src+x] - src[2*i_src+x]);
1227 ff += ssd(src[ x] - src[2*i_src+x]) +
1228 ssd(src[i_src+x] - src[3*i_src+x]);
1230 if( ff < 6*fr/8 && fr > 32 )
1236 return fc < 1 ? false : true;
1238 #ifdef CAN_COMPILE_MMXEXT
1239 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1246 /* Detect interlacing */
1248 pxor_r2r( mm7, mm7 );
1249 for( y = 0; y < 9; y += 2 )
1252 pxor_r2r( mm5, mm5 );
1253 pxor_r2r( mm6, mm6 );
1254 for( x = 0; x < 8; x+=4 )
1256 movd_m2r( src[ x], mm0 );
1257 movd_m2r( src[1*i_src+x], mm1 );
1258 movd_m2r( src[2*i_src+x], mm2 );
1259 movd_m2r( src[3*i_src+x], mm3 );
1261 punpcklbw_r2r( mm7, mm0 );
1262 punpcklbw_r2r( mm7, mm1 );
1263 punpcklbw_r2r( mm7, mm2 );
1264 punpcklbw_r2r( mm7, mm3 );
1266 movq_r2r( mm0, mm4 );
1268 psubw_r2r( mm1, mm0 );
1269 psubw_r2r( mm2, mm4 );
1271 psubw_r2r( mm1, mm2 );
1272 psubw_r2r( mm1, mm3 );
1274 pmaddwd_r2r( mm0, mm0 );
1275 pmaddwd_r2r( mm4, mm4 );
1276 pmaddwd_r2r( mm2, mm2 );
1277 pmaddwd_r2r( mm3, mm3 );
1278 paddd_r2r( mm0, mm2 );
1279 paddd_r2r( mm4, mm3 );
1280 paddd_r2r( mm2, mm5 );
1281 paddd_r2r( mm3, mm6 );
1284 movq_r2r( mm5, mm0 );
1285 psrlq_i2r( 32, mm0 );
1286 paddd_r2r( mm0, mm5 );
1287 movd_r2m( mm5, fr );
1289 movq_r2r( mm6, mm0 );
1290 psrlq_i2r( 32, mm0 );
1291 paddd_r2r( mm0, mm6 );
1292 movd_r2m( mm6, ff );
1294 if( ff < 6*fr/8 && fr > 32 )
1303 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1304 uint8_t *src1, int i_src1,
1305 uint8_t *src2, int i_src2 )
1310 for( y = 0; y < 8; y += 2 )
1312 memcpy( dst, src1, 8 );
1315 for( x = 0; x < 8; x++ )
1316 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1324 #ifdef CAN_COMPILE_MMXEXT
1325 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1326 uint8_t *src1, int i_src1,
1327 uint8_t *src2, int i_src2 )
1329 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1333 pxor_r2r( mm7, mm7 );
1334 for( y = 0; y < 8; y += 2 )
1336 for( x = 0; x < 8; x +=4 )
1338 movd_m2r( src1[x], mm0 );
1339 movd_r2m( mm0, dst[x] );
1341 movd_m2r( src2[x], mm1 );
1342 movd_m2r( src1[i_src1+x], mm2 );
1344 punpcklbw_r2r( mm7, mm0 );
1345 punpcklbw_r2r( mm7, mm1 );
1346 punpcklbw_r2r( mm7, mm2 );
1347 paddw_r2r( mm1, mm1 );
1348 movq_r2r( mm1, mm3 );
1349 paddw_r2r( mm3, mm3 );
1350 paddw_r2r( mm2, mm0 );
1351 paddw_r2r( mm3, mm1 );
1352 paddw_m2r( m_4, mm1 );
1353 paddw_r2r( mm1, mm0 );
1354 psraw_i2r( 3, mm0 );
1355 packuswb_r2r( mm7, mm0 );
1356 movd_r2m( mm0, dst[i_dst+x] );
1367 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1370 for( y = 0; y < 8; y++ )
1371 memset( &dst[y*i_dst], v, 8 );
1374 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1377 * TODO: a better one for the inner part.
1379 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1380 uint8_t *src, int i_src )
1385 for( y = 0; y < 8; y += 2 )
1387 memcpy( dst, src, 8 );
1390 for( x = 0; x < 8; x++ )
1391 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1396 #ifdef CAN_COMPILE_MMXEXT
1397 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1398 uint8_t *src, int i_src )
1403 for( y = 0; y < 8; y += 2 )
1405 movq_m2r( src[0], mm0 );
1406 movq_r2m( mm0, dst[0] );
1409 movq_m2r( src[2*i_src], mm1 );
1410 pavgb_r2r( mm1, mm0 );
1412 movq_r2m( mm0, dst[0] );
1420 /* XDeint8x8Field: Edge oriented interpolation
1421 * (Need -4 and +5 pixels H, +1 line)
1423 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1424 uint8_t *src, int i_src )
1429 for( y = 0; y < 8; y += 2 )
1431 memcpy( dst, src, 8 );
1434 for( x = 0; x < 8; x++ )
1436 uint8_t *src2 = &src[2*i_src];
1437 /* I use 8 pixels just to match the MMX version, but it's overkill
1438 * 5 would be enough (less isn't good) */
1439 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1440 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1441 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1442 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1444 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1445 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1446 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1447 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1449 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1450 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1451 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1452 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1454 if( c0 < c1 && c1 <= c2 )
1455 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1456 else if( c2 < c1 && c1 <= c0 )
1457 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1459 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1466 #ifdef CAN_COMPILE_MMXEXT
1467 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1468 uint8_t *src, int i_src )
1473 for( y = 0; y < 8; y += 2 )
1475 memcpy( dst, src, 8 );
1478 for( x = 0; x < 8; x++ )
1480 uint8_t *src2 = &src[2*i_src];
1483 movq_m2r( src[x-2], mm0 );
1484 movq_m2r( src[x-3], mm1 );
1485 movq_m2r( src[x-4], mm2 );
1487 psadbw_m2r( src2[x-4], mm0 );
1488 psadbw_m2r( src2[x-3], mm1 );
1489 psadbw_m2r( src2[x-2], mm2 );
1491 movd_r2m( mm0, c2 );
1492 movd_r2m( mm1, c1 );
1493 movd_r2m( mm2, c0 );
1495 if( c0 < c1 && c1 <= c2 )
1496 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1497 else if( c2 < c1 && c1 <= c0 )
1498 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1500 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1509 /* NxN arbitray size (and then only use pixel in the NxN block)
1511 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1512 int i_height, int i_width )
1519 /* Detect interlacing */
1520 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1523 for( y = 0; y < i_height - 2; y += 2 )
1525 const uint8_t *s = &src[y*i_src];
1526 for( x = 0; x < i_width; x++ )
1528 fr += ssd(s[ x] - s[1*i_src+x]);
1529 ff += ssd(s[ x] - s[2*i_src+x]);
1531 if( ff < fr && fr > i_width / 2 )
1535 return fc < 2 ? false : true;
1538 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1539 uint8_t *src, int i_src,
1540 int i_width, int i_height )
1545 for( y = 0; y < i_height; y += 2 )
1547 memcpy( dst, src, i_width );
1550 if( y < i_height - 2 )
1552 for( x = 0; x < i_width; x++ )
1553 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1557 /* Blend last line */
1558 for( x = 0; x < i_width; x++ )
1559 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1566 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1567 uint8_t *src, int i_src,
1568 int i_width, int i_height )
1573 for( y = 0; y < i_height; y += 2 )
1575 memcpy( dst, src, i_width );
1578 if( y < i_height - 2 )
1580 for( x = 0; x < i_width; x++ )
1581 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1585 /* Blend last line */
1586 for( x = 0; x < i_width; x++ )
1587 dst[x] = (src[x] + src[i_src+x]) >> 1;
1594 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1595 int i_width, int i_height )
1597 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1598 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1600 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1604 static inline int median( int a, int b, int c )
1606 int min = a, max =a;
1617 return a + b + c - min - max;
1623 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1624 uint8_t *src, int i_src,
1625 const int i_mbx, int i_modx )
1629 for( x = 0; x < i_mbx; x++ )
1632 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1634 if( x == 0 || x == i_mbx - 1 )
1635 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1637 XDeint8x8FieldC( dst, i_dst, src, i_src );
1641 XDeint8x8MergeC( dst, i_dst,
1642 &src[0*i_src], 2*i_src,
1643 &src[1*i_src], 2*i_src );
1651 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1653 #ifdef CAN_COMPILE_MMXEXT
1654 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1655 uint8_t *src, int i_src,
1656 const int i_mbx, int i_modx )
1660 /* Reset current line */
1661 for( x = 0; x < i_mbx; x++ )
1664 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1666 if( x == 0 || x == i_mbx - 1 )
1667 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1669 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1673 XDeint8x8MergeMMXEXT( dst, i_dst,
1674 &src[0*i_src], 2*i_src,
1675 &src[1*i_src], 2*i_src );
1683 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1687 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1691 /* Copy image and skip lines */
1692 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1694 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1695 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1697 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1698 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1700 const int i_dst = p_outpic->p[i_plane].i_pitch;
1701 const int i_src = p_pic->p[i_plane].i_pitch;
1705 for( y = 0; y < i_mby; y++ )
1707 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1708 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1710 #ifdef CAN_COMPILE_MMXEXT
1711 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1712 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1715 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1718 /* Last line (C only)*/
1721 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1722 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1724 for( x = 0; x < i_mbx; x++ )
1726 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1733 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1737 #ifdef CAN_COMPILE_MMXEXT
1738 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1743 /*****************************************************************************
1744 * FilterCallback: called when changing the deinterlace method on the fly.
1745 *****************************************************************************/
1746 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1747 vlc_value_t oldval, vlc_value_t newval,
1750 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1751 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1752 vout_sys_t *p_sys = p_vout->p_sys;
1754 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1756 vlc_mutex_lock( &p_sys->filter_lock );
1757 const bool b_old_half_height = p_sys->b_half_height;
1759 SetFilterMethod( p_vout, newval.psz_string );
1761 if( !b_old_half_height == !p_sys->b_half_height )
1763 vlc_mutex_unlock( &p_sys->filter_lock );
1767 /* We need to kill the old vout */
1770 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
1771 vout_CloseAndRelease( p_sys->p_vout );
1774 /* Try to open a new video output */
1775 p_sys->p_vout = SpawnRealVout( p_vout );
1777 if( p_sys->p_vout == NULL )
1779 /* Everything failed */
1780 msg_Err( p_vout, "cannot open vout, aborting" );
1782 vlc_mutex_unlock( &p_sys->filter_lock );
1783 return VLC_EGENERIC;
1786 vout_filter_AddChild( p_vout, p_sys->p_vout, MouseEvent );
1788 vlc_mutex_unlock( &p_sys->filter_lock );
1792 /*****************************************************************************
1793 * video filter2 functions
1794 *****************************************************************************/
1795 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1797 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1798 picture_t *p_pic_dst;
1800 /* Request output picture */
1801 p_pic_dst = filter_NewPicture( p_filter );
1802 if( p_pic_dst == NULL )
1804 picture_Release( p_pic );
1808 switch( p_vout->p_sys->i_mode )
1810 case DEINTERLACE_DISCARD:
1811 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1814 case DEINTERLACE_BOB:
1816 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1817 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1821 case DEINTERLACE_LINEAR:
1823 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1824 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1826 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1827 picture_Release( p_pic_dst );
1828 picture_Release( p_pic );
1831 case DEINTERLACE_MEAN:
1832 RenderMean( p_vout, p_pic_dst, p_pic );
1835 case DEINTERLACE_BLEND:
1836 RenderBlend( p_vout, p_pic_dst, p_pic );
1840 RenderX( p_pic_dst, p_pic );
1844 picture_CopyProperties( p_pic_dst, p_pic );
1845 p_pic_dst->b_progressive = true;
1847 picture_Release( p_pic );
1851 /*****************************************************************************
1853 *****************************************************************************/
1854 static int OpenFilter( vlc_object_t *p_this )
1856 filter_t *p_filter = (filter_t*)p_this;
1857 vout_thread_t *p_vout;
1860 if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
1861 return VLC_EGENERIC;
1863 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1865 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1866 vlc_object_attach( p_vout, p_filter );
1867 p_filter->p_sys = (filter_sys_t *)p_vout;
1868 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1870 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1872 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1874 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1875 var_Set( p_filter, "deinterlace-mode", val );
1876 free( val.psz_string );
1878 if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1880 vlc_object_detach( p_vout );
1881 vlc_object_release( p_vout );
1882 return VLC_EGENERIC;
1886 GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
1887 if( !p_filter->b_allow_fmt_out_change &&
1888 ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
1889 fmt.i_height != p_filter->fmt_in.video.i_height ) )
1891 CloseFilter( VLC_OBJECT(p_filter) );
1892 return VLC_EGENERIC;
1894 p_filter->fmt_out.video = fmt;
1895 p_filter->fmt_out.i_codec = fmt.i_chroma;
1896 p_filter->pf_video_filter = Deinterlace;
1898 msg_Dbg( p_filter, "deinterlacing" );
1903 /*****************************************************************************
1904 * CloseFilter: clean up the filter
1905 *****************************************************************************/
1906 static void CloseFilter( vlc_object_t *p_this )
1908 filter_t *p_filter = (filter_t*)p_this;
1909 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1911 Destroy( VLC_OBJECT(p_vout) );
1912 vlc_object_detach( p_vout );
1913 vlc_object_release( p_vout );