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