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