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 static void GetOutputFormat( vout_thread_t *p_vout,
325 video_format_t *p_dst, const video_format_t *p_src )
329 if( p_vout->p_sys->b_half_height )
331 p_dst->i_height /= 2;
332 p_dst->i_visible_height /= 2;
333 p_dst->i_y_offset /= 2;
334 p_dst->i_sar_den *= 2;
337 if( p_src->i_chroma == VLC_FOURCC('I','4','2','2') )
339 switch( p_vout->p_sys->i_mode )
341 case DEINTERLACE_MEAN:
342 case DEINTERLACE_LINEAR:
344 p_dst->i_chroma = VLC_FOURCC('I','4','2','2');
347 p_dst->i_chroma = VLC_FOURCC('I','4','2','0');
353 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
355 return i_chroma == VLC_FOURCC('I','4','2','0') ||
356 i_chroma == VLC_FOURCC('I','Y','U','V') ||
357 i_chroma == VLC_FOURCC('Y','V','1','2') ||
358 i_chroma == VLC_FOURCC('I','4','2','2');
361 /*****************************************************************************
362 * Init: initialize Deinterlace video thread output method
363 *****************************************************************************/
364 static int Init( vout_thread_t *p_vout )
369 I_OUTPUTPICTURES = 0;
371 if( !IsChromaSupported( p_vout->render.i_chroma ) )
372 return VLC_EGENERIC; /* unknown chroma */
374 /* Initialize the output structure, full of directbuffers since we want
375 * the decoder to output directly to our structures. */
376 p_vout->output.i_chroma = p_vout->render.i_chroma;
377 p_vout->output.i_width = p_vout->render.i_width;
378 p_vout->output.i_height = p_vout->render.i_height;
379 p_vout->output.i_aspect = p_vout->render.i_aspect;
380 p_vout->fmt_out = p_vout->fmt_in;
382 /* Try to open the real video output */
383 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
385 if( p_vout->p_sys->p_vout == NULL )
387 /* Everything failed */
388 msg_Err( p_vout, "cannot open vout, aborting" );
393 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
395 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
397 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
399 ADD_PARENT_CALLBACKS( SendEventsToChild );
404 /*****************************************************************************
405 * SpawnRealVout: spawn the real video output.
406 *****************************************************************************/
407 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
409 msg_Dbg( p_vout, "spawning the real video output" );
412 GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
414 return vout_Create( p_vout, &fmt );
417 /*****************************************************************************
418 * End: terminate Deinterlace video thread output method
419 *****************************************************************************/
420 static void End( vout_thread_t *p_vout )
424 DEL_PARENT_CALLBACKS( SendEventsToChild );
426 if( p_vout->p_sys->p_vout )
427 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
429 /* Free the fake output buffers we allocated */
430 for( i_index = I_OUTPUTPICTURES ; i_index ; )
433 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
436 if( p_vout->p_sys->p_vout )
437 vout_CloseAndRelease( p_vout->p_sys->p_vout );
440 /*****************************************************************************
441 * Destroy: destroy Deinterlace video thread output method
442 *****************************************************************************
443 * Terminate an output method created by DeinterlaceCreateOutputMethod
444 *****************************************************************************/
445 static void Destroy( vlc_object_t *p_this )
447 vout_thread_t *p_vout = (vout_thread_t *)p_this;
448 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
449 free( p_vout->p_sys );
452 /*****************************************************************************
453 * Render: displays previously rendered output
454 *****************************************************************************
455 * This function send the currently rendered image to Deinterlace image,
456 * waits until it is displayed and switch the two rendering buffers, preparing
458 *****************************************************************************/
459 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
461 vout_sys_t *p_sys = p_vout->p_sys;
462 picture_t *pp_outpic[2];
464 /* FIXME are they needed ? */
465 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
466 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
467 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
468 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
470 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
471 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
472 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
473 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
474 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
475 if( p_vout->p_sys->b_half_height )
477 p_sys->p_vout->fmt_in.i_y_offset /= 2;
478 p_sys->p_vout->fmt_in.i_visible_height /= 2;
481 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
483 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
485 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
486 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
487 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
489 video_format_t fmt = p_vout->fmt_out;
490 if( p_vout->p_sys->b_half_height )
492 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
496 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
501 pp_outpic[0] = pp_outpic[1] = NULL;
503 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
505 /* Get a new picture */
506 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
510 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
512 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
515 msleep( VOUT_OUTMEM_SLEEP );
518 pp_outpic[0]->date = p_pic->date;
520 /* If we are using double rate, get an additional new picture */
521 if( p_vout->p_sys->b_double_rate )
523 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
527 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
529 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
530 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
533 msleep( VOUT_OUTMEM_SLEEP );
536 /* 20ms is a bit arbitrary, but it's only for the first image we get */
537 if( !p_vout->p_sys->last_date )
538 pp_outpic[1]->date = p_pic->date + 20000;
540 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
541 p_vout->p_sys->last_date = p_pic->date;
544 switch( p_vout->p_sys->i_mode )
546 case DEINTERLACE_DISCARD:
547 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
548 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
551 case DEINTERLACE_BOB:
552 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
553 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
554 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
555 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
558 case DEINTERLACE_LINEAR:
559 RenderLinear( 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 RenderLinear( 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_MEAN:
566 RenderMean( p_vout, pp_outpic[0], p_pic );
567 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
570 case DEINTERLACE_BLEND:
571 RenderBlend( p_vout, pp_outpic[0], p_pic );
572 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
576 RenderX( pp_outpic[0], p_pic );
577 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
580 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
583 /*****************************************************************************
584 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
585 *****************************************************************************/
586 static void RenderDiscard( vout_thread_t *p_vout,
587 picture_t *p_outpic, picture_t *p_pic, int i_field )
591 /* Copy image and skip lines */
592 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
594 uint8_t *p_in, *p_out_end, *p_out;
597 p_in = p_pic->p[i_plane].p_pixels
598 + i_field * p_pic->p[i_plane].i_pitch;
600 p_out = p_outpic->p[i_plane].p_pixels;
601 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
602 * p_outpic->p[i_plane].i_visible_lines;
604 switch( p_vout->render.i_chroma )
606 case VLC_FOURCC('I','4','2','0'):
607 case VLC_FOURCC('I','Y','U','V'):
608 case VLC_FOURCC('Y','V','1','2'):
610 for( ; p_out < p_out_end ; )
612 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
614 p_out += p_outpic->p[i_plane].i_pitch;
615 p_in += 2 * p_pic->p[i_plane].i_pitch;
619 case VLC_FOURCC('I','4','2','2'):
621 i_increment = 2 * p_pic->p[i_plane].i_pitch;
623 if( i_plane == Y_PLANE )
625 for( ; p_out < p_out_end ; )
627 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
628 p_out += p_outpic->p[i_plane].i_pitch;
629 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
630 p_out += p_outpic->p[i_plane].i_pitch;
636 for( ; p_out < p_out_end ; )
638 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
639 p_out += p_outpic->p[i_plane].i_pitch;
651 /*****************************************************************************
652 * RenderBob: renders a BOB picture - simple copy
653 *****************************************************************************/
654 static void RenderBob( vout_thread_t *p_vout,
655 picture_t *p_outpic, picture_t *p_pic, int i_field )
659 /* Copy image and skip lines */
660 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
662 uint8_t *p_in, *p_out_end, *p_out;
664 p_in = p_pic->p[i_plane].p_pixels;
665 p_out = p_outpic->p[i_plane].p_pixels;
666 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
667 * p_outpic->p[i_plane].i_visible_lines;
669 switch( p_vout->render.i_chroma )
671 case VLC_FOURCC('I','4','2','0'):
672 case VLC_FOURCC('I','Y','U','V'):
673 case VLC_FOURCC('Y','V','1','2'):
674 /* For BOTTOM field we need to add the first line */
677 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
678 p_in += p_pic->p[i_plane].i_pitch;
679 p_out += p_outpic->p[i_plane].i_pitch;
682 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
684 for( ; p_out < p_out_end ; )
686 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
688 p_out += p_outpic->p[i_plane].i_pitch;
690 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
692 p_in += 2 * p_pic->p[i_plane].i_pitch;
693 p_out += p_outpic->p[i_plane].i_pitch;
696 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
698 /* For TOP field we need to add the last line */
701 p_in += p_pic->p[i_plane].i_pitch;
702 p_out += p_outpic->p[i_plane].i_pitch;
703 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
707 case VLC_FOURCC('I','4','2','2'):
708 /* For BOTTOM field we need to add the first line */
711 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
712 p_in += p_pic->p[i_plane].i_pitch;
713 p_out += p_outpic->p[i_plane].i_pitch;
716 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
718 if( i_plane == Y_PLANE )
720 for( ; p_out < p_out_end ; )
722 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
724 p_out += p_outpic->p[i_plane].i_pitch;
726 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
728 p_in += 2 * p_pic->p[i_plane].i_pitch;
729 p_out += p_outpic->p[i_plane].i_pitch;
734 for( ; p_out < p_out_end ; )
736 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
738 p_out += p_outpic->p[i_plane].i_pitch;
739 p_in += 2 * p_pic->p[i_plane].i_pitch;
743 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
745 /* For TOP field we need to add the last line */
748 p_in += p_pic->p[i_plane].i_pitch;
749 p_out += p_outpic->p[i_plane].i_pitch;
750 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
757 #define Merge p_vout->p_sys->pf_merge
758 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
760 /*****************************************************************************
761 * RenderLinear: BOB with linear interpolation
762 *****************************************************************************/
763 static void RenderLinear( vout_thread_t *p_vout,
764 picture_t *p_outpic, picture_t *p_pic, int i_field )
768 /* Copy image and skip lines */
769 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
771 uint8_t *p_in, *p_out_end, *p_out;
773 p_in = p_pic->p[i_plane].p_pixels;
774 p_out = p_outpic->p[i_plane].p_pixels;
775 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
776 * p_outpic->p[i_plane].i_visible_lines;
778 /* For BOTTOM field we need to add the first line */
781 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
782 p_in += p_pic->p[i_plane].i_pitch;
783 p_out += p_outpic->p[i_plane].i_pitch;
786 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
788 for( ; p_out < p_out_end ; )
790 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
792 p_out += p_outpic->p[i_plane].i_pitch;
794 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
795 p_pic->p[i_plane].i_pitch );
797 p_in += 2 * p_pic->p[i_plane].i_pitch;
798 p_out += p_outpic->p[i_plane].i_pitch;
801 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
803 /* For TOP field we need to add the last line */
806 p_in += p_pic->p[i_plane].i_pitch;
807 p_out += p_outpic->p[i_plane].i_pitch;
808 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
814 static void RenderMean( vout_thread_t *p_vout,
815 picture_t *p_outpic, picture_t *p_pic )
819 /* Copy image and skip lines */
820 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
822 uint8_t *p_in, *p_out_end, *p_out;
824 p_in = p_pic->p[i_plane].p_pixels;
826 p_out = p_outpic->p[i_plane].p_pixels;
827 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
828 * p_outpic->p[i_plane].i_visible_lines;
830 /* All lines: mean value */
831 for( ; p_out < p_out_end ; )
833 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
834 p_pic->p[i_plane].i_pitch );
836 p_out += p_outpic->p[i_plane].i_pitch;
837 p_in += 2 * p_pic->p[i_plane].i_pitch;
843 static void RenderBlend( vout_thread_t *p_vout,
844 picture_t *p_outpic, picture_t *p_pic )
848 /* Copy image and skip lines */
849 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
851 uint8_t *p_in, *p_out_end, *p_out;
853 p_in = p_pic->p[i_plane].p_pixels;
855 p_out = p_outpic->p[i_plane].p_pixels;
856 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
857 * p_outpic->p[i_plane].i_visible_lines;
859 switch( p_vout->render.i_chroma )
861 case VLC_FOURCC('I','4','2','0'):
862 case VLC_FOURCC('I','Y','U','V'):
863 case VLC_FOURCC('Y','V','1','2'):
864 /* First line: simple copy */
865 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
866 p_out += p_outpic->p[i_plane].i_pitch;
868 /* Remaining lines: mean value */
869 for( ; p_out < p_out_end ; )
871 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
872 p_pic->p[i_plane].i_pitch );
874 p_out += p_outpic->p[i_plane].i_pitch;
875 p_in += p_pic->p[i_plane].i_pitch;
879 case VLC_FOURCC('I','4','2','2'):
880 /* First line: simple copy */
881 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
882 p_out += p_outpic->p[i_plane].i_pitch;
884 /* Remaining lines: mean value */
885 if( i_plane == Y_PLANE )
887 for( ; p_out < p_out_end ; )
889 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
890 p_pic->p[i_plane].i_pitch );
892 p_out += p_outpic->p[i_plane].i_pitch;
893 p_in += p_pic->p[i_plane].i_pitch;
899 for( ; p_out < p_out_end ; )
901 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
902 p_pic->p[i_plane].i_pitch );
904 p_out += p_outpic->p[i_plane].i_pitch;
905 p_in += 2*p_pic->p[i_plane].i_pitch;
916 static void MergeGeneric( void *_p_dest, const void *_p_s1,
917 const void *_p_s2, size_t i_bytes )
919 uint8_t* p_dest = (uint8_t*)_p_dest;
920 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
921 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
922 uint8_t* p_end = p_dest + i_bytes - 8;
924 while( p_dest < p_end )
926 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
927 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
928 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
929 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
930 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
931 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
932 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
933 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
938 while( p_dest < p_end )
940 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
944 #if defined(CAN_COMPILE_MMXEXT)
945 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
948 uint8_t* p_dest = (uint8_t*)_p_dest;
949 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
950 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
951 uint8_t* p_end = p_dest + i_bytes - 8;
952 while( p_dest < p_end )
954 __asm__ __volatile__( "movq %2,%%mm1;"
956 "movq %%mm1, %0" :"=m" (*p_dest):
966 while( p_dest < p_end )
968 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
973 #if defined(CAN_COMPILE_3DNOW)
974 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
977 uint8_t* p_dest = (uint8_t*)_p_dest;
978 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
979 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
980 uint8_t* p_end = p_dest + i_bytes - 8;
981 while( p_dest < p_end )
983 __asm__ __volatile__( "movq %2,%%mm1;"
985 "movq %%mm1, %0" :"=m" (*p_dest):
995 while( p_dest < p_end )
997 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1002 #if defined(CAN_COMPILE_SSE)
1003 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1006 uint8_t* p_dest = (uint8_t*)_p_dest;
1007 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1008 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1010 while( (uintptr_t)p_s1 % 16 )
1012 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1014 p_end = p_dest + i_bytes - 16;
1015 while( p_dest < p_end )
1017 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1019 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1029 while( p_dest < p_end )
1031 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1036 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1037 static void EndMMX( void )
1039 __asm__ __volatile__( "emms" :: );
1043 #if defined(CAN_COMPILE_3DNOW)
1044 static void End3DNow( void )
1046 __asm__ __volatile__( "femms" :: );
1050 #ifdef CAN_COMPILE_C_ALTIVEC
1051 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1052 const void *_p_s2, size_t i_bytes )
1054 uint8_t *p_dest = (uint8_t *)_p_dest;
1055 uint8_t *p_s1 = (uint8_t *)_p_s1;
1056 uint8_t *p_s2 = (uint8_t *)_p_s2;
1057 uint8_t *p_end = p_dest + i_bytes - 15;
1059 /* Use C until the first 16-bytes aligned destination pixel */
1060 while( (int)p_dest & 0xF )
1062 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1065 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1067 /* Unaligned source */
1068 vector unsigned char s1v, s2v, destv;
1069 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1070 vector unsigned char perm1v, perm2v;
1072 perm1v = vec_lvsl( 0, p_s1 );
1073 perm2v = vec_lvsl( 0, p_s2 );
1074 s1oldv = vec_ld( 0, p_s1 );
1075 s2oldv = vec_ld( 0, p_s2 );
1077 while( p_dest < p_end )
1079 s1newv = vec_ld( 16, p_s1 );
1080 s2newv = vec_ld( 16, p_s2 );
1081 s1v = vec_perm( s1oldv, s1newv, perm1v );
1082 s2v = vec_perm( s2oldv, s2newv, perm2v );
1085 destv = vec_avg( s1v, s2v );
1086 vec_st( destv, 0, p_dest );
1095 /* Aligned source */
1096 vector unsigned char s1v, s2v, destv;
1098 while( p_dest < p_end )
1100 s1v = vec_ld( 0, p_s1 );
1101 s2v = vec_ld( 0, p_s2 );
1102 destv = vec_avg( s1v, s2v );
1103 vec_st( destv, 0, p_dest );
1113 while( p_dest < p_end )
1115 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1120 /*****************************************************************************
1121 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1122 * and apply a process to recreate the bottom field :
1123 * If a 8x8 block is classified as :
1124 * - progressive: it applies a small blend (1,6,1)
1126 * * in the MMX version: we do a ME between the 2 fields, if there is a
1127 * good match we use MC to recreate the bottom field (with a small
1129 * * otherwise: it recreates the bottom field by an edge oriented
1131 *****************************************************************************/
1133 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1134 * XXX: It need to access to 8x10
1135 * We use more than 8 lines to help with scrolling (text)
1136 * (and because XDeint8x8Frame use line 9)
1137 * XXX: smooth/uniform area with noise detection doesn't works well
1138 * but it's not really a problem because they don't have much details anyway
1140 static inline int ssd( int a ) { return a*a; }
1141 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1147 /* Detect interlacing */
1149 for( y = 0; y < 7; y += 2 )
1152 for( x = 0; x < 8; x++ )
1154 fr += ssd(src[ x] - src[1*i_src+x]) +
1155 ssd(src[i_src+x] - src[2*i_src+x]);
1156 ff += ssd(src[ x] - src[2*i_src+x]) +
1157 ssd(src[i_src+x] - src[3*i_src+x]);
1159 if( ff < 6*fr/8 && fr > 32 )
1165 return fc < 1 ? false : true;
1167 #ifdef CAN_COMPILE_MMXEXT
1168 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1175 /* Detect interlacing */
1177 pxor_r2r( mm7, mm7 );
1178 for( y = 0; y < 9; y += 2 )
1181 pxor_r2r( mm5, mm5 );
1182 pxor_r2r( mm6, mm6 );
1183 for( x = 0; x < 8; x+=4 )
1185 movd_m2r( src[ x], mm0 );
1186 movd_m2r( src[1*i_src+x], mm1 );
1187 movd_m2r( src[2*i_src+x], mm2 );
1188 movd_m2r( src[3*i_src+x], mm3 );
1190 punpcklbw_r2r( mm7, mm0 );
1191 punpcklbw_r2r( mm7, mm1 );
1192 punpcklbw_r2r( mm7, mm2 );
1193 punpcklbw_r2r( mm7, mm3 );
1195 movq_r2r( mm0, mm4 );
1197 psubw_r2r( mm1, mm0 );
1198 psubw_r2r( mm2, mm4 );
1200 psubw_r2r( mm1, mm2 );
1201 psubw_r2r( mm1, mm3 );
1203 pmaddwd_r2r( mm0, mm0 );
1204 pmaddwd_r2r( mm4, mm4 );
1205 pmaddwd_r2r( mm2, mm2 );
1206 pmaddwd_r2r( mm3, mm3 );
1207 paddd_r2r( mm0, mm2 );
1208 paddd_r2r( mm4, mm3 );
1209 paddd_r2r( mm2, mm5 );
1210 paddd_r2r( mm3, mm6 );
1213 movq_r2r( mm5, mm0 );
1214 psrlq_i2r( 32, mm0 );
1215 paddd_r2r( mm0, mm5 );
1216 movd_r2m( mm5, fr );
1218 movq_r2r( mm6, mm0 );
1219 psrlq_i2r( 32, mm0 );
1220 paddd_r2r( mm0, mm6 );
1221 movd_r2m( mm6, ff );
1223 if( ff < 6*fr/8 && fr > 32 )
1232 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1233 uint8_t *src1, int i_src1,
1234 uint8_t *src2, int i_src2 )
1239 for( y = 0; y < 8; y += 2 )
1241 memcpy( dst, src1, 8 );
1244 for( x = 0; x < 8; x++ )
1245 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1253 #ifdef CAN_COMPILE_MMXEXT
1254 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1255 uint8_t *src1, int i_src1,
1256 uint8_t *src2, int i_src2 )
1258 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1262 pxor_r2r( mm7, mm7 );
1263 for( y = 0; y < 8; y += 2 )
1265 for( x = 0; x < 8; x +=4 )
1267 movd_m2r( src1[x], mm0 );
1268 movd_r2m( mm0, dst[x] );
1270 movd_m2r( src2[x], mm1 );
1271 movd_m2r( src1[i_src1+x], mm2 );
1273 punpcklbw_r2r( mm7, mm0 );
1274 punpcklbw_r2r( mm7, mm1 );
1275 punpcklbw_r2r( mm7, mm2 );
1276 paddw_r2r( mm1, mm1 );
1277 movq_r2r( mm1, mm3 );
1278 paddw_r2r( mm3, mm3 );
1279 paddw_r2r( mm2, mm0 );
1280 paddw_r2r( mm3, mm1 );
1281 paddw_m2r( m_4, mm1 );
1282 paddw_r2r( mm1, mm0 );
1283 psraw_i2r( 3, mm0 );
1284 packuswb_r2r( mm7, mm0 );
1285 movd_r2m( mm0, dst[i_dst+x] );
1296 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1299 for( y = 0; y < 8; y++ )
1300 memset( &dst[y*i_dst], v, 8 );
1303 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1306 * TODO: a better one for the inner part.
1308 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1309 uint8_t *src, int i_src )
1314 for( y = 0; y < 8; y += 2 )
1316 memcpy( dst, src, 8 );
1319 for( x = 0; x < 8; x++ )
1320 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1325 #ifdef CAN_COMPILE_MMXEXT
1326 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1327 uint8_t *src, int i_src )
1332 for( y = 0; y < 8; y += 2 )
1334 movq_m2r( src[0], mm0 );
1335 movq_r2m( mm0, dst[0] );
1338 movq_m2r( src[2*i_src], mm1 );
1339 pavgb_r2r( mm1, mm0 );
1341 movq_r2m( mm0, dst[0] );
1349 /* XDeint8x8Field: Edge oriented interpolation
1350 * (Need -4 and +5 pixels H, +1 line)
1352 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1353 uint8_t *src, int i_src )
1358 for( y = 0; y < 8; y += 2 )
1360 memcpy( dst, src, 8 );
1363 for( x = 0; x < 8; x++ )
1365 uint8_t *src2 = &src[2*i_src];
1366 /* I use 8 pixels just to match the MMX version, but it's overkill
1367 * 5 would be enough (less isn't good) */
1368 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1369 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1370 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1371 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1373 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1374 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1375 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1376 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1378 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1379 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1380 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1381 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1383 if( c0 < c1 && c1 <= c2 )
1384 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1385 else if( c2 < c1 && c1 <= c0 )
1386 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1388 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1395 #ifdef CAN_COMPILE_MMXEXT
1396 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1397 uint8_t *src, int i_src )
1402 for( y = 0; y < 8; y += 2 )
1404 memcpy( dst, src, 8 );
1407 for( x = 0; x < 8; x++ )
1409 uint8_t *src2 = &src[2*i_src];
1412 movq_m2r( src[x-2], mm0 );
1413 movq_m2r( src[x-3], mm1 );
1414 movq_m2r( src[x-4], mm2 );
1416 psadbw_m2r( src2[x-4], mm0 );
1417 psadbw_m2r( src2[x-3], mm1 );
1418 psadbw_m2r( src2[x-2], mm2 );
1420 movd_r2m( mm0, c2 );
1421 movd_r2m( mm1, c1 );
1422 movd_r2m( mm2, c0 );
1424 if( c0 < c1 && c1 <= c2 )
1425 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1426 else if( c2 < c1 && c1 <= c0 )
1427 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1429 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1438 /* NxN arbitray size (and then only use pixel in the NxN block)
1440 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1441 int i_height, int i_width )
1448 /* Detect interlacing */
1449 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1452 for( y = 0; y < i_height - 2; y += 2 )
1454 const uint8_t *s = &src[y*i_src];
1455 for( x = 0; x < i_width; x++ )
1457 fr += ssd(s[ x] - s[1*i_src+x]);
1458 ff += ssd(s[ x] - s[2*i_src+x]);
1460 if( ff < fr && fr > i_width / 2 )
1464 return fc < 2 ? false : true;
1467 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1468 uint8_t *src, int i_src,
1469 int i_width, int i_height )
1474 for( y = 0; y < i_height; y += 2 )
1476 memcpy( dst, src, i_width );
1479 if( y < i_height - 2 )
1481 for( x = 0; x < i_width; x++ )
1482 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1486 /* Blend last line */
1487 for( x = 0; x < i_width; x++ )
1488 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1495 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1496 uint8_t *src, int i_src,
1497 int i_width, int i_height )
1502 for( y = 0; y < i_height; y += 2 )
1504 memcpy( dst, src, i_width );
1507 if( y < i_height - 2 )
1509 for( x = 0; x < i_width; x++ )
1510 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1514 /* Blend last line */
1515 for( x = 0; x < i_width; x++ )
1516 dst[x] = (src[x] + src[i_src+x]) >> 1;
1523 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1524 int i_width, int i_height )
1526 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1527 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1529 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1533 static inline int median( int a, int b, int c )
1535 int min = a, max =a;
1546 return a + b + c - min - max;
1552 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1553 uint8_t *src, int i_src,
1554 const int i_mbx, int i_modx )
1558 for( x = 0; x < i_mbx; x++ )
1561 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1563 if( x == 0 || x == i_mbx - 1 )
1564 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1566 XDeint8x8FieldC( dst, i_dst, src, i_src );
1570 XDeint8x8MergeC( dst, i_dst,
1571 &src[0*i_src], 2*i_src,
1572 &src[1*i_src], 2*i_src );
1580 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1582 #ifdef CAN_COMPILE_MMXEXT
1583 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1584 uint8_t *src, int i_src,
1585 const int i_mbx, int i_modx )
1589 /* Reset current line */
1590 for( x = 0; x < i_mbx; x++ )
1593 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1595 if( x == 0 || x == i_mbx - 1 )
1596 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1598 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1602 XDeint8x8MergeMMXEXT( dst, i_dst,
1603 &src[0*i_src], 2*i_src,
1604 &src[1*i_src], 2*i_src );
1612 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1616 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1620 /* Copy image and skip lines */
1621 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1623 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1624 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1626 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1627 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1629 const int i_dst = p_outpic->p[i_plane].i_pitch;
1630 const int i_src = p_pic->p[i_plane].i_pitch;
1634 for( y = 0; y < i_mby; y++ )
1636 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1637 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1639 #ifdef CAN_COMPILE_MMXEXT
1640 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1641 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1644 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1647 /* Last line (C only)*/
1650 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1651 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1653 for( x = 0; x < i_mbx; x++ )
1655 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1662 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1666 #ifdef CAN_COMPILE_MMXEXT
1667 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1672 /*****************************************************************************
1673 * SendEvents: forward mouse and keyboard events to the parent p_vout
1674 *****************************************************************************/
1675 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1676 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1678 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
1679 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1680 vlc_value_t sentval = newval;
1682 if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
1685 var_Set( p_vout, psz_var, sentval );
1690 /*****************************************************************************
1691 * FilterCallback: called when changing the deinterlace method on the fly.
1692 *****************************************************************************/
1693 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1694 vlc_value_t oldval, vlc_value_t newval,
1697 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1698 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1700 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1702 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1703 const bool b_old_half_height = p_vout->p_sys->b_half_height;
1705 SetFilterMethod( p_vout, newval.psz_string );
1707 if( !b_old_half_height == !p_vout->p_sys->b_half_height )
1709 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1713 /* We need to kill the old vout */
1714 if( p_vout->p_sys->p_vout )
1716 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1717 vout_CloseAndRelease( p_vout->p_sys->p_vout );
1720 /* Try to open a new video output */
1721 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
1723 if( p_vout->p_sys->p_vout == NULL )
1725 /* Everything failed */
1726 msg_Err( p_vout, "cannot open vout, aborting" );
1728 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1729 return VLC_EGENERIC;
1732 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1734 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1738 /*****************************************************************************
1739 * SendEventsToChild: forward events to the child/children vout
1740 *****************************************************************************/
1741 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
1742 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1744 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1745 vout_thread_t *p_vout = (vout_thread_t *)p_this;
1746 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
1751 /*****************************************************************************
1752 * video filter2 functions
1753 *****************************************************************************/
1754 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1756 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1757 picture_t *p_pic_dst;
1759 /* Request output picture */
1760 p_pic_dst = filter_NewPicture( p_filter );
1761 if( p_pic_dst == NULL )
1763 picture_Release( p_pic );
1767 switch( p_vout->p_sys->i_mode )
1769 case DEINTERLACE_DISCARD:
1770 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1773 case DEINTERLACE_BOB:
1775 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1776 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1780 case DEINTERLACE_LINEAR:
1782 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1783 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1785 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1786 picture_Release( p_pic_dst );
1787 picture_Release( p_pic );
1790 case DEINTERLACE_MEAN:
1791 RenderMean( p_vout, p_pic_dst, p_pic );
1794 case DEINTERLACE_BLEND:
1795 RenderBlend( p_vout, p_pic_dst, p_pic );
1799 RenderX( p_pic_dst, p_pic );
1803 picture_CopyProperties( p_pic_dst, p_pic );
1804 p_pic_dst->b_progressive = true;
1806 picture_Release( p_pic );
1810 /*****************************************************************************
1812 *****************************************************************************/
1813 static int OpenFilter( vlc_object_t *p_this )
1815 filter_t *p_filter = (filter_t*)p_this;
1816 vout_thread_t *p_vout;
1819 if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
1820 return VLC_EGENERIC;
1822 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1824 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1825 vlc_object_attach( p_vout, p_filter );
1826 p_filter->p_sys = (filter_sys_t *)p_vout;
1827 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1829 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1831 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1833 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1834 var_Set( p_filter, "deinterlace-mode", val );
1835 free( val.psz_string );
1837 if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1839 vlc_object_detach( p_vout );
1840 vlc_object_release( p_vout );
1841 return VLC_EGENERIC;
1845 GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
1846 if( !p_filter->b_allow_fmt_out_change &&
1847 ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
1848 fmt.i_height != p_filter->fmt_in.video.i_height ) )
1850 CloseFilter( VLC_OBJECT(p_filter) );
1851 return VLC_EGENERIC;
1853 p_filter->fmt_out.video = fmt;
1854 p_filter->fmt_out.i_codec = fmt.i_chroma;
1855 p_filter->pf_video_filter = Deinterlace;
1857 msg_Dbg( p_filter, "deinterlacing" );
1862 /*****************************************************************************
1863 * CloseFilter: clean up the filter
1864 *****************************************************************************/
1865 static void CloseFilter( vlc_object_t *p_this )
1867 filter_t *p_filter = (filter_t*)p_this;
1868 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1870 Destroy( VLC_OBJECT(p_vout) );
1871 vlc_object_detach( p_vout );
1872 vlc_object_release( p_vout );