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