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