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