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