]> git.sesse.net Git - vlc/blob - modules/video_filter/deinterlace.c
1de201e3991593c2636b1f41db58ec396deaf593
[vlc] / modules / video_filter / deinterlace.c
1 /*****************************************************************************
2  * deinterlace.c : deinterlacer plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2009 the VideoLAN team
5  * $Id$
6  *
7  * Author: Sam Hocevar <sam@zoy.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <errno.h>
33
34 #ifdef HAVE_ALTIVEC_H
35 #   include <altivec.h>
36 #endif
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_vout.h>
41 #include <vlc_sout.h>
42 #include <vlc_filter.h>
43
44 #ifdef CAN_COMPILE_MMXEXT
45 #   include "mmx.h"
46 #endif
47
48 #include "filter_common.h"
49
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
56
57 /*****************************************************************************
58  * Local protypes
59  *****************************************************************************/
60 static int  Create    ( vlc_object_t * );
61 static void Destroy   ( vlc_object_t * );
62
63 static int  Init      ( vout_thread_t * );
64 static void End       ( vout_thread_t * );
65 static void Render    ( vout_thread_t *, picture_t * );
66
67 static int  MouseEvent( vlc_object_t *p_this, char const *psz_var,
68                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
69
70 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
71 static void RenderBob    ( vout_thread_t *, picture_t *, picture_t *, int );
72 static void RenderMean   ( vout_thread_t *, picture_t *, picture_t * );
73 static void RenderBlend  ( vout_thread_t *, picture_t *, picture_t * );
74 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
75 static void RenderX      ( picture_t *, picture_t * );
76
77 static void MergeGeneric ( void *, const void *, const void *, size_t );
78 #if defined(CAN_COMPILE_C_ALTIVEC)
79 static void MergeAltivec ( void *, const void *, const void *, size_t );
80 #endif
81 #if defined(CAN_COMPILE_MMXEXT)
82 static void MergeMMXEXT  ( void *, const void *, const void *, size_t );
83 #endif
84 #if defined(CAN_COMPILE_3DNOW)
85 static void Merge3DNow   ( void *, const void *, const void *, size_t );
86 #endif
87 #if defined(CAN_COMPILE_SSE)
88 static void MergeSSE2    ( void *, const void *, const void *, size_t );
89 #endif
90 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
91 static void EndMMX       ( void );
92 #endif
93 #if defined(CAN_COMPILE_3DNOW)
94 static void End3DNow     ( void );
95 #endif
96 #if defined __ARM_NEON__
97 static void MergeNEON (void *, const void *, const void *, size_t);
98 #endif
99
100 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method );
101 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
102
103 static int OpenFilter( vlc_object_t *p_this );
104 static void CloseFilter( vlc_object_t *p_this );
105
106 /*****************************************************************************
107  * Callback prototypes
108  *****************************************************************************/
109 static int FilterCallback( vlc_object_t *, char const *,
110                            vlc_value_t, vlc_value_t, void * );
111
112 /*****************************************************************************
113  * Module descriptor
114  *****************************************************************************/
115 #define MODE_TEXT N_("Deinterlace mode")
116 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
117
118 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
119 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
120
121 #define FILTER_CFG_PREFIX "sout-deinterlace-"
122
123 static const char *const mode_list[] = {
124     "discard", "blend", "mean", "bob", "linear", "x" };
125 static const char *const mode_list_text[] = {
126     N_("Discard"), N_("Blend"), N_("Mean"), N_("Bob"), N_("Linear"), "X" };
127
128 vlc_module_begin ()
129     set_description( N_("Deinterlacing video filter") )
130     set_shortname( N_("Deinterlace" ))
131     set_capability( "video filter", 0 )
132     set_category( CAT_VIDEO )
133     set_subcategory( SUBCAT_VIDEO_VFILTER )
134
135     set_section( N_("Display"),NULL)
136     add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
137                 MODE_LONGTEXT, false )
138         change_string_list( mode_list, mode_list_text, 0 )
139         change_safe ()
140
141     add_shortcut( "deinterlace" )
142     set_callbacks( Create, Destroy )
143
144     add_submodule ()
145     set_capability( "video filter2", 0 )
146     set_section( N_("Streaming"),NULL)
147     add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
148                 SOUT_MODE_LONGTEXT, false )
149         change_string_list( mode_list, mode_list_text, 0 )
150     add_shortcut( "deinterlace" )
151     set_callbacks( OpenFilter, CloseFilter )
152 vlc_module_end ()
153
154 static const char *const ppsz_filter_options[] = {
155     "mode", NULL
156 };
157
158 /*****************************************************************************
159  * vout_sys_t: Deinterlace video output method descriptor
160  *****************************************************************************
161  * This structure is part of the video output thread descriptor.
162  * It describes the Deinterlace specific properties of an output thread.
163  *****************************************************************************/
164 struct vout_sys_t
165 {
166     int        i_mode;        /* Deinterlace mode */
167     bool b_double_rate; /* Shall we double the framerate? */
168     bool b_half_height; /* Shall be devide the height by 2 */
169
170     mtime_t    last_date;
171     mtime_t    next_date;
172
173     vout_thread_t *p_vout;
174
175     vlc_mutex_t filter_lock;
176
177     void (*pf_merge) ( void *, const void *, const void *, size_t );
178     void (*pf_end_merge) ( void );
179 };
180
181 /*****************************************************************************
182  * Control: control facility for the vout (forwards to child vout)
183  *****************************************************************************/
184 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
185 {
186     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
187 }
188
189 /*****************************************************************************
190  * Create: allocates Deinterlace video thread output method
191  *****************************************************************************
192  * This function allocates and initializes a Deinterlace vout method.
193  *****************************************************************************/
194 static int Create( vlc_object_t *p_this )
195 {
196     vout_thread_t *p_vout = (vout_thread_t *)p_this;
197     vout_sys_t *p_sys;
198     char *psz_mode;
199
200     /* Allocate structure */
201     p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
202     if( p_vout->p_sys == NULL )
203         return VLC_ENOMEM;
204
205     p_vout->pf_init = Init;
206     p_vout->pf_end = End;
207     p_vout->pf_manage = NULL;
208     p_vout->pf_render = Render;
209     p_vout->pf_display = NULL;
210     p_vout->pf_control = Control;
211
212     p_sys->i_mode = DEINTERLACE_DISCARD;
213     p_sys->b_double_rate = false;
214     p_sys->b_half_height = true;
215     p_sys->last_date = 0;
216     p_sys->p_vout = 0;
217     vlc_mutex_init( &p_sys->filter_lock );
218
219 #if defined(CAN_COMPILE_C_ALTIVEC)
220     if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
221     {
222         p_sys->pf_merge = MergeAltivec;
223         p_sys->pf_end_merge = NULL;
224     }
225     else
226 #endif
227 #if defined(CAN_COMPILE_SSE)
228     if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
229     {
230         p_sys->pf_merge = MergeSSE2;
231         p_sys->pf_end_merge = EndMMX;
232     }
233     else
234 #endif
235 #if defined(CAN_COMPILE_MMXEXT)
236     if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
237     {
238         p_sys->pf_merge = MergeMMXEXT;
239         p_sys->pf_end_merge = EndMMX;
240     }
241     else
242 #endif
243 #if defined(CAN_COMPILE_3DNOW)
244     if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
245     {
246         p_sys->pf_merge = Merge3DNow;
247         p_sys->pf_end_merge = End3DNow;
248     }
249     else
250 #endif
251 #if defined __ARM_NEON__
252     if( vlc_CPU() & CPU_CAPABILITY_NEON )
253     {
254         p_sys->pf_merge = MergeNEON;
255         p_sys->pf_end_merge = NULL;
256     }
257     else
258 #endif
259     {
260         p_sys->pf_merge = MergeGeneric;
261         p_sys->pf_end_merge = NULL;
262     }
263
264     /* Look what method was requested */
265     psz_mode = var_CreateGetString( p_vout, "deinterlace-mode" );
266
267     if( !psz_mode )
268     {
269         msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
270         msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
271
272         psz_mode = strdup( "discard" );
273     }
274
275     SetFilterMethod( p_vout, psz_mode );
276
277     free( psz_mode );
278
279     return VLC_SUCCESS;
280 }
281
282 /*****************************************************************************
283  * SetFilterMethod: setup the deinterlace method to use.
284  *****************************************************************************/
285 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method )
286 {
287     vout_sys_t *p_sys = p_vout->p_sys;
288     if( !strcmp( psz_method, "mean" ) )
289     {
290         p_sys->i_mode = DEINTERLACE_MEAN;
291         p_sys->b_double_rate = false;
292         p_sys->b_half_height = true;
293     }
294     else if( !strcmp( psz_method, "blend" )
295              || !strcmp( psz_method, "average" )
296              || !strcmp( psz_method, "combine-fields" ) )
297     {
298         p_sys->i_mode = DEINTERLACE_BLEND;
299         p_sys->b_double_rate = false;
300         p_sys->b_half_height = false;
301     }
302     else if( !strcmp( psz_method, "bob" )
303              || !strcmp( psz_method, "progressive-scan" ) )
304     {
305         p_sys->i_mode = DEINTERLACE_BOB;
306         p_sys->b_double_rate = true;
307         p_sys->b_half_height = false;
308     }
309     else if( !strcmp( psz_method, "linear" ) )
310     {
311         p_sys->i_mode = DEINTERLACE_LINEAR;
312         p_sys->b_double_rate = true;
313         p_sys->b_half_height = false;
314     }
315     else if( !strcmp( psz_method, "x" ) )
316     {
317         p_sys->i_mode = DEINTERLACE_X;
318         p_sys->b_double_rate = false;
319         p_sys->b_half_height = false;
320     }
321     else
322     {
323         const bool b_i422 = p_vout->render.i_chroma == VLC_CODEC_I422;
324         if( strcmp( psz_method, "discard" ) )
325             msg_Err( p_vout, "no valid deinterlace mode provided, "
326                      "using \"discard\"" );
327
328         p_sys->i_mode = DEINTERLACE_DISCARD;
329         p_sys->b_double_rate = false;
330         p_sys->b_half_height = !b_i422;
331     }
332
333     msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
334 }
335
336 static void GetOutputFormat( vout_thread_t *p_vout,
337                              video_format_t *p_dst, const video_format_t *p_src )
338 {
339     *p_dst = *p_src;
340
341     if( p_vout->p_sys->b_half_height )
342     {
343         p_dst->i_height /= 2;
344         p_dst->i_visible_height /= 2;
345         p_dst->i_y_offset /= 2;
346         p_dst->i_sar_den *= 2;
347     }
348
349     if( p_src->i_chroma == VLC_CODEC_I422 )
350     {
351         switch( p_vout->p_sys->i_mode )
352         {
353         case DEINTERLACE_MEAN:
354         case DEINTERLACE_LINEAR:
355         case DEINTERLACE_X:
356             p_dst->i_chroma = VLC_CODEC_I422;
357             break;
358         default:
359             p_dst->i_chroma = VLC_CODEC_I420;
360             break;
361         }
362     }
363 }
364
365 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
366 {
367     return i_chroma == VLC_CODEC_I420 ||
368            i_chroma == VLC_CODEC_YV12 ||
369            i_chroma == VLC_CODEC_I422;
370 }
371
372 /*****************************************************************************
373  * Init: initialize Deinterlace video thread output method
374  *****************************************************************************/
375 static int Init( vout_thread_t *p_vout )
376 {
377     I_OUTPUTPICTURES = 0;
378
379     if( !IsChromaSupported( p_vout->render.i_chroma ) )
380         return VLC_EGENERIC; /* unknown chroma */
381
382     /* Initialize the output structure, full of directbuffers since we want
383      * the decoder to output directly to our structures. */
384     p_vout->output.i_chroma = p_vout->render.i_chroma;
385     p_vout->output.i_width  = p_vout->render.i_width;
386     p_vout->output.i_height = p_vout->render.i_height;
387     p_vout->output.i_aspect = p_vout->render.i_aspect;
388     p_vout->fmt_out = p_vout->fmt_in;
389
390     /* Try to open the real video output */
391     p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
392
393     if( p_vout->p_sys->p_vout == NULL )
394     {
395         /* Everything failed */
396         msg_Err( p_vout, "cannot open vout, aborting" );
397
398         return VLC_EGENERIC;
399     }
400
401     vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
402
403     vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
404
405     var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
406
407     return VLC_SUCCESS;
408 }
409
410 /*****************************************************************************
411  * SpawnRealVout: spawn the real video output.
412  *****************************************************************************/
413 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
414 {
415     msg_Dbg( p_vout, "spawning the real video output" );
416
417     video_format_t fmt;
418     GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
419
420     return vout_Create( p_vout, &fmt );
421 }
422
423 /*****************************************************************************
424  * End: terminate Deinterlace video thread output method
425  *****************************************************************************/
426 static void End( vout_thread_t *p_vout )
427 {
428     vout_sys_t *p_sys = p_vout->p_sys;
429
430     var_DelCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
431
432     if( p_sys->p_vout )
433     {
434         vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
435         vout_CloseAndRelease( p_sys->p_vout );
436     }
437
438     vout_filter_ReleaseDirectBuffers( p_vout );
439 }
440
441 /*****************************************************************************
442  * Destroy: destroy Deinterlace video thread output method
443  *****************************************************************************
444  * Terminate an output method created by DeinterlaceCreateOutputMethod
445  *****************************************************************************/
446 static void Destroy( vlc_object_t *p_this )
447 {
448     vout_thread_t *p_vout = (vout_thread_t *)p_this;
449     vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
450     free( p_vout->p_sys );
451 }
452
453 /**
454  * Forward mouse event with proper conversion.
455  */
456 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
457                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
458 {
459     vout_thread_t *p_vout = p_data;
460     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
461
462     if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
463         newval.i_int *= 2;
464
465     return var_Set( p_vout, psz_var, newval );
466 }
467
468 /*****************************************************************************
469  * Render: displays previously rendered output
470  *****************************************************************************
471  * This function send the currently rendered image to Deinterlace image,
472  * waits until it is displayed and switch the two rendering buffers, preparing
473  * next frame.
474  *****************************************************************************/
475 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
476 {
477     vout_sys_t *p_sys = p_vout->p_sys;
478     picture_t *pp_outpic[2];
479
480     /* FIXME are they needed ? */
481     p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
482     p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
483     p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
484     p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
485
486     /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
487     p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
488     p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
489     p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
490     p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
491     if( p_vout->p_sys->b_half_height )
492     {
493         p_sys->p_vout->fmt_in.i_y_offset /= 2;
494         p_sys->p_vout->fmt_in.i_visible_height /= 2;
495     }
496
497     if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
498     {
499         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
500
501         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
502         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
503         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
504
505         video_format_t fmt = p_vout->fmt_out;
506         if( p_vout->p_sys->b_half_height )
507         {
508             fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
509             fmt.i_sar_den *= 2;
510         }
511
512         p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
513     }
514     if( !p_sys->p_vout )
515         return;
516
517     pp_outpic[0] = pp_outpic[1] = NULL;
518
519     vlc_mutex_lock( &p_vout->p_sys->filter_lock );
520
521     /* Get a new picture */
522     while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
523                                                 0, 0, 0 ) )
524               == NULL )
525     {
526         if( !vlc_object_alive( p_vout ) || p_vout->b_error )
527         {
528             vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
529             return;
530         }
531         msleep( VOUT_OUTMEM_SLEEP );
532     }
533
534     pp_outpic[0]->date = p_pic->date;
535
536     /* If we are using double rate, get an additional new picture */
537     if( p_vout->p_sys->b_double_rate )
538     {
539         while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
540                                                  0, 0, 0 ) )
541                   == NULL )
542         {
543             if( !vlc_object_alive( p_vout ) || p_vout->b_error )
544             {
545                 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
546                 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
547                 return;
548             }
549             msleep( VOUT_OUTMEM_SLEEP );
550         }
551
552         /* 20ms is a bit arbitrary, but it's only for the first image we get */
553         if( !p_vout->p_sys->last_date )
554             pp_outpic[1]->date = p_pic->date + 20000;
555         else
556             pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
557         p_vout->p_sys->last_date = p_pic->date;
558     }
559
560     switch( p_vout->p_sys->i_mode )
561     {
562         case DEINTERLACE_DISCARD:
563             RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
564             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
565             break;
566
567         case DEINTERLACE_BOB:
568             RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
569             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
570             RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
571             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
572             break;
573
574         case DEINTERLACE_LINEAR:
575             RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
576             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
577             RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
578             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
579             break;
580
581         case DEINTERLACE_MEAN:
582             RenderMean( p_vout, pp_outpic[0], p_pic );
583             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
584             break;
585
586         case DEINTERLACE_BLEND:
587             RenderBlend( p_vout, pp_outpic[0], p_pic );
588             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
589             break;
590
591         case DEINTERLACE_X:
592             RenderX( pp_outpic[0], p_pic );
593             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
594             break;
595     }
596     vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
597 }
598
599 /*****************************************************************************
600  * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
601  *****************************************************************************/
602 static void RenderDiscard( vout_thread_t *p_vout,
603                            picture_t *p_outpic, picture_t *p_pic, int i_field )
604 {
605     int i_plane;
606
607     /* Copy image and skip lines */
608     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
609     {
610         uint8_t *p_in, *p_out_end, *p_out;
611         int i_increment;
612
613         p_in = p_pic->p[i_plane].p_pixels
614                    + i_field * p_pic->p[i_plane].i_pitch;
615
616         p_out = p_outpic->p[i_plane].p_pixels;
617         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
618                              * p_outpic->p[i_plane].i_visible_lines;
619
620         switch( p_vout->render.i_chroma )
621         {
622         case VLC_CODEC_I420:
623         case VLC_CODEC_YV12:
624
625             for( ; p_out < p_out_end ; )
626             {
627                 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
628
629                 p_out += p_outpic->p[i_plane].i_pitch;
630                 p_in += 2 * p_pic->p[i_plane].i_pitch;
631             }
632             break;
633
634         case VLC_CODEC_I422:
635
636             i_increment = 2 * p_pic->p[i_plane].i_pitch;
637
638             if( i_plane == Y_PLANE )
639             {
640                 for( ; p_out < p_out_end ; )
641                 {
642                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
643                     p_out += p_outpic->p[i_plane].i_pitch;
644                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
645                     p_out += p_outpic->p[i_plane].i_pitch;
646                     p_in += i_increment;
647                 }
648             }
649             else
650             {
651                 for( ; p_out < p_out_end ; )
652                 {
653                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
654                     p_out += p_outpic->p[i_plane].i_pitch;
655                     p_in += i_increment;
656                 }
657             }
658             break;
659
660         default:
661             break;
662         }
663     }
664 }
665
666 /*****************************************************************************
667  * RenderBob: renders a BOB picture - simple copy
668  *****************************************************************************/
669 static void RenderBob( vout_thread_t *p_vout,
670                        picture_t *p_outpic, picture_t *p_pic, int i_field )
671 {
672     int i_plane;
673
674     /* Copy image and skip lines */
675     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
676     {
677         uint8_t *p_in, *p_out_end, *p_out;
678
679         p_in = p_pic->p[i_plane].p_pixels;
680         p_out = p_outpic->p[i_plane].p_pixels;
681         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
682                              * p_outpic->p[i_plane].i_visible_lines;
683
684         switch( p_vout->render.i_chroma )
685         {
686             case VLC_CODEC_I420:
687             case VLC_CODEC_YV12:
688                 /* For BOTTOM field we need to add the first line */
689                 if( i_field == 1 )
690                 {
691                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
692                     p_in += p_pic->p[i_plane].i_pitch;
693                     p_out += p_outpic->p[i_plane].i_pitch;
694                 }
695
696                 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
697
698                 for( ; p_out < p_out_end ; )
699                 {
700                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
701
702                     p_out += p_outpic->p[i_plane].i_pitch;
703
704                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
705
706                     p_in += 2 * p_pic->p[i_plane].i_pitch;
707                     p_out += p_outpic->p[i_plane].i_pitch;
708                 }
709
710                 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
711
712                 /* For TOP field we need to add the last line */
713                 if( i_field == 0 )
714                 {
715                     p_in += p_pic->p[i_plane].i_pitch;
716                     p_out += p_outpic->p[i_plane].i_pitch;
717                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
718                 }
719                 break;
720
721             case VLC_CODEC_I422:
722                 /* For BOTTOM field we need to add the first line */
723                 if( i_field == 1 )
724                 {
725                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
726                     p_in += p_pic->p[i_plane].i_pitch;
727                     p_out += p_outpic->p[i_plane].i_pitch;
728                 }
729
730                 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
731
732                 if( i_plane == Y_PLANE )
733                 {
734                     for( ; p_out < p_out_end ; )
735                     {
736                         vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
737
738                         p_out += p_outpic->p[i_plane].i_pitch;
739
740                         vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
741
742                         p_in += 2 * p_pic->p[i_plane].i_pitch;
743                         p_out += p_outpic->p[i_plane].i_pitch;
744                     }
745                 }
746                 else
747                 {
748                     for( ; p_out < p_out_end ; )
749                     {
750                         vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
751
752                         p_out += p_outpic->p[i_plane].i_pitch;
753                         p_in += 2 * p_pic->p[i_plane].i_pitch;
754                     }
755                 }
756
757                 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
758
759                 /* For TOP field we need to add the last line */
760                 if( i_field == 0 )
761                 {
762                     p_in += p_pic->p[i_plane].i_pitch;
763                     p_out += p_outpic->p[i_plane].i_pitch;
764                     vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
765                 }
766                 break;
767         }
768     }
769 }
770
771 #define Merge p_vout->p_sys->pf_merge
772 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
773
774 /*****************************************************************************
775  * RenderLinear: BOB with linear interpolation
776  *****************************************************************************/
777 static void RenderLinear( vout_thread_t *p_vout,
778                           picture_t *p_outpic, picture_t *p_pic, int i_field )
779 {
780     int i_plane;
781
782     /* Copy image and skip lines */
783     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
784     {
785         uint8_t *p_in, *p_out_end, *p_out;
786
787         p_in = p_pic->p[i_plane].p_pixels;
788         p_out = p_outpic->p[i_plane].p_pixels;
789         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
790                              * p_outpic->p[i_plane].i_visible_lines;
791
792         /* For BOTTOM field we need to add the first line */
793         if( i_field == 1 )
794         {
795             vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
796             p_in += p_pic->p[i_plane].i_pitch;
797             p_out += p_outpic->p[i_plane].i_pitch;
798         }
799
800         p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
801
802         for( ; p_out < p_out_end ; )
803         {
804             vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
805
806             p_out += p_outpic->p[i_plane].i_pitch;
807
808             Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
809                    p_pic->p[i_plane].i_pitch );
810
811             p_in += 2 * p_pic->p[i_plane].i_pitch;
812             p_out += p_outpic->p[i_plane].i_pitch;
813         }
814
815         vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
816
817         /* For TOP field we need to add the last line */
818         if( i_field == 0 )
819         {
820             p_in += p_pic->p[i_plane].i_pitch;
821             p_out += p_outpic->p[i_plane].i_pitch;
822             vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
823         }
824     }
825     EndMerge();
826 }
827
828 static void RenderMean( vout_thread_t *p_vout,
829                         picture_t *p_outpic, picture_t *p_pic )
830 {
831     int i_plane;
832
833     /* Copy image and skip lines */
834     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
835     {
836         uint8_t *p_in, *p_out_end, *p_out;
837
838         p_in = p_pic->p[i_plane].p_pixels;
839
840         p_out = p_outpic->p[i_plane].p_pixels;
841         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
842                              * p_outpic->p[i_plane].i_visible_lines;
843
844         /* All lines: mean value */
845         for( ; p_out < p_out_end ; )
846         {
847             Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
848                    p_pic->p[i_plane].i_pitch );
849
850             p_out += p_outpic->p[i_plane].i_pitch;
851             p_in += 2 * p_pic->p[i_plane].i_pitch;
852         }
853     }
854     EndMerge();
855 }
856
857 static void RenderBlend( vout_thread_t *p_vout,
858                          picture_t *p_outpic, picture_t *p_pic )
859 {
860     int i_plane;
861
862     /* Copy image and skip lines */
863     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
864     {
865         uint8_t *p_in, *p_out_end, *p_out;
866
867         p_in = p_pic->p[i_plane].p_pixels;
868
869         p_out = p_outpic->p[i_plane].p_pixels;
870         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
871                              * p_outpic->p[i_plane].i_visible_lines;
872
873         switch( p_vout->render.i_chroma )
874         {
875             case VLC_CODEC_I420:
876             case VLC_CODEC_YV12:
877                 /* First line: simple copy */
878                 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
879                 p_out += p_outpic->p[i_plane].i_pitch;
880
881                 /* Remaining lines: mean value */
882                 for( ; p_out < p_out_end ; )
883                 {
884                     Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
885                            p_pic->p[i_plane].i_pitch );
886
887                     p_out += p_outpic->p[i_plane].i_pitch;
888                     p_in += p_pic->p[i_plane].i_pitch;
889                 }
890                 break;
891
892             case VLC_CODEC_I422:
893                 /* First line: simple copy */
894                 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
895                 p_out += p_outpic->p[i_plane].i_pitch;
896
897                 /* Remaining lines: mean value */
898                 if( i_plane == Y_PLANE )
899                 {
900                     for( ; p_out < p_out_end ; )
901                     {
902                         Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
903                                p_pic->p[i_plane].i_pitch );
904
905                         p_out += p_outpic->p[i_plane].i_pitch;
906                         p_in += p_pic->p[i_plane].i_pitch;
907                     }
908                 }
909
910                 else
911                 {
912                     for( ; p_out < p_out_end ; )
913                     {
914                         Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
915                                p_pic->p[i_plane].i_pitch );
916
917                         p_out += p_outpic->p[i_plane].i_pitch;
918                         p_in += 2*p_pic->p[i_plane].i_pitch;
919                     }
920                 }
921                 break;
922         }
923     }
924     EndMerge();
925 }
926
927 #undef Merge
928
929 static void MergeGeneric( void *_p_dest, const void *_p_s1,
930                           const void *_p_s2, size_t i_bytes )
931 {
932     uint8_t* p_dest = (uint8_t*)_p_dest;
933     const uint8_t *p_s1 = (const uint8_t *)_p_s1;
934     const uint8_t *p_s2 = (const uint8_t *)_p_s2;
935     uint8_t* p_end = p_dest + i_bytes - 8;
936
937     while( p_dest < p_end )
938     {
939         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
940         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
941         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
942         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
943         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
944         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
945         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
946         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
947     }
948
949     p_end += 8;
950
951     while( p_dest < p_end )
952     {
953         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
954     }
955 }
956
957 #if defined(CAN_COMPILE_MMXEXT)
958 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
959                          size_t i_bytes )
960 {
961     uint8_t* p_dest = (uint8_t*)_p_dest;
962     const uint8_t *p_s1 = (const uint8_t *)_p_s1;
963     const uint8_t *p_s2 = (const uint8_t *)_p_s2;
964     uint8_t* p_end = p_dest + i_bytes - 8;
965     while( p_dest < p_end )
966     {
967         __asm__  __volatile__( "movq %2,%%mm1;"
968                                "pavgb %1, %%mm1;"
969                                "movq %%mm1, %0" :"=m" (*p_dest):
970                                                  "m" (*p_s1),
971                                                  "m" (*p_s2) );
972         p_dest += 8;
973         p_s1 += 8;
974         p_s2 += 8;
975     }
976
977     p_end += 8;
978
979     while( p_dest < p_end )
980     {
981         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
982     }
983 }
984 #endif
985
986 #if defined(CAN_COMPILE_3DNOW)
987 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
988                         size_t i_bytes )
989 {
990     uint8_t* p_dest = (uint8_t*)_p_dest;
991     const uint8_t *p_s1 = (const uint8_t *)_p_s1;
992     const uint8_t *p_s2 = (const uint8_t *)_p_s2;
993     uint8_t* p_end = p_dest + i_bytes - 8;
994     while( p_dest < p_end )
995     {
996         __asm__  __volatile__( "movq %2,%%mm1;"
997                                "pavgusb %1, %%mm1;"
998                                "movq %%mm1, %0" :"=m" (*p_dest):
999                                                  "m" (*p_s1),
1000                                                  "m" (*p_s2) );
1001         p_dest += 8;
1002         p_s1 += 8;
1003         p_s2 += 8;
1004     }
1005
1006     p_end += 8;
1007
1008     while( p_dest < p_end )
1009     {
1010         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1011     }
1012 }
1013 #endif
1014
1015 #if defined(CAN_COMPILE_SSE)
1016 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1017                        size_t i_bytes )
1018 {
1019     uint8_t* p_dest = (uint8_t*)_p_dest;
1020     const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1021     const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1022     uint8_t* p_end;
1023     while( (uintptr_t)p_s1 % 16 )
1024     {
1025         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1026     }
1027     p_end = p_dest + i_bytes - 16;
1028     while( p_dest < p_end )
1029     {
1030         __asm__  __volatile__( "movdqu %2,%%xmm1;"
1031                                "pavgb %1, %%xmm1;"
1032                                "movdqu %%xmm1, %0" :"=m" (*p_dest):
1033                                                  "m" (*p_s1),
1034                                                  "m" (*p_s2) );
1035         p_dest += 16;
1036         p_s1 += 16;
1037         p_s2 += 16;
1038     }
1039
1040     p_end += 16;
1041
1042     while( p_dest < p_end )
1043     {
1044         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1045     }
1046 }
1047 #endif
1048
1049 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1050 static void EndMMX( void )
1051 {
1052     __asm__ __volatile__( "emms" :: );
1053 }
1054 #endif
1055
1056 #if defined(CAN_COMPILE_3DNOW)
1057 static void End3DNow( void )
1058 {
1059     __asm__ __volatile__( "femms" :: );
1060 }
1061 #endif
1062
1063 #ifdef CAN_COMPILE_C_ALTIVEC
1064 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1065                           const void *_p_s2, size_t i_bytes )
1066 {
1067     uint8_t *p_dest = (uint8_t *)_p_dest;
1068     uint8_t *p_s1   = (uint8_t *)_p_s1;
1069     uint8_t *p_s2   = (uint8_t *)_p_s2;
1070     uint8_t *p_end  = p_dest + i_bytes - 15;
1071
1072     /* Use C until the first 16-bytes aligned destination pixel */
1073     while( (uintptr_t)p_dest & 0xF )
1074     {
1075         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1076     }
1077
1078     if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1079     {
1080         /* Unaligned source */
1081         vector unsigned char s1v, s2v, destv;
1082         vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1083         vector unsigned char perm1v, perm2v;
1084
1085         perm1v = vec_lvsl( 0, p_s1 );
1086         perm2v = vec_lvsl( 0, p_s2 );
1087         s1oldv = vec_ld( 0, p_s1 );
1088         s2oldv = vec_ld( 0, p_s2 );
1089
1090         while( p_dest < p_end )
1091         {
1092             s1newv = vec_ld( 16, p_s1 );
1093             s2newv = vec_ld( 16, p_s2 );
1094             s1v    = vec_perm( s1oldv, s1newv, perm1v );
1095             s2v    = vec_perm( s2oldv, s2newv, perm2v );
1096             s1oldv = s1newv;
1097             s2oldv = s2newv;
1098             destv  = vec_avg( s1v, s2v );
1099             vec_st( destv, 0, p_dest );
1100
1101             p_s1   += 16;
1102             p_s2   += 16;
1103             p_dest += 16;
1104         }
1105     }
1106     else
1107     {
1108         /* Aligned source */
1109         vector unsigned char s1v, s2v, destv;
1110
1111         while( p_dest < p_end )
1112         {
1113             s1v   = vec_ld( 0, p_s1 );
1114             s2v   = vec_ld( 0, p_s2 );
1115             destv = vec_avg( s1v, s2v );
1116             vec_st( destv, 0, p_dest );
1117
1118             p_s1   += 16;
1119             p_s2   += 16;
1120             p_dest += 16;
1121         }
1122     }
1123
1124     p_end += 15;
1125
1126     while( p_dest < p_end )
1127     {
1128         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1129     }
1130 }
1131 #endif
1132
1133 #ifdef __ARM_NEON__
1134 static void MergeNEON (void *restrict out, const void *in1,
1135                        const void *in2, size_t n)
1136 {
1137     uint8_t *outp = out;
1138     const uint8_t *in1p = in1;
1139     const uint8_t *in2p = in2;
1140     size_t mis = ((uintptr_t)outp) & 15;
1141
1142     if (mis)
1143     {
1144         MergeGeneric (outp, in1p, in2p, mis);
1145         outp += mis;
1146         in1p += mis;
1147         in2p += mis;
1148         n -= mis;
1149     }
1150
1151     uint8_t *end = outp + (n & ~15);
1152
1153     if ((((uintptr_t)in1p)|((uintptr_t)in2p)) & 15)
1154         while (outp < end)
1155             asm volatile (
1156                 "vld1.u8  {q0-q1}, [%[in1]]!\n"
1157                 "vld1.u8  {q2-q3}, [%[in2]]!\n"
1158                 "vhadd.u8 q4, q0, q2\n"
1159                 "vld1.u8  {q6-q7}, [%[in1]]!\n"
1160                 "vhadd.u8 q5, q1, q3\n"
1161                 "vld1.u8  {q8-q9}, [%[in2]]!\n"
1162                 "vhadd.u8 q10, q6, q8\n"
1163                 "vhadd.u8 q11, q7, q9\n"
1164                 "vst1.u8  {q4-q5}, [%[out],:128]!\n"
1165                 "vst1.u8  {q10-q11}, [%[out],:128]!\n"
1166                 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1167                 :
1168                 : "q0", "q1", "q2", "memory");
1169     else
1170          while (outp < end)
1171             asm volatile (
1172                 "vld1.u8  {q0-q1}, [%[in1],:128]!\n"
1173                 "vld1.u8  {q2-q3}, [%[in2],:128]!\n"
1174                 "vhadd.u8 q4, q0, q2\n"
1175                 "vld1.u8  {q6-q7}, [%[in1],:128]!\n"
1176                 "vhadd.u8 q5, q1, q3\n"
1177                 "vld1.u8  {q8-q9}, [%[in2],:128]!\n"
1178                 "vhadd.u8 q10, q6, q8\n"
1179                 "vhadd.u8 q11, q7, q9\n"
1180                 "vst1.u8  {q4-q5}, [%[out],:128]!\n"
1181                 "vst1.u8  {q10-q11}, [%[out],:128]!\n"
1182                 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1183                 :
1184                 : "q0", "q1", "q2", "memory");
1185     n &= 15;
1186     if (n)
1187         MergeGeneric (outp, in1p, in2p, n);
1188 }
1189 #endif
1190
1191 /*****************************************************************************
1192  * RenderX: This algo works on a 8x8 block basic, it copies the top field
1193  * and apply a process to recreate the bottom field :
1194  *  If a 8x8 block is classified as :
1195  *   - progressive: it applies a small blend (1,6,1)
1196  *   - interlaced:
1197  *    * in the MMX version: we do a ME between the 2 fields, if there is a
1198  *    good match we use MC to recreate the bottom field (with a small
1199  *    blend (1,6,1) )
1200  *    * otherwise: it recreates the bottom field by an edge oriented
1201  *    interpolation.
1202   *****************************************************************************/
1203
1204 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1205  * XXX: It need to access to 8x10
1206  * We use more than 8 lines to help with scrolling (text)
1207  * (and because XDeint8x8Frame use line 9)
1208  * XXX: smooth/uniform area with noise detection doesn't works well
1209  * but it's not really a problem because they don't have much details anyway
1210  */
1211 static inline int ssd( int a ) { return a*a; }
1212 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1213 {
1214     int y, x;
1215     int ff, fr;
1216     int fc;
1217
1218     /* Detect interlacing */
1219     fc = 0;
1220     for( y = 0; y < 7; y += 2 )
1221     {
1222         ff = fr = 0;
1223         for( x = 0; x < 8; x++ )
1224         {
1225             fr += ssd(src[      x] - src[1*i_src+x]) +
1226                   ssd(src[i_src+x] - src[2*i_src+x]);
1227             ff += ssd(src[      x] - src[2*i_src+x]) +
1228                   ssd(src[i_src+x] - src[3*i_src+x]);
1229         }
1230         if( ff < 6*fr/8 && fr > 32 )
1231             fc++;
1232
1233         src += 2*i_src;
1234     }
1235
1236     return fc < 1 ? false : true;
1237 }
1238 #ifdef CAN_COMPILE_MMXEXT
1239 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1240 {
1241
1242     int y, x;
1243     int32_t ff, fr;
1244     int fc;
1245
1246     /* Detect interlacing */
1247     fc = 0;
1248     pxor_r2r( mm7, mm7 );
1249     for( y = 0; y < 9; y += 2 )
1250     {
1251         ff = fr = 0;
1252         pxor_r2r( mm5, mm5 );
1253         pxor_r2r( mm6, mm6 );
1254         for( x = 0; x < 8; x+=4 )
1255         {
1256             movd_m2r( src[        x], mm0 );
1257             movd_m2r( src[1*i_src+x], mm1 );
1258             movd_m2r( src[2*i_src+x], mm2 );
1259             movd_m2r( src[3*i_src+x], mm3 );
1260
1261             punpcklbw_r2r( mm7, mm0 );
1262             punpcklbw_r2r( mm7, mm1 );
1263             punpcklbw_r2r( mm7, mm2 );
1264             punpcklbw_r2r( mm7, mm3 );
1265
1266             movq_r2r( mm0, mm4 );
1267
1268             psubw_r2r( mm1, mm0 );
1269             psubw_r2r( mm2, mm4 );
1270
1271             psubw_r2r( mm1, mm2 );
1272             psubw_r2r( mm1, mm3 );
1273
1274             pmaddwd_r2r( mm0, mm0 );
1275             pmaddwd_r2r( mm4, mm4 );
1276             pmaddwd_r2r( mm2, mm2 );
1277             pmaddwd_r2r( mm3, mm3 );
1278             paddd_r2r( mm0, mm2 );
1279             paddd_r2r( mm4, mm3 );
1280             paddd_r2r( mm2, mm5 );
1281             paddd_r2r( mm3, mm6 );
1282         }
1283
1284         movq_r2r( mm5, mm0 );
1285         psrlq_i2r( 32, mm0 );
1286         paddd_r2r( mm0, mm5 );
1287         movd_r2m( mm5, fr );
1288
1289         movq_r2r( mm6, mm0 );
1290         psrlq_i2r( 32, mm0 );
1291         paddd_r2r( mm0, mm6 );
1292         movd_r2m( mm6, ff );
1293
1294         if( ff < 6*fr/8 && fr > 32 )
1295             fc++;
1296
1297         src += 2*i_src;
1298     }
1299     return fc;
1300 }
1301 #endif
1302
1303 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1304                                     uint8_t *src1, int i_src1,
1305                                     uint8_t *src2, int i_src2 )
1306 {
1307     int y, x;
1308
1309     /* Progressive */
1310     for( y = 0; y < 8; y += 2 )
1311     {
1312         memcpy( dst, src1, 8 );
1313         dst  += i_dst;
1314
1315         for( x = 0; x < 8; x++ )
1316             dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1317         dst += i_dst;
1318
1319         src1 += i_src1;
1320         src2 += i_src2;
1321     }
1322 }
1323
1324 #ifdef CAN_COMPILE_MMXEXT
1325 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1326                                          uint8_t *src1, int i_src1,
1327                                          uint8_t *src2, int i_src2 )
1328 {
1329     static const uint64_t m_4 = INT64_C(0x0004000400040004);
1330     int y, x;
1331
1332     /* Progressive */
1333     pxor_r2r( mm7, mm7 );
1334     for( y = 0; y < 8; y += 2 )
1335     {
1336         for( x = 0; x < 8; x +=4 )
1337         {
1338             movd_m2r( src1[x], mm0 );
1339             movd_r2m( mm0, dst[x] );
1340
1341             movd_m2r( src2[x], mm1 );
1342             movd_m2r( src1[i_src1+x], mm2 );
1343
1344             punpcklbw_r2r( mm7, mm0 );
1345             punpcklbw_r2r( mm7, mm1 );
1346             punpcklbw_r2r( mm7, mm2 );
1347             paddw_r2r( mm1, mm1 );
1348             movq_r2r( mm1, mm3 );
1349             paddw_r2r( mm3, mm3 );
1350             paddw_r2r( mm2, mm0 );
1351             paddw_r2r( mm3, mm1 );
1352             paddw_m2r( m_4, mm1 );
1353             paddw_r2r( mm1, mm0 );
1354             psraw_i2r( 3, mm0 );
1355             packuswb_r2r( mm7, mm0 );
1356             movd_r2m( mm0, dst[i_dst+x] );
1357         }
1358         dst += 2*i_dst;
1359         src1 += i_src1;
1360         src2 += i_src2;
1361     }
1362 }
1363
1364 #endif
1365
1366 /* For debug */
1367 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1368 {
1369     int y;
1370     for( y = 0; y < 8; y++ )
1371         memset( &dst[y*i_dst], v, 8 );
1372 }
1373
1374 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1375  * neighbour
1376  * (Use 8x9 pixels)
1377  * TODO: a better one for the inner part.
1378  */
1379 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1380                                      uint8_t *src, int i_src )
1381 {
1382     int y, x;
1383
1384     /* Interlaced */
1385     for( y = 0; y < 8; y += 2 )
1386     {
1387         memcpy( dst, src, 8 );
1388         dst += i_dst;
1389
1390         for( x = 0; x < 8; x++ )
1391             dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1392         dst += 1*i_dst;
1393         src += 2*i_src;
1394     }
1395 }
1396 #ifdef CAN_COMPILE_MMXEXT
1397 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1398                                           uint8_t *src, int i_src )
1399 {
1400     int y;
1401
1402     /* Interlaced */
1403     for( y = 0; y < 8; y += 2 )
1404     {
1405         movq_m2r( src[0], mm0 );
1406         movq_r2m( mm0, dst[0] );
1407         dst += i_dst;
1408
1409         movq_m2r( src[2*i_src], mm1 );
1410         pavgb_r2r( mm1, mm0 );
1411
1412         movq_r2m( mm0, dst[0] );
1413
1414         dst += 1*i_dst;
1415         src += 2*i_src;
1416     }
1417 }
1418 #endif
1419
1420 /* XDeint8x8Field: Edge oriented interpolation
1421  * (Need -4 and +5 pixels H, +1 line)
1422  */
1423 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1424                                     uint8_t *src, int i_src )
1425 {
1426     int y, x;
1427
1428     /* Interlaced */
1429     for( y = 0; y < 8; y += 2 )
1430     {
1431         memcpy( dst, src, 8 );
1432         dst += i_dst;
1433
1434         for( x = 0; x < 8; x++ )
1435         {
1436             uint8_t *src2 = &src[2*i_src];
1437             /* I use 8 pixels just to match the MMX version, but it's overkill
1438              * 5 would be enough (less isn't good) */
1439             const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1440                            abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1441                            abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1442                            abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1443
1444             const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1445                            abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1446                            abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1447                            abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1448
1449             const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1450                            abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1451                            abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1452                            abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1453
1454             if( c0 < c1 && c1 <= c2 )
1455                 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1456             else if( c2 < c1 && c1 <= c0 )
1457                 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1458             else
1459                 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1460         }
1461
1462         dst += 1*i_dst;
1463         src += 2*i_src;
1464     }
1465 }
1466 #ifdef CAN_COMPILE_MMXEXT
1467 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1468                                          uint8_t *src, int i_src )
1469 {
1470     int y, x;
1471
1472     /* Interlaced */
1473     for( y = 0; y < 8; y += 2 )
1474     {
1475         memcpy( dst, src, 8 );
1476         dst += i_dst;
1477
1478         for( x = 0; x < 8; x++ )
1479         {
1480             uint8_t *src2 = &src[2*i_src];
1481             int32_t c0, c1, c2;
1482
1483             movq_m2r( src[x-2], mm0 );
1484             movq_m2r( src[x-3], mm1 );
1485             movq_m2r( src[x-4], mm2 );
1486
1487             psadbw_m2r( src2[x-4], mm0 );
1488             psadbw_m2r( src2[x-3], mm1 );
1489             psadbw_m2r( src2[x-2], mm2 );
1490
1491             movd_r2m( mm0, c2 );
1492             movd_r2m( mm1, c1 );
1493             movd_r2m( mm2, c0 );
1494
1495             if( c0 < c1 && c1 <= c2 )
1496                 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1497             else if( c2 < c1 && c1 <= c0 )
1498                 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1499             else
1500                 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1501         }
1502
1503         dst += 1*i_dst;
1504         src += 2*i_src;
1505     }
1506 }
1507 #endif
1508
1509 /* NxN arbitray size (and then only use pixel in the NxN block)
1510  */
1511 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1512                                    int i_height, int i_width )
1513 {
1514     int y, x;
1515     int ff, fr;
1516     int fc;
1517
1518
1519     /* Detect interlacing */
1520     /* FIXME way too simple, need to be more like XDeint8x8Detect */
1521     ff = fr = 0;
1522     fc = 0;
1523     for( y = 0; y < i_height - 2; y += 2 )
1524     {
1525         const uint8_t *s = &src[y*i_src];
1526         for( x = 0; x < i_width; x++ )
1527         {
1528             fr += ssd(s[      x] - s[1*i_src+x]);
1529             ff += ssd(s[      x] - s[2*i_src+x]);
1530         }
1531         if( ff < fr && fr > i_width / 2 )
1532             fc++;
1533     }
1534
1535     return fc < 2 ? false : true;
1536 }
1537
1538 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1539                                    uint8_t *src, int i_src,
1540                                    int i_width, int i_height )
1541 {
1542     int y, x;
1543
1544     /* Progressive */
1545     for( y = 0; y < i_height; y += 2 )
1546     {
1547         memcpy( dst, src, i_width );
1548         dst += i_dst;
1549
1550         if( y < i_height - 2 )
1551         {
1552             for( x = 0; x < i_width; x++ )
1553                 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1554         }
1555         else
1556         {
1557             /* Blend last line */
1558             for( x = 0; x < i_width; x++ )
1559                 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1560         }
1561         dst += 1*i_dst;
1562         src += 2*i_src;
1563     }
1564 }
1565
1566 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1567                                    uint8_t *src, int i_src,
1568                                    int i_width, int i_height )
1569 {
1570     int y, x;
1571
1572     /* Interlaced */
1573     for( y = 0; y < i_height; y += 2 )
1574     {
1575         memcpy( dst, src, i_width );
1576         dst += i_dst;
1577
1578         if( y < i_height - 2 )
1579         {
1580             for( x = 0; x < i_width; x++ )
1581                 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1582         }
1583         else
1584         {
1585             /* Blend last line */
1586             for( x = 0; x < i_width; x++ )
1587                 dst[x] = (src[x] + src[i_src+x]) >> 1;
1588         }
1589         dst += 1*i_dst;
1590         src += 2*i_src;
1591     }
1592 }
1593
1594 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1595                               int i_width, int i_height )
1596 {
1597     if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1598         XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1599     else
1600         XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1601 }
1602
1603
1604 static inline int median( int a, int b, int c )
1605 {
1606     int min = a, max =a;
1607     if( b < min )
1608         min = b;
1609     else
1610         max = b;
1611
1612     if( c < min )
1613         min = c;
1614     else if( c > max )
1615         max = c;
1616
1617     return a + b + c - min - max;
1618 }
1619
1620
1621 /* XDeintBand8x8:
1622  */
1623 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1624                                    uint8_t *src, int i_src,
1625                                    const int i_mbx, int i_modx )
1626 {
1627     int x;
1628
1629     for( x = 0; x < i_mbx; x++ )
1630     {
1631         int s;
1632         if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1633         {
1634             if( x == 0 || x == i_mbx - 1 )
1635                 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1636             else
1637                 XDeint8x8FieldC( dst, i_dst, src, i_src );
1638         }
1639         else
1640         {
1641             XDeint8x8MergeC( dst, i_dst,
1642                              &src[0*i_src], 2*i_src,
1643                              &src[1*i_src], 2*i_src );
1644         }
1645
1646         dst += 8;
1647         src += 8;
1648     }
1649
1650     if( i_modx )
1651         XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1652 }
1653 #ifdef CAN_COMPILE_MMXEXT
1654 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1655                                         uint8_t *src, int i_src,
1656                                         const int i_mbx, int i_modx )
1657 {
1658     int x;
1659
1660     /* Reset current line */
1661     for( x = 0; x < i_mbx; x++ )
1662     {
1663         int s;
1664         if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1665         {
1666             if( x == 0 || x == i_mbx - 1 )
1667                 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1668             else
1669                 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1670         }
1671         else
1672         {
1673             XDeint8x8MergeMMXEXT( dst, i_dst,
1674                                   &src[0*i_src], 2*i_src,
1675                                   &src[1*i_src], 2*i_src );
1676         }
1677
1678         dst += 8;
1679         src += 8;
1680     }
1681
1682     if( i_modx )
1683         XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1684 }
1685 #endif
1686
1687 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1688 {
1689     int i_plane;
1690
1691     /* Copy image and skip lines */
1692     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1693     {
1694         const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1695         const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1696
1697         const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1698         const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1699
1700         const int i_dst = p_outpic->p[i_plane].i_pitch;
1701         const int i_src = p_pic->p[i_plane].i_pitch;
1702
1703         int y, x;
1704
1705         for( y = 0; y < i_mby; y++ )
1706         {
1707             uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1708             uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1709
1710 #ifdef CAN_COMPILE_MMXEXT
1711             if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1712                 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1713             else
1714 #endif
1715                 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1716         }
1717
1718         /* Last line (C only)*/
1719         if( i_mody )
1720         {
1721             uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1722             uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1723
1724             for( x = 0; x < i_mbx; x++ )
1725             {
1726                 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1727
1728                 dst += 8;
1729                 src += 8;
1730             }
1731
1732             if( i_modx )
1733                 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1734         }
1735     }
1736
1737 #ifdef CAN_COMPILE_MMXEXT
1738     if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1739         emms();
1740 #endif
1741 }
1742
1743 /*****************************************************************************
1744  * FilterCallback: called when changing the deinterlace method on the fly.
1745  *****************************************************************************/
1746 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1747                            vlc_value_t oldval, vlc_value_t newval,
1748                            void *p_data )
1749 {
1750     VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1751     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1752     vout_sys_t *p_sys = p_vout->p_sys;
1753
1754     msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1755
1756     vlc_mutex_lock( &p_sys->filter_lock );
1757     const bool b_old_half_height = p_sys->b_half_height;
1758
1759     SetFilterMethod( p_vout, newval.psz_string );
1760
1761     if( !b_old_half_height == !p_sys->b_half_height )
1762     {
1763         vlc_mutex_unlock( &p_sys->filter_lock );
1764         return VLC_SUCCESS;
1765     }
1766
1767     /* We need to kill the old vout */
1768     if( p_sys->p_vout )
1769     {
1770         vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
1771         vout_CloseAndRelease( p_sys->p_vout );
1772     }
1773
1774     /* Try to open a new video output */
1775     p_sys->p_vout = SpawnRealVout( p_vout );
1776
1777     if( p_sys->p_vout == NULL )
1778     {
1779         /* Everything failed */
1780         msg_Err( p_vout, "cannot open vout, aborting" );
1781
1782         vlc_mutex_unlock( &p_sys->filter_lock );
1783         return VLC_EGENERIC;
1784     }
1785
1786     vout_filter_AddChild( p_vout, p_sys->p_vout, MouseEvent );
1787
1788     vlc_mutex_unlock( &p_sys->filter_lock );
1789     return VLC_SUCCESS;
1790 }
1791
1792 /*****************************************************************************
1793  * video filter2 functions
1794  *****************************************************************************/
1795 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1796 {
1797     vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1798     picture_t *p_pic_dst;
1799
1800     /* Request output picture */
1801     p_pic_dst = filter_NewPicture( p_filter );
1802     if( p_pic_dst == NULL )
1803     {
1804         picture_Release( p_pic );
1805         return NULL;
1806     }
1807
1808     switch( p_vout->p_sys->i_mode )
1809     {
1810         case DEINTERLACE_DISCARD:
1811             RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1812             break;
1813
1814         case DEINTERLACE_BOB:
1815 #if 0
1816             RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1817             RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1818             break;
1819 #endif
1820
1821         case DEINTERLACE_LINEAR:
1822 #if 0
1823             RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1824             RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1825 #endif
1826             msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1827             picture_Release( p_pic_dst );
1828             picture_Release( p_pic );
1829             return NULL;
1830
1831         case DEINTERLACE_MEAN:
1832             RenderMean( p_vout, p_pic_dst, p_pic );
1833             break;
1834
1835         case DEINTERLACE_BLEND:
1836             RenderBlend( p_vout, p_pic_dst, p_pic );
1837             break;
1838
1839         case DEINTERLACE_X:
1840             RenderX( p_pic_dst, p_pic );
1841             break;
1842     }
1843
1844     picture_CopyProperties( p_pic_dst, p_pic );
1845     p_pic_dst->b_progressive = true;
1846
1847     picture_Release( p_pic );
1848     return p_pic_dst;
1849 }
1850
1851 /*****************************************************************************
1852  * OpenFilter:
1853  *****************************************************************************/
1854 static int OpenFilter( vlc_object_t *p_this )
1855 {
1856     filter_t *p_filter = (filter_t*)p_this;
1857     vout_thread_t *p_vout;
1858     vlc_value_t val;
1859
1860     if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
1861         return VLC_EGENERIC;
1862
1863     /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1864      * by spu filters */
1865     p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1866     vlc_object_attach( p_vout, p_filter );
1867     p_filter->p_sys = (filter_sys_t *)p_vout;
1868     p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1869
1870     config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1871                    p_filter->p_cfg );
1872     var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1873
1874     var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1875     var_Set( p_filter, "deinterlace-mode", val );
1876     free( val.psz_string );
1877
1878     if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1879     {
1880         vlc_object_detach( p_vout );
1881         vlc_object_release( p_vout );
1882         return VLC_EGENERIC;
1883     }
1884
1885     video_format_t fmt;
1886     GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
1887     if( !p_filter->b_allow_fmt_out_change &&
1888         ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
1889           fmt.i_height != p_filter->fmt_in.video.i_height ) )
1890     {
1891         CloseFilter( VLC_OBJECT(p_filter) );
1892         return VLC_EGENERIC;
1893     }
1894     p_filter->fmt_out.video = fmt;
1895     p_filter->fmt_out.i_codec = fmt.i_chroma;
1896     p_filter->pf_video_filter = Deinterlace;
1897
1898     msg_Dbg( p_filter, "deinterlacing" );
1899
1900     return VLC_SUCCESS;
1901 }
1902
1903 /*****************************************************************************
1904  * CloseFilter: clean up the filter
1905  *****************************************************************************/
1906 static void CloseFilter( vlc_object_t *p_this )
1907 {
1908     filter_t *p_filter = (filter_t*)p_this;
1909     vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1910
1911     Destroy( VLC_OBJECT(p_vout) );
1912     vlc_object_detach( p_vout );
1913     vlc_object_release( p_vout );
1914 }
1915