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