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