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 void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
68 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
69 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
70 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
71 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
72 static void RenderX ( picture_t *, picture_t * );
74 static void MergeGeneric ( void *, const void *, const void *, size_t );
75 #if defined(CAN_COMPILE_C_ALTIVEC)
76 static void MergeAltivec ( void *, const void *, const void *, size_t );
78 #if defined(CAN_COMPILE_MMXEXT)
79 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
81 #if defined(CAN_COMPILE_3DNOW)
82 static void Merge3DNow ( void *, const void *, const void *, size_t );
84 #if defined(CAN_COMPILE_SSE)
85 static void MergeSSE2 ( void *, const void *, const void *, size_t );
87 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
88 static void EndMMX ( void );
90 #if defined(CAN_COMPILE_3DNOW)
91 static void End3DNow ( void );
94 static int SendEvents ( vlc_object_t *, char const *,
95 vlc_value_t, vlc_value_t, 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 )
137 add_shortcut( "deinterlace" )
138 set_callbacks( Create, Destroy )
141 set_capability( "video filter2", 0 )
142 set_section( N_("Streaming"),NULL)
143 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
144 SOUT_MODE_LONGTEXT, false )
145 change_string_list( mode_list, mode_list_text, 0 )
146 set_callbacks( OpenFilter, CloseFilter )
149 static const char *const ppsz_filter_options[] = {
153 /*****************************************************************************
154 * vout_sys_t: Deinterlace video output method descriptor
155 *****************************************************************************
156 * This structure is part of the video output thread descriptor.
157 * It describes the Deinterlace specific properties of an output thread.
158 *****************************************************************************/
161 int i_mode; /* Deinterlace mode */
162 bool b_double_rate; /* Shall we double the framerate? */
163 bool b_half_height; /* Shall be devide the height by 2 */
168 vout_thread_t *p_vout;
170 vlc_mutex_t filter_lock;
172 void (*pf_merge) ( void *, const void *, const void *, size_t );
173 void (*pf_end_merge) ( void );
176 /*****************************************************************************
177 * Control: control facility for the vout (forwards to child vout)
178 *****************************************************************************/
179 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
181 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
184 /*****************************************************************************
185 * Create: allocates Deinterlace video thread output method
186 *****************************************************************************
187 * This function allocates and initializes a Deinterlace vout method.
188 *****************************************************************************/
189 static int Create( vlc_object_t *p_this )
191 vout_thread_t *p_vout = (vout_thread_t *)p_this;
194 /* Allocate structure */
195 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
196 if( p_vout->p_sys == NULL )
199 p_vout->pf_init = Init;
200 p_vout->pf_end = End;
201 p_vout->pf_manage = NULL;
202 p_vout->pf_render = Render;
203 p_vout->pf_display = NULL;
204 p_vout->pf_control = Control;
206 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
207 p_vout->p_sys->b_double_rate = false;
208 p_vout->p_sys->b_half_height = true;
209 p_vout->p_sys->last_date = 0;
210 p_vout->p_sys->p_vout = 0;
211 vlc_mutex_init( &p_vout->p_sys->filter_lock );
213 #if defined(CAN_COMPILE_C_ALTIVEC)
214 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
216 p_vout->p_sys->pf_merge = MergeAltivec;
217 p_vout->p_sys->pf_end_merge = NULL;
221 #if defined(CAN_COMPILE_SSE)
222 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
224 p_vout->p_sys->pf_merge = MergeSSE2;
225 p_vout->p_sys->pf_end_merge = EndMMX;
229 #if defined(CAN_COMPILE_MMXEXT)
230 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
232 p_vout->p_sys->pf_merge = MergeMMXEXT;
233 p_vout->p_sys->pf_end_merge = EndMMX;
237 #if defined(CAN_COMPILE_3DNOW)
238 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
240 p_vout->p_sys->pf_merge = Merge3DNow;
241 p_vout->p_sys->pf_end_merge = End3DNow;
246 p_vout->p_sys->pf_merge = MergeGeneric;
247 p_vout->p_sys->pf_end_merge = NULL;
250 /* Look what method was requested */
251 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
252 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
254 if( val.psz_string == NULL )
256 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
257 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
259 val.psz_string = strdup( "discard" );
262 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
264 SetFilterMethod( p_vout, val.psz_string );
266 free( val.psz_string );
271 /*****************************************************************************
272 * SetFilterMethod: setup the deinterlace method to use.
273 *****************************************************************************/
274 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
276 if( !strcmp( psz_method, "mean" ) )
278 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
279 p_vout->p_sys->b_double_rate = false;
280 p_vout->p_sys->b_half_height = true;
282 else if( !strcmp( psz_method, "blend" )
283 || !strcmp( psz_method, "average" )
284 || !strcmp( psz_method, "combine-fields" ) )
286 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
287 p_vout->p_sys->b_double_rate = false;
288 p_vout->p_sys->b_half_height = false;
290 else if( !strcmp( psz_method, "bob" )
291 || !strcmp( psz_method, "progressive-scan" ) )
293 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
294 p_vout->p_sys->b_double_rate = true;
295 p_vout->p_sys->b_half_height = false;
297 else if( !strcmp( psz_method, "linear" ) )
299 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
300 p_vout->p_sys->b_double_rate = true;
301 p_vout->p_sys->b_half_height = false;
303 else if( !strcmp( psz_method, "x" ) )
305 p_vout->p_sys->i_mode = DEINTERLACE_X;
306 p_vout->p_sys->b_double_rate = false;
307 p_vout->p_sys->b_half_height = false;
311 const bool b_i422 = p_vout->render.i_chroma == VLC_FOURCC('I','4','2','2');
312 if( strcmp( psz_method, "discard" ) )
313 msg_Err( p_vout, "no valid deinterlace mode provided, "
314 "using \"discard\"" );
316 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
317 p_vout->p_sys->b_double_rate = false;
318 p_vout->p_sys->b_half_height = !b_i422;
321 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
324 /*****************************************************************************
325 * Init: initialize Deinterlace video thread output method
326 *****************************************************************************/
327 static int Init( vout_thread_t *p_vout )
332 I_OUTPUTPICTURES = 0;
334 /* Initialize the output structure, full of directbuffers since we want
335 * the decoder to output directly to our structures. */
336 switch( p_vout->render.i_chroma )
338 case VLC_FOURCC('I','4','2','0'):
339 case VLC_FOURCC('I','Y','U','V'):
340 case VLC_FOURCC('Y','V','1','2'):
341 case VLC_FOURCC('I','4','2','2'):
342 p_vout->output.i_chroma = p_vout->render.i_chroma;
343 p_vout->output.i_width = p_vout->render.i_width;
344 p_vout->output.i_height = p_vout->render.i_height;
345 p_vout->output.i_aspect = p_vout->render.i_aspect;
346 p_vout->fmt_out = p_vout->fmt_in;
350 return VLC_EGENERIC; /* unknown chroma */
354 /* Try to open the real video output */
355 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
357 if( p_vout->p_sys->p_vout == NULL )
359 /* Everything failed */
360 msg_Err( p_vout, "cannot open vout, aborting" );
365 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
367 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
369 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
371 ADD_PARENT_CALLBACKS( SendEventsToChild );
376 /*****************************************************************************
377 * SpawnRealVout: spawn the real video output.
378 *****************************************************************************/
379 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
381 vout_thread_t *p_real_vout = NULL;
383 memset( &fmt, 0, sizeof( video_format_t ) );
385 msg_Dbg( p_vout, "spawning the real video output" );
387 fmt = p_vout->fmt_out;
388 if( p_vout->p_sys->b_half_height )
390 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
394 switch( p_vout->render.i_chroma )
396 case VLC_FOURCC('I','4','2','0'):
397 case VLC_FOURCC('I','Y','U','V'):
398 case VLC_FOURCC('Y','V','1','2'):
399 p_real_vout = vout_Create( p_vout, &fmt );
402 case VLC_FOURCC('I','4','2','2'):
403 switch( p_vout->p_sys->i_mode )
405 case DEINTERLACE_MEAN:
406 case DEINTERLACE_LINEAR:
408 fmt.i_chroma = VLC_FOURCC('I','4','2','2');
411 fmt.i_chroma = VLC_FOURCC('I','4','2','0');
414 p_real_vout = vout_Create( p_vout, &fmt );
424 /*****************************************************************************
425 * End: terminate Deinterlace video thread output method
426 *****************************************************************************/
427 static void End( vout_thread_t *p_vout )
431 DEL_PARENT_CALLBACKS( SendEventsToChild );
433 if( p_vout->p_sys->p_vout )
434 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
436 /* Free the fake output buffers we allocated */
437 for( i_index = I_OUTPUTPICTURES ; i_index ; )
440 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
443 if( p_vout->p_sys->p_vout )
444 vout_CloseAndRelease( p_vout->p_sys->p_vout );
447 /*****************************************************************************
448 * Destroy: destroy Deinterlace video thread output method
449 *****************************************************************************
450 * Terminate an output method created by DeinterlaceCreateOutputMethod
451 *****************************************************************************/
452 static void Destroy( vlc_object_t *p_this )
454 vout_thread_t *p_vout = (vout_thread_t *)p_this;
455 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
456 free( p_vout->p_sys );
459 /*****************************************************************************
460 * Render: displays previously rendered output
461 *****************************************************************************
462 * This function send the currently rendered image to Deinterlace image,
463 * waits until it is displayed and switch the two rendering buffers, preparing
465 *****************************************************************************/
466 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
468 vout_sys_t *p_sys = p_vout->p_sys;
469 picture_t *pp_outpic[2];
471 /* FIXME are they needed ? */
472 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
473 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
474 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
475 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
477 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
478 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
479 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
480 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
481 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
482 if( p_vout->p_sys->b_half_height )
484 p_sys->p_vout->fmt_in.i_y_offset /= 2;
485 p_sys->p_vout->fmt_in.i_visible_height /= 2;
488 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
490 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
492 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
493 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
494 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
496 video_format_t fmt = p_vout->fmt_out;
497 if( p_vout->p_sys->b_half_height )
499 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
503 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
508 pp_outpic[0] = pp_outpic[1] = NULL;
510 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
512 /* Get a new picture */
513 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
517 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
519 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
522 msleep( VOUT_OUTMEM_SLEEP );
525 pp_outpic[0]->date = p_pic->date;
527 /* If we are using double rate, get an additional new picture */
528 if( p_vout->p_sys->b_double_rate )
530 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
534 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
536 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
537 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
540 msleep( VOUT_OUTMEM_SLEEP );
543 /* 20ms is a bit arbitrary, but it's only for the first image we get */
544 if( !p_vout->p_sys->last_date )
545 pp_outpic[1]->date = p_pic->date + 20000;
547 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
548 p_vout->p_sys->last_date = p_pic->date;
551 switch( p_vout->p_sys->i_mode )
553 case DEINTERLACE_DISCARD:
554 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
555 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
558 case DEINTERLACE_BOB:
559 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
560 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
561 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
562 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
565 case DEINTERLACE_LINEAR:
566 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
567 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
568 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
569 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
572 case DEINTERLACE_MEAN:
573 RenderMean( p_vout, pp_outpic[0], p_pic );
574 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
577 case DEINTERLACE_BLEND:
578 RenderBlend( p_vout, pp_outpic[0], p_pic );
579 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
583 RenderX( pp_outpic[0], p_pic );
584 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
587 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
590 /*****************************************************************************
591 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
592 *****************************************************************************/
593 static void RenderDiscard( vout_thread_t *p_vout,
594 picture_t *p_outpic, picture_t *p_pic, int i_field )
598 /* Copy image and skip lines */
599 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
601 uint8_t *p_in, *p_out_end, *p_out;
604 p_in = p_pic->p[i_plane].p_pixels
605 + i_field * p_pic->p[i_plane].i_pitch;
607 p_out = p_outpic->p[i_plane].p_pixels;
608 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
609 * p_outpic->p[i_plane].i_visible_lines;
611 switch( p_vout->render.i_chroma )
613 case VLC_FOURCC('I','4','2','0'):
614 case VLC_FOURCC('I','Y','U','V'):
615 case VLC_FOURCC('Y','V','1','2'):
617 for( ; p_out < p_out_end ; )
619 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
621 p_out += p_outpic->p[i_plane].i_pitch;
622 p_in += 2 * p_pic->p[i_plane].i_pitch;
626 case VLC_FOURCC('I','4','2','2'):
628 i_increment = 2 * p_pic->p[i_plane].i_pitch;
630 if( i_plane == Y_PLANE )
632 for( ; p_out < p_out_end ; )
634 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
635 p_out += p_outpic->p[i_plane].i_pitch;
636 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
637 p_out += p_outpic->p[i_plane].i_pitch;
643 for( ; p_out < p_out_end ; )
645 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
646 p_out += p_outpic->p[i_plane].i_pitch;
658 /*****************************************************************************
659 * RenderBob: renders a BOB picture - simple copy
660 *****************************************************************************/
661 static void RenderBob( vout_thread_t *p_vout,
662 picture_t *p_outpic, picture_t *p_pic, int i_field )
666 /* Copy image and skip lines */
667 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
669 uint8_t *p_in, *p_out_end, *p_out;
671 p_in = p_pic->p[i_plane].p_pixels;
672 p_out = p_outpic->p[i_plane].p_pixels;
673 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
674 * p_outpic->p[i_plane].i_visible_lines;
676 switch( p_vout->render.i_chroma )
678 case VLC_FOURCC('I','4','2','0'):
679 case VLC_FOURCC('I','Y','U','V'):
680 case VLC_FOURCC('Y','V','1','2'):
681 /* For BOTTOM field we need to add the first line */
684 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
685 p_in += p_pic->p[i_plane].i_pitch;
686 p_out += p_outpic->p[i_plane].i_pitch;
689 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
691 for( ; p_out < p_out_end ; )
693 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
695 p_out += p_outpic->p[i_plane].i_pitch;
697 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
699 p_in += 2 * p_pic->p[i_plane].i_pitch;
700 p_out += p_outpic->p[i_plane].i_pitch;
703 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
705 /* For TOP field we need to add the last line */
708 p_in += p_pic->p[i_plane].i_pitch;
709 p_out += p_outpic->p[i_plane].i_pitch;
710 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
714 case VLC_FOURCC('I','4','2','2'):
715 /* For BOTTOM field we need to add the first line */
718 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
719 p_in += p_pic->p[i_plane].i_pitch;
720 p_out += p_outpic->p[i_plane].i_pitch;
723 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
725 if( i_plane == Y_PLANE )
727 for( ; p_out < p_out_end ; )
729 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
731 p_out += p_outpic->p[i_plane].i_pitch;
733 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
735 p_in += 2 * p_pic->p[i_plane].i_pitch;
736 p_out += p_outpic->p[i_plane].i_pitch;
741 for( ; p_out < p_out_end ; )
743 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
745 p_out += p_outpic->p[i_plane].i_pitch;
746 p_in += 2 * p_pic->p[i_plane].i_pitch;
750 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
752 /* For TOP field we need to add the last line */
755 p_in += p_pic->p[i_plane].i_pitch;
756 p_out += p_outpic->p[i_plane].i_pitch;
757 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
764 #define Merge p_vout->p_sys->pf_merge
765 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
767 /*****************************************************************************
768 * RenderLinear: BOB with linear interpolation
769 *****************************************************************************/
770 static void RenderLinear( vout_thread_t *p_vout,
771 picture_t *p_outpic, picture_t *p_pic, int i_field )
775 /* Copy image and skip lines */
776 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
778 uint8_t *p_in, *p_out_end, *p_out;
780 p_in = p_pic->p[i_plane].p_pixels;
781 p_out = p_outpic->p[i_plane].p_pixels;
782 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
783 * p_outpic->p[i_plane].i_visible_lines;
785 /* For BOTTOM field we need to add the first line */
788 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
789 p_in += p_pic->p[i_plane].i_pitch;
790 p_out += p_outpic->p[i_plane].i_pitch;
793 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
795 for( ; p_out < p_out_end ; )
797 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
799 p_out += p_outpic->p[i_plane].i_pitch;
801 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
802 p_pic->p[i_plane].i_pitch );
804 p_in += 2 * p_pic->p[i_plane].i_pitch;
805 p_out += p_outpic->p[i_plane].i_pitch;
808 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
810 /* For TOP field we need to add the last line */
813 p_in += p_pic->p[i_plane].i_pitch;
814 p_out += p_outpic->p[i_plane].i_pitch;
815 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
821 static void RenderMean( vout_thread_t *p_vout,
822 picture_t *p_outpic, picture_t *p_pic )
826 /* Copy image and skip lines */
827 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
829 uint8_t *p_in, *p_out_end, *p_out;
831 p_in = p_pic->p[i_plane].p_pixels;
833 p_out = p_outpic->p[i_plane].p_pixels;
834 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
835 * p_outpic->p[i_plane].i_visible_lines;
837 /* All lines: mean value */
838 for( ; p_out < p_out_end ; )
840 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
841 p_pic->p[i_plane].i_pitch );
843 p_out += p_outpic->p[i_plane].i_pitch;
844 p_in += 2 * p_pic->p[i_plane].i_pitch;
850 static void RenderBlend( vout_thread_t *p_vout,
851 picture_t *p_outpic, picture_t *p_pic )
855 /* Copy image and skip lines */
856 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
858 uint8_t *p_in, *p_out_end, *p_out;
860 p_in = p_pic->p[i_plane].p_pixels;
862 p_out = p_outpic->p[i_plane].p_pixels;
863 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
864 * p_outpic->p[i_plane].i_visible_lines;
866 switch( p_vout->render.i_chroma )
868 case VLC_FOURCC('I','4','2','0'):
869 case VLC_FOURCC('I','Y','U','V'):
870 case VLC_FOURCC('Y','V','1','2'):
871 /* First line: simple copy */
872 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
873 p_out += p_outpic->p[i_plane].i_pitch;
875 /* Remaining lines: mean value */
876 for( ; p_out < p_out_end ; )
878 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
879 p_pic->p[i_plane].i_pitch );
881 p_out += p_outpic->p[i_plane].i_pitch;
882 p_in += p_pic->p[i_plane].i_pitch;
886 case VLC_FOURCC('I','4','2','2'):
887 /* First line: simple copy */
888 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
889 p_out += p_outpic->p[i_plane].i_pitch;
891 /* Remaining lines: mean value */
892 if( i_plane == Y_PLANE )
894 for( ; p_out < p_out_end ; )
896 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
897 p_pic->p[i_plane].i_pitch );
899 p_out += p_outpic->p[i_plane].i_pitch;
900 p_in += p_pic->p[i_plane].i_pitch;
906 for( ; p_out < p_out_end ; )
908 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
909 p_pic->p[i_plane].i_pitch );
911 p_out += p_outpic->p[i_plane].i_pitch;
912 p_in += 2*p_pic->p[i_plane].i_pitch;
923 static void MergeGeneric( void *_p_dest, const void *_p_s1,
924 const void *_p_s2, size_t i_bytes )
926 uint8_t* p_dest = (uint8_t*)_p_dest;
927 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
928 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
929 uint8_t* p_end = p_dest + i_bytes - 8;
931 while( p_dest < p_end )
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;
938 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
939 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
940 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
945 while( p_dest < p_end )
947 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
951 #if defined(CAN_COMPILE_MMXEXT)
952 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
955 uint8_t* p_dest = (uint8_t*)_p_dest;
956 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
957 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
958 uint8_t* p_end = p_dest + i_bytes - 8;
959 while( p_dest < p_end )
961 __asm__ __volatile__( "movq %2,%%mm1;"
963 "movq %%mm1, %0" :"=m" (*p_dest):
973 while( p_dest < p_end )
975 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
980 #if defined(CAN_COMPILE_3DNOW)
981 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
984 uint8_t* p_dest = (uint8_t*)_p_dest;
985 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
986 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
987 uint8_t* p_end = p_dest + i_bytes - 8;
988 while( p_dest < p_end )
990 __asm__ __volatile__( "movq %2,%%mm1;"
992 "movq %%mm1, %0" :"=m" (*p_dest):
1002 while( p_dest < p_end )
1004 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1009 #if defined(CAN_COMPILE_SSE)
1010 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1013 uint8_t* p_dest = (uint8_t*)_p_dest;
1014 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1015 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1017 while( (uintptr_t)p_s1 % 16 )
1019 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1021 p_end = p_dest + i_bytes - 16;
1022 while( p_dest < p_end )
1024 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1026 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1036 while( p_dest < p_end )
1038 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1043 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1044 static void EndMMX( void )
1046 __asm__ __volatile__( "emms" :: );
1050 #if defined(CAN_COMPILE_3DNOW)
1051 static void End3DNow( void )
1053 __asm__ __volatile__( "femms" :: );
1057 #ifdef CAN_COMPILE_C_ALTIVEC
1058 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1059 const void *_p_s2, size_t i_bytes )
1061 uint8_t *p_dest = (uint8_t *)_p_dest;
1062 uint8_t *p_s1 = (uint8_t *)_p_s1;
1063 uint8_t *p_s2 = (uint8_t *)_p_s2;
1064 uint8_t *p_end = p_dest + i_bytes - 15;
1066 /* Use C until the first 16-bytes aligned destination pixel */
1067 while( (int)p_dest & 0xF )
1069 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1072 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1074 /* Unaligned source */
1075 vector unsigned char s1v, s2v, destv;
1076 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1077 vector unsigned char perm1v, perm2v;
1079 perm1v = vec_lvsl( 0, p_s1 );
1080 perm2v = vec_lvsl( 0, p_s2 );
1081 s1oldv = vec_ld( 0, p_s1 );
1082 s2oldv = vec_ld( 0, p_s2 );
1084 while( p_dest < p_end )
1086 s1newv = vec_ld( 16, p_s1 );
1087 s2newv = vec_ld( 16, p_s2 );
1088 s1v = vec_perm( s1oldv, s1newv, perm1v );
1089 s2v = vec_perm( s2oldv, s2newv, perm2v );
1092 destv = vec_avg( s1v, s2v );
1093 vec_st( destv, 0, p_dest );
1102 /* Aligned source */
1103 vector unsigned char s1v, s2v, destv;
1105 while( p_dest < p_end )
1107 s1v = vec_ld( 0, p_s1 );
1108 s2v = vec_ld( 0, p_s2 );
1109 destv = vec_avg( s1v, s2v );
1110 vec_st( destv, 0, p_dest );
1120 while( p_dest < p_end )
1122 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1127 /*****************************************************************************
1128 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1129 * and apply a process to recreate the bottom field :
1130 * If a 8x8 block is classified as :
1131 * - progressive: it applies a small blend (1,6,1)
1133 * * in the MMX version: we do a ME between the 2 fields, if there is a
1134 * good match we use MC to recreate the bottom field (with a small
1136 * * otherwise: it recreates the bottom field by an edge oriented
1138 *****************************************************************************/
1140 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1141 * XXX: It need to access to 8x10
1142 * We use more than 8 lines to help with scrolling (text)
1143 * (and because XDeint8x8Frame use line 9)
1144 * XXX: smooth/uniform area with noise detection doesn't works well
1145 * but it's not really a problem because they don't have much details anyway
1147 static inline int ssd( int a ) { return a*a; }
1148 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1154 /* Detect interlacing */
1156 for( y = 0; y < 7; y += 2 )
1159 for( x = 0; x < 8; x++ )
1161 fr += ssd(src[ x] - src[1*i_src+x]) +
1162 ssd(src[i_src+x] - src[2*i_src+x]);
1163 ff += ssd(src[ x] - src[2*i_src+x]) +
1164 ssd(src[i_src+x] - src[3*i_src+x]);
1166 if( ff < 6*fr/8 && fr > 32 )
1172 return fc < 1 ? false : true;
1174 #ifdef CAN_COMPILE_MMXEXT
1175 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1182 /* Detect interlacing */
1184 pxor_r2r( mm7, mm7 );
1185 for( y = 0; y < 9; y += 2 )
1188 pxor_r2r( mm5, mm5 );
1189 pxor_r2r( mm6, mm6 );
1190 for( x = 0; x < 8; x+=4 )
1192 movd_m2r( src[ x], mm0 );
1193 movd_m2r( src[1*i_src+x], mm1 );
1194 movd_m2r( src[2*i_src+x], mm2 );
1195 movd_m2r( src[3*i_src+x], mm3 );
1197 punpcklbw_r2r( mm7, mm0 );
1198 punpcklbw_r2r( mm7, mm1 );
1199 punpcklbw_r2r( mm7, mm2 );
1200 punpcklbw_r2r( mm7, mm3 );
1202 movq_r2r( mm0, mm4 );
1204 psubw_r2r( mm1, mm0 );
1205 psubw_r2r( mm2, mm4 );
1207 psubw_r2r( mm1, mm2 );
1208 psubw_r2r( mm1, mm3 );
1210 pmaddwd_r2r( mm0, mm0 );
1211 pmaddwd_r2r( mm4, mm4 );
1212 pmaddwd_r2r( mm2, mm2 );
1213 pmaddwd_r2r( mm3, mm3 );
1214 paddd_r2r( mm0, mm2 );
1215 paddd_r2r( mm4, mm3 );
1216 paddd_r2r( mm2, mm5 );
1217 paddd_r2r( mm3, mm6 );
1220 movq_r2r( mm5, mm0 );
1221 psrlq_i2r( 32, mm0 );
1222 paddd_r2r( mm0, mm5 );
1223 movd_r2m( mm5, fr );
1225 movq_r2r( mm6, mm0 );
1226 psrlq_i2r( 32, mm0 );
1227 paddd_r2r( mm0, mm6 );
1228 movd_r2m( mm6, ff );
1230 if( ff < 6*fr/8 && fr > 32 )
1239 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1240 uint8_t *src1, int i_src1,
1241 uint8_t *src2, int i_src2 )
1246 for( y = 0; y < 8; y += 2 )
1248 memcpy( dst, src1, 8 );
1251 for( x = 0; x < 8; x++ )
1252 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1260 #ifdef CAN_COMPILE_MMXEXT
1261 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1262 uint8_t *src1, int i_src1,
1263 uint8_t *src2, int i_src2 )
1265 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1269 pxor_r2r( mm7, mm7 );
1270 for( y = 0; y < 8; y += 2 )
1272 for( x = 0; x < 8; x +=4 )
1274 movd_m2r( src1[x], mm0 );
1275 movd_r2m( mm0, dst[x] );
1277 movd_m2r( src2[x], mm1 );
1278 movd_m2r( src1[i_src1+x], mm2 );
1280 punpcklbw_r2r( mm7, mm0 );
1281 punpcklbw_r2r( mm7, mm1 );
1282 punpcklbw_r2r( mm7, mm2 );
1283 paddw_r2r( mm1, mm1 );
1284 movq_r2r( mm1, mm3 );
1285 paddw_r2r( mm3, mm3 );
1286 paddw_r2r( mm2, mm0 );
1287 paddw_r2r( mm3, mm1 );
1288 paddw_m2r( m_4, mm1 );
1289 paddw_r2r( mm1, mm0 );
1290 psraw_i2r( 3, mm0 );
1291 packuswb_r2r( mm7, mm0 );
1292 movd_r2m( mm0, dst[i_dst+x] );
1303 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1306 for( y = 0; y < 8; y++ )
1307 memset( &dst[y*i_dst], v, 8 );
1310 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1313 * TODO: a better one for the inner part.
1315 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1316 uint8_t *src, int i_src )
1321 for( y = 0; y < 8; y += 2 )
1323 memcpy( dst, src, 8 );
1326 for( x = 0; x < 8; x++ )
1327 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1332 #ifdef CAN_COMPILE_MMXEXT
1333 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1334 uint8_t *src, int i_src )
1339 for( y = 0; y < 8; y += 2 )
1341 movq_m2r( src[0], mm0 );
1342 movq_r2m( mm0, dst[0] );
1345 movq_m2r( src[2*i_src], mm1 );
1346 pavgb_r2r( mm1, mm0 );
1348 movq_r2m( mm0, dst[0] );
1356 /* XDeint8x8Field: Edge oriented interpolation
1357 * (Need -4 and +5 pixels H, +1 line)
1359 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1360 uint8_t *src, int i_src )
1365 for( y = 0; y < 8; y += 2 )
1367 memcpy( dst, src, 8 );
1370 for( x = 0; x < 8; x++ )
1372 uint8_t *src2 = &src[2*i_src];
1373 /* I use 8 pixels just to match the MMX version, but it's overkill
1374 * 5 would be enough (less isn't good) */
1375 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1376 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1377 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1378 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1380 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1381 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1382 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1383 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1385 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1386 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1387 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1388 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1390 if( c0 < c1 && c1 <= c2 )
1391 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1392 else if( c2 < c1 && c1 <= c0 )
1393 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1395 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1402 #ifdef CAN_COMPILE_MMXEXT
1403 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1404 uint8_t *src, int i_src )
1409 for( y = 0; y < 8; y += 2 )
1411 memcpy( dst, src, 8 );
1414 for( x = 0; x < 8; x++ )
1416 uint8_t *src2 = &src[2*i_src];
1419 movq_m2r( src[x-2], mm0 );
1420 movq_m2r( src[x-3], mm1 );
1421 movq_m2r( src[x-4], mm2 );
1423 psadbw_m2r( src2[x-4], mm0 );
1424 psadbw_m2r( src2[x-3], mm1 );
1425 psadbw_m2r( src2[x-2], mm2 );
1427 movd_r2m( mm0, c2 );
1428 movd_r2m( mm1, c1 );
1429 movd_r2m( mm2, c0 );
1431 if( c0 < c1 && c1 <= c2 )
1432 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1433 else if( c2 < c1 && c1 <= c0 )
1434 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1436 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1445 /* NxN arbitray size (and then only use pixel in the NxN block)
1447 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1448 int i_height, int i_width )
1455 /* Detect interlacing */
1456 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1459 for( y = 0; y < i_height - 2; y += 2 )
1461 const uint8_t *s = &src[y*i_src];
1462 for( x = 0; x < i_width; x++ )
1464 fr += ssd(s[ x] - s[1*i_src+x]);
1465 ff += ssd(s[ x] - s[2*i_src+x]);
1467 if( ff < fr && fr > i_width / 2 )
1471 return fc < 2 ? false : true;
1474 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1475 uint8_t *src, int i_src,
1476 int i_width, int i_height )
1481 for( y = 0; y < i_height; y += 2 )
1483 memcpy( dst, src, i_width );
1486 if( y < i_height - 2 )
1488 for( x = 0; x < i_width; x++ )
1489 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1493 /* Blend last line */
1494 for( x = 0; x < i_width; x++ )
1495 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1502 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1503 uint8_t *src, int i_src,
1504 int i_width, int i_height )
1509 for( y = 0; y < i_height; y += 2 )
1511 memcpy( dst, src, i_width );
1514 if( y < i_height - 2 )
1516 for( x = 0; x < i_width; x++ )
1517 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1521 /* Blend last line */
1522 for( x = 0; x < i_width; x++ )
1523 dst[x] = (src[x] + src[i_src+x]) >> 1;
1530 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1531 int i_width, int i_height )
1533 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1534 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1536 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1540 static inline int median( int a, int b, int c )
1542 int min = a, max =a;
1553 return a + b + c - min - max;
1559 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1560 uint8_t *src, int i_src,
1561 const int i_mbx, int i_modx )
1565 for( x = 0; x < i_mbx; x++ )
1568 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1570 if( x == 0 || x == i_mbx - 1 )
1571 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1573 XDeint8x8FieldC( dst, i_dst, src, i_src );
1577 XDeint8x8MergeC( dst, i_dst,
1578 &src[0*i_src], 2*i_src,
1579 &src[1*i_src], 2*i_src );
1587 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1589 #ifdef CAN_COMPILE_MMXEXT
1590 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1591 uint8_t *src, int i_src,
1592 const int i_mbx, int i_modx )
1596 /* Reset current line */
1597 for( x = 0; x < i_mbx; x++ )
1600 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1602 if( x == 0 || x == i_mbx - 1 )
1603 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1605 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1609 XDeint8x8MergeMMXEXT( dst, i_dst,
1610 &src[0*i_src], 2*i_src,
1611 &src[1*i_src], 2*i_src );
1619 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1623 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1627 /* Copy image and skip lines */
1628 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1630 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1631 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1633 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1634 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1636 const int i_dst = p_outpic->p[i_plane].i_pitch;
1637 const int i_src = p_pic->p[i_plane].i_pitch;
1641 for( y = 0; y < i_mby; y++ )
1643 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1644 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1646 #ifdef CAN_COMPILE_MMXEXT
1647 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1648 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1651 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1654 /* Last line (C only)*/
1657 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1658 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1660 for( x = 0; x < i_mbx; x++ )
1662 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1669 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1673 #ifdef CAN_COMPILE_MMXEXT
1674 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1679 /*****************************************************************************
1680 * SendEvents: forward mouse and keyboard events to the parent p_vout
1681 *****************************************************************************/
1682 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1683 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1685 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
1686 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1687 vlc_value_t sentval = newval;
1689 if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
1692 var_Set( p_vout, psz_var, sentval );
1697 /*****************************************************************************
1698 * FilterCallback: called when changing the deinterlace method on the fly.
1699 *****************************************************************************/
1700 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1701 vlc_value_t oldval, vlc_value_t newval,
1704 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1705 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1707 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1709 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1710 const bool b_old_half_height = p_vout->p_sys->b_half_height;
1712 SetFilterMethod( p_vout, newval.psz_string );
1714 if( !b_old_half_height == !p_vout->p_sys->b_half_height )
1716 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1720 /* We need to kill the old vout */
1721 if( p_vout->p_sys->p_vout )
1723 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1724 vout_CloseAndRelease( p_vout->p_sys->p_vout );
1727 /* Try to open a new video output */
1728 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
1730 if( p_vout->p_sys->p_vout == NULL )
1732 /* Everything failed */
1733 msg_Err( p_vout, "cannot open vout, aborting" );
1735 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1736 return VLC_EGENERIC;
1739 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1741 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1745 /*****************************************************************************
1746 * SendEventsToChild: forward events to the child/children vout
1747 *****************************************************************************/
1748 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
1749 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1751 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1752 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1753 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
1758 /*****************************************************************************
1759 * video filter2 functions
1760 *****************************************************************************/
1761 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1763 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1764 picture_t *p_pic_dst;
1766 /* Request output picture */
1767 p_pic_dst = filter_NewPicture( p_filter );
1768 if( p_pic_dst == NULL )
1771 switch( p_vout->p_sys->i_mode )
1773 case DEINTERLACE_DISCARD:
1775 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1777 msg_Err( p_vout, "discarding lines is not supported yet" );
1778 picture_Release( p_pic_dst );
1782 case DEINTERLACE_BOB:
1784 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1785 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1789 case DEINTERLACE_LINEAR:
1791 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1792 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1794 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1795 picture_Release( p_pic_dst );
1799 case DEINTERLACE_MEAN:
1800 RenderMean( p_vout, p_pic_dst, p_pic );
1803 case DEINTERLACE_BLEND:
1804 RenderBlend( p_vout, p_pic_dst, p_pic );
1808 RenderX( p_pic_dst, p_pic );
1812 picture_CopyProperties( p_pic_dst, p_pic );
1813 p_pic_dst->b_progressive = true;
1815 picture_Release( p_pic );
1819 /*****************************************************************************
1821 *****************************************************************************/
1822 static int OpenFilter( vlc_object_t *p_this )
1824 filter_t *p_filter = (filter_t*)p_this;
1825 vout_thread_t *p_vout;
1828 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
1829 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
1830 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
1831 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
1833 return VLC_EGENERIC;
1836 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1838 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1839 vlc_object_attach( p_vout, p_filter );
1840 p_filter->p_sys = (filter_sys_t *)p_vout;
1841 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1843 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1845 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1846 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1847 var_Set( p_filter, "deinterlace-mode", val );
1848 free( val.psz_string );
1850 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1852 vlc_object_detach( p_vout );
1853 vlc_object_release( p_vout );
1854 return VLC_EGENERIC;
1857 p_filter->pf_video_filter = Deinterlace;
1859 msg_Dbg( p_filter, "deinterlacing" );
1864 /*****************************************************************************
1865 * CloseFilter: clean up the filter
1866 *****************************************************************************/
1867 static void CloseFilter( vlc_object_t *p_this )
1869 filter_t *p_filter = (filter_t*)p_this;
1870 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1872 Destroy( VLC_OBJECT(p_vout) );
1873 vlc_object_detach( p_vout );
1874 vlc_object_release( p_vout );