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