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