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