1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
7 * Author: Sam Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
42 #include "vlc_filter.h"
44 #ifdef CAN_COMPILE_MMXEXT
48 #include "filter_common.h"
50 #define DEINTERLACE_DISCARD 1
51 #define DEINTERLACE_MEAN 2
52 #define DEINTERLACE_BLEND 3
53 #define DEINTERLACE_BOB 4
54 #define DEINTERLACE_LINEAR 5
55 #define DEINTERLACE_X 6
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Create ( vlc_object_t * );
61 static void Destroy ( vlc_object_t * );
63 static int Init ( vout_thread_t * );
64 static void End ( vout_thread_t * );
65 static void Render ( vout_thread_t *, picture_t * );
67 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
68 vlc_value_t oldval, vlc_value_t newval, void *p_data );
70 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
71 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
72 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
73 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
74 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
75 static void RenderX ( picture_t *, picture_t * );
77 static void MergeGeneric ( void *, const void *, const void *, size_t );
78 #if defined(CAN_COMPILE_C_ALTIVEC)
79 static void MergeAltivec ( void *, const void *, const void *, size_t );
81 #if defined(CAN_COMPILE_MMXEXT)
82 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
84 #if defined(CAN_COMPILE_3DNOW)
85 static void Merge3DNow ( void *, const void *, const void *, size_t );
87 #if defined(CAN_COMPILE_SSE)
88 static void MergeSSE2 ( void *, const void *, const void *, size_t );
90 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
91 static void EndMMX ( void );
93 #if defined(CAN_COMPILE_3DNOW)
94 static void End3DNow ( void );
97 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
98 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
100 static int OpenFilter( vlc_object_t *p_this );
101 static void CloseFilter( vlc_object_t *p_this );
103 /*****************************************************************************
104 * Callback prototypes
105 *****************************************************************************/
106 static int FilterCallback( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 /*****************************************************************************
111 *****************************************************************************/
112 #define MODE_TEXT N_("Deinterlace mode")
113 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
115 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
116 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
118 #define FILTER_CFG_PREFIX "sout-deinterlace-"
120 static const char *const mode_list[] = {
121 "discard", "blend", "mean", "bob", "linear", "x" };
122 static const char *const mode_list_text[] = {
123 N_("Discard"), N_("Blend"), N_("Mean"), N_("Bob"), N_("Linear"), "X" };
126 set_description( N_("Deinterlacing video filter") )
127 set_shortname( N_("Deinterlace" ))
128 set_capability( "video filter", 0 )
129 set_category( CAT_VIDEO )
130 set_subcategory( SUBCAT_VIDEO_VFILTER )
132 set_section( N_("Display"),NULL)
133 add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
134 MODE_LONGTEXT, false )
135 change_string_list( mode_list, mode_list_text, 0 )
138 add_shortcut( "deinterlace" )
139 set_callbacks( Create, Destroy )
142 set_capability( "video filter2", 0 )
143 set_section( N_("Streaming"),NULL)
144 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
145 SOUT_MODE_LONGTEXT, false )
146 change_string_list( mode_list, mode_list_text, 0 )
147 set_callbacks( OpenFilter, CloseFilter )
150 static const char *const ppsz_filter_options[] = {
154 /*****************************************************************************
155 * vout_sys_t: Deinterlace video output method descriptor
156 *****************************************************************************
157 * This structure is part of the video output thread descriptor.
158 * It describes the Deinterlace specific properties of an output thread.
159 *****************************************************************************/
162 int i_mode; /* Deinterlace mode */
163 bool b_double_rate; /* Shall we double the framerate? */
164 bool b_half_height; /* Shall be devide the height by 2 */
169 vout_thread_t *p_vout;
171 vlc_mutex_t filter_lock;
173 void (*pf_merge) ( void *, const void *, const void *, size_t );
174 void (*pf_end_merge) ( void );
177 /*****************************************************************************
178 * Control: control facility for the vout (forwards to child vout)
179 *****************************************************************************/
180 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
182 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
185 /*****************************************************************************
186 * Create: allocates Deinterlace video thread output method
187 *****************************************************************************
188 * This function allocates and initializes a Deinterlace vout method.
189 *****************************************************************************/
190 static int Create( vlc_object_t *p_this )
192 vout_thread_t *p_vout = (vout_thread_t *)p_this;
195 /* Allocate structure */
196 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
197 if( p_vout->p_sys == NULL )
200 p_vout->pf_init = Init;
201 p_vout->pf_end = End;
202 p_vout->pf_manage = NULL;
203 p_vout->pf_render = Render;
204 p_vout->pf_display = NULL;
205 p_vout->pf_control = Control;
207 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
208 p_vout->p_sys->b_double_rate = false;
209 p_vout->p_sys->b_half_height = true;
210 p_vout->p_sys->last_date = 0;
211 p_vout->p_sys->p_vout = 0;
212 vlc_mutex_init( &p_vout->p_sys->filter_lock );
214 #if defined(CAN_COMPILE_C_ALTIVEC)
215 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
217 p_vout->p_sys->pf_merge = MergeAltivec;
218 p_vout->p_sys->pf_end_merge = NULL;
222 #if defined(CAN_COMPILE_SSE)
223 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
225 p_vout->p_sys->pf_merge = MergeSSE2;
226 p_vout->p_sys->pf_end_merge = EndMMX;
230 #if defined(CAN_COMPILE_MMXEXT)
231 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
233 p_vout->p_sys->pf_merge = MergeMMXEXT;
234 p_vout->p_sys->pf_end_merge = EndMMX;
238 #if defined(CAN_COMPILE_3DNOW)
239 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
241 p_vout->p_sys->pf_merge = Merge3DNow;
242 p_vout->p_sys->pf_end_merge = End3DNow;
247 p_vout->p_sys->pf_merge = MergeGeneric;
248 p_vout->p_sys->pf_end_merge = NULL;
251 /* Look what method was requested */
252 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
253 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
255 if( val.psz_string == NULL )
257 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
258 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
260 val.psz_string = strdup( "discard" );
263 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
265 SetFilterMethod( p_vout, val.psz_string );
267 free( val.psz_string );
272 /*****************************************************************************
273 * SetFilterMethod: setup the deinterlace method to use.
274 *****************************************************************************/
275 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
277 if( !strcmp( psz_method, "mean" ) )
279 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
280 p_vout->p_sys->b_double_rate = false;
281 p_vout->p_sys->b_half_height = true;
283 else if( !strcmp( psz_method, "blend" )
284 || !strcmp( psz_method, "average" )
285 || !strcmp( psz_method, "combine-fields" ) )
287 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
288 p_vout->p_sys->b_double_rate = false;
289 p_vout->p_sys->b_half_height = false;
291 else if( !strcmp( psz_method, "bob" )
292 || !strcmp( psz_method, "progressive-scan" ) )
294 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
295 p_vout->p_sys->b_double_rate = true;
296 p_vout->p_sys->b_half_height = false;
298 else if( !strcmp( psz_method, "linear" ) )
300 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
301 p_vout->p_sys->b_double_rate = true;
302 p_vout->p_sys->b_half_height = false;
304 else if( !strcmp( psz_method, "x" ) )
306 p_vout->p_sys->i_mode = DEINTERLACE_X;
307 p_vout->p_sys->b_double_rate = false;
308 p_vout->p_sys->b_half_height = false;
312 const bool b_i422 = p_vout->render.i_chroma == VLC_FOURCC('I','4','2','2');
313 if( strcmp( psz_method, "discard" ) )
314 msg_Err( p_vout, "no valid deinterlace mode provided, "
315 "using \"discard\"" );
317 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
318 p_vout->p_sys->b_double_rate = false;
319 p_vout->p_sys->b_half_height = !b_i422;
322 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
325 static void GetOutputFormat( vout_thread_t *p_vout,
326 video_format_t *p_dst, const video_format_t *p_src )
330 if( p_vout->p_sys->b_half_height )
332 p_dst->i_height /= 2;
333 p_dst->i_visible_height /= 2;
334 p_dst->i_y_offset /= 2;
335 p_dst->i_sar_den *= 2;
338 if( p_src->i_chroma == VLC_FOURCC('I','4','2','2') )
340 switch( p_vout->p_sys->i_mode )
342 case DEINTERLACE_MEAN:
343 case DEINTERLACE_LINEAR:
345 p_dst->i_chroma = VLC_FOURCC('I','4','2','2');
348 p_dst->i_chroma = VLC_FOURCC('I','4','2','0');
354 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
356 return i_chroma == VLC_FOURCC('I','4','2','0') ||
357 i_chroma == VLC_FOURCC('I','Y','U','V') ||
358 i_chroma == VLC_FOURCC('Y','V','1','2') ||
359 i_chroma == VLC_FOURCC('I','4','2','2');
362 /*****************************************************************************
363 * Init: initialize Deinterlace video thread output method
364 *****************************************************************************/
365 static int Init( vout_thread_t *p_vout )
367 I_OUTPUTPICTURES = 0;
369 if( !IsChromaSupported( p_vout->render.i_chroma ) )
370 return VLC_EGENERIC; /* unknown chroma */
372 /* Initialize the output structure, full of directbuffers since we want
373 * the decoder to output directly to our structures. */
374 p_vout->output.i_chroma = p_vout->render.i_chroma;
375 p_vout->output.i_width = p_vout->render.i_width;
376 p_vout->output.i_height = p_vout->render.i_height;
377 p_vout->output.i_aspect = p_vout->render.i_aspect;
378 p_vout->fmt_out = p_vout->fmt_in;
380 /* Try to open the real video output */
381 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
383 if( p_vout->p_sys->p_vout == NULL )
385 /* Everything failed */
386 msg_Err( p_vout, "cannot open vout, aborting" );
391 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
393 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
395 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
400 /*****************************************************************************
401 * SpawnRealVout: spawn the real video output.
402 *****************************************************************************/
403 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
405 msg_Dbg( p_vout, "spawning the real video output" );
408 GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
410 return vout_Create( p_vout, &fmt );
413 /*****************************************************************************
414 * End: terminate Deinterlace video thread output method
415 *****************************************************************************/
416 static void End( vout_thread_t *p_vout )
418 vout_sys_t *p_sys = p_vout->p_sys;
422 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
423 vout_CloseAndRelease( p_sys->p_vout );
426 vout_filter_ReleaseDirectBuffers( p_vout );
429 /*****************************************************************************
430 * Destroy: destroy Deinterlace video thread output method
431 *****************************************************************************
432 * Terminate an output method created by DeinterlaceCreateOutputMethod
433 *****************************************************************************/
434 static void Destroy( vlc_object_t *p_this )
436 vout_thread_t *p_vout = (vout_thread_t *)p_this;
437 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
438 free( p_vout->p_sys );
442 * Forward mouse event with proper conversion.
444 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
445 vlc_value_t oldval, vlc_value_t newval, void *p_data )
447 vout_thread_t *p_vout = p_data;
448 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
450 if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
453 return var_Set( p_vout, psz_var, newval );
456 /*****************************************************************************
457 * Render: displays previously rendered output
458 *****************************************************************************
459 * This function send the currently rendered image to Deinterlace image,
460 * waits until it is displayed and switch the two rendering buffers, preparing
462 *****************************************************************************/
463 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
465 vout_sys_t *p_sys = p_vout->p_sys;
466 picture_t *pp_outpic[2];
468 /* FIXME are they needed ? */
469 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
470 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
471 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
472 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
474 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
475 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
476 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
477 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
478 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
479 if( p_vout->p_sys->b_half_height )
481 p_sys->p_vout->fmt_in.i_y_offset /= 2;
482 p_sys->p_vout->fmt_in.i_visible_height /= 2;
485 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
487 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
489 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
490 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
491 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
493 video_format_t fmt = p_vout->fmt_out;
494 if( p_vout->p_sys->b_half_height )
496 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
500 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
505 pp_outpic[0] = pp_outpic[1] = NULL;
507 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
509 /* Get a new picture */
510 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
514 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
516 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
519 msleep( VOUT_OUTMEM_SLEEP );
522 pp_outpic[0]->date = p_pic->date;
524 /* If we are using double rate, get an additional new picture */
525 if( p_vout->p_sys->b_double_rate )
527 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
531 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
533 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
534 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
537 msleep( VOUT_OUTMEM_SLEEP );
540 /* 20ms is a bit arbitrary, but it's only for the first image we get */
541 if( !p_vout->p_sys->last_date )
542 pp_outpic[1]->date = p_pic->date + 20000;
544 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
545 p_vout->p_sys->last_date = p_pic->date;
548 switch( p_vout->p_sys->i_mode )
550 case DEINTERLACE_DISCARD:
551 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
552 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
555 case DEINTERLACE_BOB:
556 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
557 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
558 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
559 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
562 case DEINTERLACE_LINEAR:
563 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
564 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
565 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
566 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
569 case DEINTERLACE_MEAN:
570 RenderMean( p_vout, pp_outpic[0], p_pic );
571 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
574 case DEINTERLACE_BLEND:
575 RenderBlend( p_vout, pp_outpic[0], p_pic );
576 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
580 RenderX( pp_outpic[0], p_pic );
581 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
584 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
587 /*****************************************************************************
588 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
589 *****************************************************************************/
590 static void RenderDiscard( vout_thread_t *p_vout,
591 picture_t *p_outpic, picture_t *p_pic, int i_field )
595 /* Copy image and skip lines */
596 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
598 uint8_t *p_in, *p_out_end, *p_out;
601 p_in = p_pic->p[i_plane].p_pixels
602 + i_field * p_pic->p[i_plane].i_pitch;
604 p_out = p_outpic->p[i_plane].p_pixels;
605 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
606 * p_outpic->p[i_plane].i_visible_lines;
608 switch( p_vout->render.i_chroma )
610 case VLC_FOURCC('I','4','2','0'):
611 case VLC_FOURCC('I','Y','U','V'):
612 case VLC_FOURCC('Y','V','1','2'):
614 for( ; p_out < p_out_end ; )
616 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
618 p_out += p_outpic->p[i_plane].i_pitch;
619 p_in += 2 * p_pic->p[i_plane].i_pitch;
623 case VLC_FOURCC('I','4','2','2'):
625 i_increment = 2 * p_pic->p[i_plane].i_pitch;
627 if( i_plane == Y_PLANE )
629 for( ; p_out < p_out_end ; )
631 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
632 p_out += p_outpic->p[i_plane].i_pitch;
633 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
634 p_out += p_outpic->p[i_plane].i_pitch;
640 for( ; p_out < p_out_end ; )
642 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
643 p_out += p_outpic->p[i_plane].i_pitch;
655 /*****************************************************************************
656 * RenderBob: renders a BOB picture - simple copy
657 *****************************************************************************/
658 static void RenderBob( vout_thread_t *p_vout,
659 picture_t *p_outpic, picture_t *p_pic, int i_field )
663 /* Copy image and skip lines */
664 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
666 uint8_t *p_in, *p_out_end, *p_out;
668 p_in = p_pic->p[i_plane].p_pixels;
669 p_out = p_outpic->p[i_plane].p_pixels;
670 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
671 * p_outpic->p[i_plane].i_visible_lines;
673 switch( p_vout->render.i_chroma )
675 case VLC_FOURCC('I','4','2','0'):
676 case VLC_FOURCC('I','Y','U','V'):
677 case VLC_FOURCC('Y','V','1','2'):
678 /* For BOTTOM field we need to add the first line */
681 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
682 p_in += p_pic->p[i_plane].i_pitch;
683 p_out += p_outpic->p[i_plane].i_pitch;
686 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
688 for( ; p_out < p_out_end ; )
690 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
692 p_out += p_outpic->p[i_plane].i_pitch;
694 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
696 p_in += 2 * p_pic->p[i_plane].i_pitch;
697 p_out += p_outpic->p[i_plane].i_pitch;
700 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
702 /* For TOP field we need to add the last line */
705 p_in += p_pic->p[i_plane].i_pitch;
706 p_out += p_outpic->p[i_plane].i_pitch;
707 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
711 case VLC_FOURCC('I','4','2','2'):
712 /* For BOTTOM field we need to add the first line */
715 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
716 p_in += p_pic->p[i_plane].i_pitch;
717 p_out += p_outpic->p[i_plane].i_pitch;
720 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
722 if( i_plane == Y_PLANE )
724 for( ; p_out < p_out_end ; )
726 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
728 p_out += p_outpic->p[i_plane].i_pitch;
730 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
732 p_in += 2 * p_pic->p[i_plane].i_pitch;
733 p_out += p_outpic->p[i_plane].i_pitch;
738 for( ; p_out < p_out_end ; )
740 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
742 p_out += p_outpic->p[i_plane].i_pitch;
743 p_in += 2 * p_pic->p[i_plane].i_pitch;
747 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
749 /* For TOP field we need to add the last line */
752 p_in += p_pic->p[i_plane].i_pitch;
753 p_out += p_outpic->p[i_plane].i_pitch;
754 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
761 #define Merge p_vout->p_sys->pf_merge
762 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
764 /*****************************************************************************
765 * RenderLinear: BOB with linear interpolation
766 *****************************************************************************/
767 static void RenderLinear( vout_thread_t *p_vout,
768 picture_t *p_outpic, picture_t *p_pic, int i_field )
772 /* Copy image and skip lines */
773 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
775 uint8_t *p_in, *p_out_end, *p_out;
777 p_in = p_pic->p[i_plane].p_pixels;
778 p_out = p_outpic->p[i_plane].p_pixels;
779 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
780 * p_outpic->p[i_plane].i_visible_lines;
782 /* For BOTTOM field we need to add the first line */
785 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
786 p_in += p_pic->p[i_plane].i_pitch;
787 p_out += p_outpic->p[i_plane].i_pitch;
790 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
792 for( ; p_out < p_out_end ; )
794 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
796 p_out += p_outpic->p[i_plane].i_pitch;
798 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
799 p_pic->p[i_plane].i_pitch );
801 p_in += 2 * p_pic->p[i_plane].i_pitch;
802 p_out += p_outpic->p[i_plane].i_pitch;
805 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
807 /* For TOP field we need to add the last line */
810 p_in += p_pic->p[i_plane].i_pitch;
811 p_out += p_outpic->p[i_plane].i_pitch;
812 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
818 static void RenderMean( vout_thread_t *p_vout,
819 picture_t *p_outpic, picture_t *p_pic )
823 /* Copy image and skip lines */
824 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
826 uint8_t *p_in, *p_out_end, *p_out;
828 p_in = p_pic->p[i_plane].p_pixels;
830 p_out = p_outpic->p[i_plane].p_pixels;
831 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
832 * p_outpic->p[i_plane].i_visible_lines;
834 /* All lines: mean value */
835 for( ; p_out < p_out_end ; )
837 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
838 p_pic->p[i_plane].i_pitch );
840 p_out += p_outpic->p[i_plane].i_pitch;
841 p_in += 2 * p_pic->p[i_plane].i_pitch;
847 static void RenderBlend( vout_thread_t *p_vout,
848 picture_t *p_outpic, picture_t *p_pic )
852 /* Copy image and skip lines */
853 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
855 uint8_t *p_in, *p_out_end, *p_out;
857 p_in = p_pic->p[i_plane].p_pixels;
859 p_out = p_outpic->p[i_plane].p_pixels;
860 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
861 * p_outpic->p[i_plane].i_visible_lines;
863 switch( p_vout->render.i_chroma )
865 case VLC_FOURCC('I','4','2','0'):
866 case VLC_FOURCC('I','Y','U','V'):
867 case VLC_FOURCC('Y','V','1','2'):
868 /* First line: simple copy */
869 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
870 p_out += p_outpic->p[i_plane].i_pitch;
872 /* Remaining lines: mean value */
873 for( ; p_out < p_out_end ; )
875 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
876 p_pic->p[i_plane].i_pitch );
878 p_out += p_outpic->p[i_plane].i_pitch;
879 p_in += p_pic->p[i_plane].i_pitch;
883 case VLC_FOURCC('I','4','2','2'):
884 /* First line: simple copy */
885 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
886 p_out += p_outpic->p[i_plane].i_pitch;
888 /* Remaining lines: mean value */
889 if( i_plane == Y_PLANE )
891 for( ; p_out < p_out_end ; )
893 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
894 p_pic->p[i_plane].i_pitch );
896 p_out += p_outpic->p[i_plane].i_pitch;
897 p_in += p_pic->p[i_plane].i_pitch;
903 for( ; p_out < p_out_end ; )
905 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
906 p_pic->p[i_plane].i_pitch );
908 p_out += p_outpic->p[i_plane].i_pitch;
909 p_in += 2*p_pic->p[i_plane].i_pitch;
920 static void MergeGeneric( void *_p_dest, const void *_p_s1,
921 const void *_p_s2, size_t i_bytes )
923 uint8_t* p_dest = (uint8_t*)_p_dest;
924 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
925 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
926 uint8_t* p_end = p_dest + i_bytes - 8;
928 while( p_dest < p_end )
930 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
931 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
932 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
933 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
934 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
935 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
936 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
937 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
942 while( p_dest < p_end )
944 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
948 #if defined(CAN_COMPILE_MMXEXT)
949 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
952 uint8_t* p_dest = (uint8_t*)_p_dest;
953 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
954 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
955 uint8_t* p_end = p_dest + i_bytes - 8;
956 while( p_dest < p_end )
958 __asm__ __volatile__( "movq %2,%%mm1;"
960 "movq %%mm1, %0" :"=m" (*p_dest):
970 while( p_dest < p_end )
972 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
977 #if defined(CAN_COMPILE_3DNOW)
978 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
981 uint8_t* p_dest = (uint8_t*)_p_dest;
982 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
983 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
984 uint8_t* p_end = p_dest + i_bytes - 8;
985 while( p_dest < p_end )
987 __asm__ __volatile__( "movq %2,%%mm1;"
989 "movq %%mm1, %0" :"=m" (*p_dest):
999 while( p_dest < p_end )
1001 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1006 #if defined(CAN_COMPILE_SSE)
1007 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1010 uint8_t* p_dest = (uint8_t*)_p_dest;
1011 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1012 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1014 while( (uintptr_t)p_s1 % 16 )
1016 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1018 p_end = p_dest + i_bytes - 16;
1019 while( p_dest < p_end )
1021 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1023 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1033 while( p_dest < p_end )
1035 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1040 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1041 static void EndMMX( void )
1043 __asm__ __volatile__( "emms" :: );
1047 #if defined(CAN_COMPILE_3DNOW)
1048 static void End3DNow( void )
1050 __asm__ __volatile__( "femms" :: );
1054 #ifdef CAN_COMPILE_C_ALTIVEC
1055 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1056 const void *_p_s2, size_t i_bytes )
1058 uint8_t *p_dest = (uint8_t *)_p_dest;
1059 uint8_t *p_s1 = (uint8_t *)_p_s1;
1060 uint8_t *p_s2 = (uint8_t *)_p_s2;
1061 uint8_t *p_end = p_dest + i_bytes - 15;
1063 /* Use C until the first 16-bytes aligned destination pixel */
1064 while( (int)p_dest & 0xF )
1066 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1069 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1071 /* Unaligned source */
1072 vector unsigned char s1v, s2v, destv;
1073 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1074 vector unsigned char perm1v, perm2v;
1076 perm1v = vec_lvsl( 0, p_s1 );
1077 perm2v = vec_lvsl( 0, p_s2 );
1078 s1oldv = vec_ld( 0, p_s1 );
1079 s2oldv = vec_ld( 0, p_s2 );
1081 while( p_dest < p_end )
1083 s1newv = vec_ld( 16, p_s1 );
1084 s2newv = vec_ld( 16, p_s2 );
1085 s1v = vec_perm( s1oldv, s1newv, perm1v );
1086 s2v = vec_perm( s2oldv, s2newv, perm2v );
1089 destv = vec_avg( s1v, s2v );
1090 vec_st( destv, 0, p_dest );
1099 /* Aligned source */
1100 vector unsigned char s1v, s2v, destv;
1102 while( p_dest < p_end )
1104 s1v = vec_ld( 0, p_s1 );
1105 s2v = vec_ld( 0, p_s2 );
1106 destv = vec_avg( s1v, s2v );
1107 vec_st( destv, 0, p_dest );
1117 while( p_dest < p_end )
1119 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1124 /*****************************************************************************
1125 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1126 * and apply a process to recreate the bottom field :
1127 * If a 8x8 block is classified as :
1128 * - progressive: it applies a small blend (1,6,1)
1130 * * in the MMX version: we do a ME between the 2 fields, if there is a
1131 * good match we use MC to recreate the bottom field (with a small
1133 * * otherwise: it recreates the bottom field by an edge oriented
1135 *****************************************************************************/
1137 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1138 * XXX: It need to access to 8x10
1139 * We use more than 8 lines to help with scrolling (text)
1140 * (and because XDeint8x8Frame use line 9)
1141 * XXX: smooth/uniform area with noise detection doesn't works well
1142 * but it's not really a problem because they don't have much details anyway
1144 static inline int ssd( int a ) { return a*a; }
1145 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1151 /* Detect interlacing */
1153 for( y = 0; y < 7; y += 2 )
1156 for( x = 0; x < 8; x++ )
1158 fr += ssd(src[ x] - src[1*i_src+x]) +
1159 ssd(src[i_src+x] - src[2*i_src+x]);
1160 ff += ssd(src[ x] - src[2*i_src+x]) +
1161 ssd(src[i_src+x] - src[3*i_src+x]);
1163 if( ff < 6*fr/8 && fr > 32 )
1169 return fc < 1 ? false : true;
1171 #ifdef CAN_COMPILE_MMXEXT
1172 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1179 /* Detect interlacing */
1181 pxor_r2r( mm7, mm7 );
1182 for( y = 0; y < 9; y += 2 )
1185 pxor_r2r( mm5, mm5 );
1186 pxor_r2r( mm6, mm6 );
1187 for( x = 0; x < 8; x+=4 )
1189 movd_m2r( src[ x], mm0 );
1190 movd_m2r( src[1*i_src+x], mm1 );
1191 movd_m2r( src[2*i_src+x], mm2 );
1192 movd_m2r( src[3*i_src+x], mm3 );
1194 punpcklbw_r2r( mm7, mm0 );
1195 punpcklbw_r2r( mm7, mm1 );
1196 punpcklbw_r2r( mm7, mm2 );
1197 punpcklbw_r2r( mm7, mm3 );
1199 movq_r2r( mm0, mm4 );
1201 psubw_r2r( mm1, mm0 );
1202 psubw_r2r( mm2, mm4 );
1204 psubw_r2r( mm1, mm2 );
1205 psubw_r2r( mm1, mm3 );
1207 pmaddwd_r2r( mm0, mm0 );
1208 pmaddwd_r2r( mm4, mm4 );
1209 pmaddwd_r2r( mm2, mm2 );
1210 pmaddwd_r2r( mm3, mm3 );
1211 paddd_r2r( mm0, mm2 );
1212 paddd_r2r( mm4, mm3 );
1213 paddd_r2r( mm2, mm5 );
1214 paddd_r2r( mm3, mm6 );
1217 movq_r2r( mm5, mm0 );
1218 psrlq_i2r( 32, mm0 );
1219 paddd_r2r( mm0, mm5 );
1220 movd_r2m( mm5, fr );
1222 movq_r2r( mm6, mm0 );
1223 psrlq_i2r( 32, mm0 );
1224 paddd_r2r( mm0, mm6 );
1225 movd_r2m( mm6, ff );
1227 if( ff < 6*fr/8 && fr > 32 )
1236 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1237 uint8_t *src1, int i_src1,
1238 uint8_t *src2, int i_src2 )
1243 for( y = 0; y < 8; y += 2 )
1245 memcpy( dst, src1, 8 );
1248 for( x = 0; x < 8; x++ )
1249 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1257 #ifdef CAN_COMPILE_MMXEXT
1258 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1259 uint8_t *src1, int i_src1,
1260 uint8_t *src2, int i_src2 )
1262 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1266 pxor_r2r( mm7, mm7 );
1267 for( y = 0; y < 8; y += 2 )
1269 for( x = 0; x < 8; x +=4 )
1271 movd_m2r( src1[x], mm0 );
1272 movd_r2m( mm0, dst[x] );
1274 movd_m2r( src2[x], mm1 );
1275 movd_m2r( src1[i_src1+x], mm2 );
1277 punpcklbw_r2r( mm7, mm0 );
1278 punpcklbw_r2r( mm7, mm1 );
1279 punpcklbw_r2r( mm7, mm2 );
1280 paddw_r2r( mm1, mm1 );
1281 movq_r2r( mm1, mm3 );
1282 paddw_r2r( mm3, mm3 );
1283 paddw_r2r( mm2, mm0 );
1284 paddw_r2r( mm3, mm1 );
1285 paddw_m2r( m_4, mm1 );
1286 paddw_r2r( mm1, mm0 );
1287 psraw_i2r( 3, mm0 );
1288 packuswb_r2r( mm7, mm0 );
1289 movd_r2m( mm0, dst[i_dst+x] );
1300 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1303 for( y = 0; y < 8; y++ )
1304 memset( &dst[y*i_dst], v, 8 );
1307 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1310 * TODO: a better one for the inner part.
1312 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1313 uint8_t *src, int i_src )
1318 for( y = 0; y < 8; y += 2 )
1320 memcpy( dst, src, 8 );
1323 for( x = 0; x < 8; x++ )
1324 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1329 #ifdef CAN_COMPILE_MMXEXT
1330 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1331 uint8_t *src, int i_src )
1336 for( y = 0; y < 8; y += 2 )
1338 movq_m2r( src[0], mm0 );
1339 movq_r2m( mm0, dst[0] );
1342 movq_m2r( src[2*i_src], mm1 );
1343 pavgb_r2r( mm1, mm0 );
1345 movq_r2m( mm0, dst[0] );
1353 /* XDeint8x8Field: Edge oriented interpolation
1354 * (Need -4 and +5 pixels H, +1 line)
1356 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1357 uint8_t *src, int i_src )
1362 for( y = 0; y < 8; y += 2 )
1364 memcpy( dst, src, 8 );
1367 for( x = 0; x < 8; x++ )
1369 uint8_t *src2 = &src[2*i_src];
1370 /* I use 8 pixels just to match the MMX version, but it's overkill
1371 * 5 would be enough (less isn't good) */
1372 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1373 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1374 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1375 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1377 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1378 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1379 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1380 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1382 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1383 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1384 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1385 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1387 if( c0 < c1 && c1 <= c2 )
1388 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1389 else if( c2 < c1 && c1 <= c0 )
1390 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1392 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1399 #ifdef CAN_COMPILE_MMXEXT
1400 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1401 uint8_t *src, int i_src )
1406 for( y = 0; y < 8; y += 2 )
1408 memcpy( dst, src, 8 );
1411 for( x = 0; x < 8; x++ )
1413 uint8_t *src2 = &src[2*i_src];
1416 movq_m2r( src[x-2], mm0 );
1417 movq_m2r( src[x-3], mm1 );
1418 movq_m2r( src[x-4], mm2 );
1420 psadbw_m2r( src2[x-4], mm0 );
1421 psadbw_m2r( src2[x-3], mm1 );
1422 psadbw_m2r( src2[x-2], mm2 );
1424 movd_r2m( mm0, c2 );
1425 movd_r2m( mm1, c1 );
1426 movd_r2m( mm2, c0 );
1428 if( c0 < c1 && c1 <= c2 )
1429 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1430 else if( c2 < c1 && c1 <= c0 )
1431 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1433 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1442 /* NxN arbitray size (and then only use pixel in the NxN block)
1444 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1445 int i_height, int i_width )
1452 /* Detect interlacing */
1453 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1456 for( y = 0; y < i_height - 2; y += 2 )
1458 const uint8_t *s = &src[y*i_src];
1459 for( x = 0; x < i_width; x++ )
1461 fr += ssd(s[ x] - s[1*i_src+x]);
1462 ff += ssd(s[ x] - s[2*i_src+x]);
1464 if( ff < fr && fr > i_width / 2 )
1468 return fc < 2 ? false : true;
1471 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1472 uint8_t *src, int i_src,
1473 int i_width, int i_height )
1478 for( y = 0; y < i_height; y += 2 )
1480 memcpy( dst, src, i_width );
1483 if( y < i_height - 2 )
1485 for( x = 0; x < i_width; x++ )
1486 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1490 /* Blend last line */
1491 for( x = 0; x < i_width; x++ )
1492 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1499 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1500 uint8_t *src, int i_src,
1501 int i_width, int i_height )
1506 for( y = 0; y < i_height; y += 2 )
1508 memcpy( dst, src, i_width );
1511 if( y < i_height - 2 )
1513 for( x = 0; x < i_width; x++ )
1514 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1518 /* Blend last line */
1519 for( x = 0; x < i_width; x++ )
1520 dst[x] = (src[x] + src[i_src+x]) >> 1;
1527 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1528 int i_width, int i_height )
1530 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1531 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1533 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1537 static inline int median( int a, int b, int c )
1539 int min = a, max =a;
1550 return a + b + c - min - max;
1556 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1557 uint8_t *src, int i_src,
1558 const int i_mbx, int i_modx )
1562 for( x = 0; x < i_mbx; x++ )
1565 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1567 if( x == 0 || x == i_mbx - 1 )
1568 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1570 XDeint8x8FieldC( dst, i_dst, src, i_src );
1574 XDeint8x8MergeC( dst, i_dst,
1575 &src[0*i_src], 2*i_src,
1576 &src[1*i_src], 2*i_src );
1584 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1586 #ifdef CAN_COMPILE_MMXEXT
1587 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1588 uint8_t *src, int i_src,
1589 const int i_mbx, int i_modx )
1593 /* Reset current line */
1594 for( x = 0; x < i_mbx; x++ )
1597 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1599 if( x == 0 || x == i_mbx - 1 )
1600 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1602 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1606 XDeint8x8MergeMMXEXT( dst, i_dst,
1607 &src[0*i_src], 2*i_src,
1608 &src[1*i_src], 2*i_src );
1616 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1620 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1624 /* Copy image and skip lines */
1625 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1627 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1628 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1630 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1631 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1633 const int i_dst = p_outpic->p[i_plane].i_pitch;
1634 const int i_src = p_pic->p[i_plane].i_pitch;
1638 for( y = 0; y < i_mby; y++ )
1640 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1641 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1643 #ifdef CAN_COMPILE_MMXEXT
1644 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1645 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1648 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1651 /* Last line (C only)*/
1654 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1655 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1657 for( x = 0; x < i_mbx; x++ )
1659 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1666 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1670 #ifdef CAN_COMPILE_MMXEXT
1671 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1676 /*****************************************************************************
1677 * FilterCallback: called when changing the deinterlace method on the fly.
1678 *****************************************************************************/
1679 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1680 vlc_value_t oldval, vlc_value_t newval,
1683 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1684 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1686 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1688 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1689 const bool b_old_half_height = p_vout->p_sys->b_half_height;
1691 SetFilterMethod( p_vout, newval.psz_string );
1693 if( !b_old_half_height == !p_vout->p_sys->b_half_height )
1695 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1699 /* We need to kill the old vout */
1700 if( p_vout->p_sys->p_vout )
1702 vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
1703 vout_CloseAndRelease( p_vout->p_sys->p_vout );
1706 /* Try to open a new video output */
1707 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
1709 if( p_vout->p_sys->p_vout == NULL )
1711 /* Everything failed */
1712 msg_Err( p_vout, "cannot open vout, aborting" );
1714 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1715 return VLC_EGENERIC;
1718 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
1720 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1724 /*****************************************************************************
1725 * video filter2 functions
1726 *****************************************************************************/
1727 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1729 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1730 picture_t *p_pic_dst;
1732 /* Request output picture */
1733 p_pic_dst = filter_NewPicture( p_filter );
1734 if( p_pic_dst == NULL )
1736 picture_Release( p_pic );
1740 switch( p_vout->p_sys->i_mode )
1742 case DEINTERLACE_DISCARD:
1743 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1746 case DEINTERLACE_BOB:
1748 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1749 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1753 case DEINTERLACE_LINEAR:
1755 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1756 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1758 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1759 picture_Release( p_pic_dst );
1760 picture_Release( p_pic );
1763 case DEINTERLACE_MEAN:
1764 RenderMean( p_vout, p_pic_dst, p_pic );
1767 case DEINTERLACE_BLEND:
1768 RenderBlend( p_vout, p_pic_dst, p_pic );
1772 RenderX( p_pic_dst, p_pic );
1776 picture_CopyProperties( p_pic_dst, p_pic );
1777 p_pic_dst->b_progressive = true;
1779 picture_Release( p_pic );
1783 /*****************************************************************************
1785 *****************************************************************************/
1786 static int OpenFilter( vlc_object_t *p_this )
1788 filter_t *p_filter = (filter_t*)p_this;
1789 vout_thread_t *p_vout;
1792 if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
1793 return VLC_EGENERIC;
1795 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1797 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1798 vlc_object_attach( p_vout, p_filter );
1799 p_filter->p_sys = (filter_sys_t *)p_vout;
1800 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1802 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1804 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1806 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1807 var_Set( p_filter, "deinterlace-mode", val );
1808 free( val.psz_string );
1810 if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1812 vlc_object_detach( p_vout );
1813 vlc_object_release( p_vout );
1814 return VLC_EGENERIC;
1818 GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
1819 if( !p_filter->b_allow_fmt_out_change &&
1820 ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
1821 fmt.i_height != p_filter->fmt_in.video.i_height ) )
1823 CloseFilter( VLC_OBJECT(p_filter) );
1824 return VLC_EGENERIC;
1826 p_filter->fmt_out.video = fmt;
1827 p_filter->fmt_out.i_codec = fmt.i_chroma;
1828 p_filter->pf_video_filter = Deinterlace;
1830 msg_Dbg( p_filter, "deinterlacing" );
1835 /*****************************************************************************
1836 * CloseFilter: clean up the filter
1837 *****************************************************************************/
1838 static void CloseFilter( vlc_object_t *p_this )
1840 filter_t *p_filter = (filter_t*)p_this;
1841 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1843 Destroy( VLC_OBJECT(p_vout) );
1844 vlc_object_detach( p_vout );
1845 vlc_object_release( p_vout );