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