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