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