]> git.sesse.net Git - vlc/blob - modules/video_filter/deinterlace.c
* ALL: use i_visible_lines in plane_t.
[vlc] / modules / video_filter / deinterlace.c
1 /*****************************************************************************
2  * deinterlace.c : deinterlacer plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id$
6  *
7  * Author: Sam Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33
34 #ifdef HAVE_ALTIVEC_H
35 #   include <altivec.h>
36 #endif
37
38 #include "filter_common.h"
39
40 #define DEINTERLACE_DISCARD 1
41 #define DEINTERLACE_MEAN    2
42 #define DEINTERLACE_BLEND   3
43 #define DEINTERLACE_BOB     4
44 #define DEINTERLACE_LINEAR  5
45
46 /*****************************************************************************
47  * Local protypes
48  *****************************************************************************/
49 static int  Create    ( vlc_object_t * );
50 static void Destroy   ( vlc_object_t * );
51
52 static int  Init      ( vout_thread_t * );
53 static void End       ( vout_thread_t * );
54 static void Render    ( vout_thread_t *, picture_t * );
55
56 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
57 static void RenderBob    ( vout_thread_t *, picture_t *, picture_t *, int );
58 static void RenderMean   ( vout_thread_t *, picture_t *, picture_t * );
59 static void RenderBlend  ( vout_thread_t *, picture_t *, picture_t * );
60 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
61
62 static void MergeGeneric ( void *, const void *, const void *, size_t );
63 #if defined(CAN_COMPILE_C_ALTIVEC)
64 static void MergeAltivec ( void *, const void *, const void *, size_t );
65 #endif
66 #if defined(CAN_COMPILE_MMXEXT)
67 static void MergeMMX     ( void *, const void *, const void *, size_t );
68 #endif
69 #if defined(CAN_COMPILE_SSE)
70 static void MergeSSE2    ( void *, const void *, const void *, size_t );
71 #endif
72 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
73 static void EndMMX       ( void );
74 #endif
75
76 static int  SendEvents   ( vlc_object_t *, char const *,
77                            vlc_value_t, vlc_value_t, void * );
78
79 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
80 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
81
82 /*****************************************************************************
83  * Callback prototypes
84  *****************************************************************************/
85 static int FilterCallback ( vlc_object_t *, char const *,
86                             vlc_value_t, vlc_value_t, void * );
87
88 /*****************************************************************************
89  * Module descriptor
90  *****************************************************************************/
91 #define MODE_TEXT N_("Deinterlace mode")
92 #define MODE_LONGTEXT N_("You can choose the default deinterlace mode")
93
94 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear" };
95 static char *mode_list_text[] = { N_("Discard"), N_("Blend"), N_("Mean"),
96                                   N_("Bob"), N_("Linear") };
97
98 vlc_module_begin();
99     set_description( _("Deinterlacing video filter") );
100     set_capability( "video filter", 0 );
101
102     add_string( "deinterlace-mode", "discard", NULL, MODE_TEXT,
103                 MODE_LONGTEXT, VLC_FALSE );
104         change_string_list( mode_list, mode_list_text, 0 );
105
106     add_shortcut( "deinterlace" );
107     set_callbacks( Create, Destroy );
108 vlc_module_end();
109
110 /*****************************************************************************
111  * vout_sys_t: Deinterlace video output method descriptor
112  *****************************************************************************
113  * This structure is part of the video output thread descriptor.
114  * It describes the Deinterlace specific properties of an output thread.
115  *****************************************************************************/
116 struct vout_sys_t
117 {
118     int        i_mode;        /* Deinterlace mode */
119     vlc_bool_t b_double_rate; /* Shall we double the framerate? */
120
121     mtime_t    last_date;
122     mtime_t    next_date;
123
124     vout_thread_t *p_vout;
125
126     vlc_mutex_t filter_lock;
127
128     void (*pf_merge) ( void *, const void *, const void *, size_t );
129     void (*pf_end_merge) ( void );
130 };
131
132 /*****************************************************************************
133  * Control: control facility for the vout (forwards to child vout)
134  *****************************************************************************/
135 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
136 {
137     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
138 }
139
140 /*****************************************************************************
141  * Create: allocates Deinterlace video thread output method
142  *****************************************************************************
143  * This function allocates and initializes a Deinterlace vout method.
144  *****************************************************************************/
145 static int Create( vlc_object_t *p_this )
146 {
147     vout_thread_t *p_vout = (vout_thread_t *)p_this;
148     vlc_value_t val;
149
150     /* Allocate structure */
151     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
152     if( p_vout->p_sys == NULL )
153     {
154         msg_Err( p_vout, "out of memory" );
155         return VLC_ENOMEM;
156     }
157
158     p_vout->pf_init = Init;
159     p_vout->pf_end = End;
160     p_vout->pf_manage = NULL;
161     p_vout->pf_render = Render;
162     p_vout->pf_display = NULL;
163     p_vout->pf_control = Control;
164
165     p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
166     p_vout->p_sys->b_double_rate = 0;
167     p_vout->p_sys->last_date = 0;
168     p_vout->p_sys->p_vout = 0;
169     vlc_mutex_init( p_vout, &p_vout->p_sys->filter_lock );
170
171 #if defined(CAN_COMPILE_C_ALTIVEC)
172     if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
173     {
174         p_vout->p_sys->pf_merge = MergeAltivec;
175         p_vout->p_sys->pf_end_merge = NULL;
176     }
177     else
178 #endif
179 #if defined(CAN_COMPILE_SSE)
180     if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2 )
181     {
182         p_vout->p_sys->pf_merge = MergeSSE2;
183         p_vout->p_sys->pf_end_merge = EndMMX;
184     }
185     else
186 #endif
187 #if defined(CAN_COMPILE_MMXEXT)
188     if( p_vout->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
189     {
190         p_vout->p_sys->pf_merge = MergeMMX;
191         p_vout->p_sys->pf_end_merge = EndMMX;
192     }
193     else
194 #endif
195     {
196         p_vout->p_sys->pf_merge = MergeGeneric;
197         p_vout->p_sys->pf_end_merge = NULL;
198     }
199
200     /* Look what method was requested */
201     var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
202     var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
203
204     if( val.psz_string == NULL )
205     {
206         msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
207         msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
208
209         val.psz_string = strdup( "discard" );
210     }
211
212     msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
213
214     SetFilterMethod( p_vout, val.psz_string );
215
216     free( val.psz_string );
217
218     var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
219
220     return VLC_SUCCESS;
221 }
222
223 /*****************************************************************************
224  * SetFilterMethod: setup the deinterlace method to use.
225  *****************************************************************************/
226 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
227 {
228     if( !strcmp( psz_method, "discard" ) )
229     {
230         p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
231         p_vout->p_sys->b_double_rate = 0;
232     }
233     else if( !strcmp( psz_method, "mean" ) )
234     {
235         p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
236         p_vout->p_sys->b_double_rate = 0;
237     }
238     else if( !strcmp( psz_method, "blend" )
239              || !strcmp( psz_method, "average" )
240              || !strcmp( psz_method, "combine-fields" ) )
241     {
242         p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
243         p_vout->p_sys->b_double_rate = 0;
244     }
245     else if( !strcmp( psz_method, "bob" )
246              || !strcmp( psz_method, "progressive-scan" ) )
247     {
248         p_vout->p_sys->i_mode = DEINTERLACE_BOB;
249         p_vout->p_sys->b_double_rate = 1;
250     }
251     else if( !strcmp( psz_method, "linear" ) )
252     {
253         p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
254         p_vout->p_sys->b_double_rate = 1;
255     }
256     else
257     {
258         msg_Err( p_vout, "no valid deinterlace mode provided, "
259                  "using \"discard\"" );
260     }
261
262     msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
263 }
264
265 /*****************************************************************************
266  * Init: initialize Deinterlace video thread output method
267  *****************************************************************************/
268 static int Init( vout_thread_t *p_vout )
269 {
270     int i_index;
271     picture_t *p_pic;
272
273     I_OUTPUTPICTURES = 0;
274
275     /* Initialize the output structure, full of directbuffers since we want
276      * the decoder to output directly to our structures. */
277     switch( p_vout->render.i_chroma )
278     {
279         case VLC_FOURCC('I','4','2','0'):
280         case VLC_FOURCC('I','Y','U','V'):
281         case VLC_FOURCC('Y','V','1','2'):
282         case VLC_FOURCC('I','4','2','2'):
283             p_vout->output.i_chroma = p_vout->render.i_chroma;
284             p_vout->output.i_width  = p_vout->render.i_width;
285             p_vout->output.i_height = p_vout->render.i_height;
286             p_vout->output.i_aspect = p_vout->render.i_aspect;
287             break;
288
289         default:
290             return VLC_EGENERIC; /* unknown chroma */
291             break;
292     }
293
294     /* Try to open the real video output */
295     p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
296
297     if( p_vout->p_sys->p_vout == NULL )
298     {
299         /* Everything failed */
300         msg_Err( p_vout, "cannot open vout, aborting" );
301
302         return VLC_EGENERIC;
303     }
304
305     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
306
307     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
308
309     ADD_PARENT_CALLBACKS( SendEventsToChild );
310
311     return VLC_SUCCESS;
312 }
313
314 /*****************************************************************************
315  * SpawnRealVout: spawn the real video output.
316  *****************************************************************************/
317 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
318 {
319     vout_thread_t *p_real_vout = NULL;
320
321     msg_Dbg( p_vout, "spawning the real video output" );
322
323     switch( p_vout->render.i_chroma )
324     {
325     case VLC_FOURCC('I','4','2','0'):
326     case VLC_FOURCC('I','Y','U','V'):
327     case VLC_FOURCC('Y','V','1','2'):
328         switch( p_vout->p_sys->i_mode )
329         {
330         case DEINTERLACE_MEAN:
331         case DEINTERLACE_DISCARD:
332             p_real_vout =
333                 vout_Create( p_vout,
334                        p_vout->output.i_width, p_vout->output.i_height / 2,
335                        p_vout->output.i_chroma, p_vout->output.i_aspect );
336             break;
337
338         case DEINTERLACE_BOB:
339         case DEINTERLACE_BLEND:
340         case DEINTERLACE_LINEAR:
341             p_real_vout =
342                 vout_Create( p_vout,
343                        p_vout->output.i_width, p_vout->output.i_height,
344                        p_vout->output.i_chroma, p_vout->output.i_aspect );
345             break;
346         }
347         break;
348
349     case VLC_FOURCC('I','4','2','2'):
350         p_real_vout =
351             vout_Create( p_vout,
352                        p_vout->output.i_width, p_vout->output.i_height,
353                        VLC_FOURCC('I','4','2','0'), p_vout->output.i_aspect );
354         break;
355
356     default:
357         break;
358     }
359
360     return p_real_vout;
361 }
362
363 /*****************************************************************************
364  * End: terminate Deinterlace video thread output method
365  *****************************************************************************/
366 static void End( vout_thread_t *p_vout )
367 {
368     int i_index;
369
370     /* Free the fake output buffers we allocated */
371     for( i_index = I_OUTPUTPICTURES ; i_index ; )
372     {
373         i_index--;
374         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
375     }
376 }
377
378 /*****************************************************************************
379  * Destroy: destroy Deinterlace video thread output method
380  *****************************************************************************
381  * Terminate an output method created by DeinterlaceCreateOutputMethod
382  *****************************************************************************/
383 static void Destroy( vlc_object_t *p_this )
384 {
385     vout_thread_t *p_vout = (vout_thread_t *)p_this;
386
387     if( p_vout->p_sys->p_vout )
388     {
389         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
390         vlc_object_detach( p_vout->p_sys->p_vout );
391         vout_Destroy( p_vout->p_sys->p_vout );
392     }
393
394     DEL_PARENT_CALLBACKS( SendEventsToChild );
395
396     free( p_vout->p_sys );
397 }
398
399 /*****************************************************************************
400  * Render: displays previously rendered output
401  *****************************************************************************
402  * This function send the currently rendered image to Deinterlace image,
403  * waits until it is displayed and switch the two rendering buffers, preparing
404  * next frame.
405  *****************************************************************************/
406 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
407 {
408     picture_t *pp_outpic[2];
409
410     vlc_mutex_lock( &p_vout->p_sys->filter_lock );
411
412     /* Get a new picture */
413     while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
414                                              0, 0, 0 ) )
415               == NULL )
416     {
417         if( p_vout->b_die || p_vout->b_error )
418         {
419             vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
420             return;
421         }
422         msleep( VOUT_OUTMEM_SLEEP );
423      }
424
425     vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
426
427     /* If we are using double rate, get an additional new picture */
428     if( p_vout->p_sys->b_double_rate )
429     {
430         while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
431                                                  0, 0, 0 ) )
432                   == NULL )
433         {
434             if( p_vout->b_die || p_vout->b_error )
435             {
436                 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
437                 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
438                 return;
439             }
440             msleep( VOUT_OUTMEM_SLEEP );
441         }
442
443         /* 20ms is a bit arbitrary, but it's only for the first image we get */
444         if( !p_vout->p_sys->last_date )
445         {
446             vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
447                               p_pic->date + 20000 );
448         }
449         else
450         {
451             vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
452                       (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
453         }
454         p_vout->p_sys->last_date = p_pic->date;
455     }
456
457     switch( p_vout->p_sys->i_mode )
458     {
459         case DEINTERLACE_DISCARD:
460             RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
461             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
462             break;
463
464         case DEINTERLACE_BOB:
465             RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
466             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
467             RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
468             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
469             break;
470
471         case DEINTERLACE_LINEAR:
472             RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
473             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
474             RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
475             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
476             break;
477
478         case DEINTERLACE_MEAN:
479             RenderMean( p_vout, pp_outpic[0], p_pic );
480             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
481             break;
482
483         case DEINTERLACE_BLEND:
484             RenderBlend( p_vout, pp_outpic[0], p_pic );
485             vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
486             break;
487     }
488
489     vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
490 }
491
492 /*****************************************************************************
493  * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
494  *****************************************************************************/
495 static void RenderDiscard( vout_thread_t *p_vout,
496                            picture_t *p_outpic, picture_t *p_pic, int i_field )
497 {
498     int i_plane;
499
500     /* Copy image and skip lines */
501     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
502     {
503         uint8_t *p_in, *p_out_end, *p_out;
504         int i_increment;
505
506         p_in = p_pic->p[i_plane].p_pixels
507                    + i_field * p_pic->p[i_plane].i_pitch;
508
509         p_out = p_outpic->p[i_plane].p_pixels;
510         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
511                              * p_outpic->p[i_plane].i_visible_lines;
512
513         switch( p_vout->render.i_chroma )
514         {
515         case VLC_FOURCC('I','4','2','0'):
516         case VLC_FOURCC('I','Y','U','V'):
517         case VLC_FOURCC('Y','V','1','2'):
518
519             for( ; p_out < p_out_end ; )
520             {
521                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
522                                           p_pic->p[i_plane].i_pitch );
523
524                 p_out += p_pic->p[i_plane].i_pitch;
525                 p_in += 2 * p_pic->p[i_plane].i_pitch;
526             }
527             break;
528
529         case VLC_FOURCC('I','4','2','2'):
530
531             i_increment = 2 * p_pic->p[i_plane].i_pitch;
532
533             if( i_plane == Y_PLANE )
534             {
535                 for( ; p_out < p_out_end ; )
536                 {
537                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
538                                               p_pic->p[i_plane].i_pitch );
539                     p_out += p_pic->p[i_plane].i_pitch;
540                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
541                                               p_pic->p[i_plane].i_pitch );
542                     p_out += p_pic->p[i_plane].i_pitch;
543                     p_in += i_increment;
544                 }
545             }
546             else
547             {
548                 for( ; p_out < p_out_end ; )
549                 {
550                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
551                                               p_pic->p[i_plane].i_pitch );
552                     p_out += p_pic->p[i_plane].i_pitch;
553                     p_in += i_increment;
554                 }
555             }
556             break;
557
558         default:
559             break;
560         }
561     }
562 }
563
564 /*****************************************************************************
565  * RenderBob: renders a BOB picture - simple copy
566  *****************************************************************************/
567 static void RenderBob( vout_thread_t *p_vout,
568                        picture_t *p_outpic, picture_t *p_pic, int i_field )
569 {
570     int i_plane;
571
572     /* Copy image and skip lines */
573     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
574     {
575         uint8_t *p_in, *p_out_end, *p_out;
576
577         p_in = p_pic->p[i_plane].p_pixels;
578         p_out = p_outpic->p[i_plane].p_pixels;
579         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
580                              * p_outpic->p[i_plane].i_visible_lines;
581
582         switch( p_vout->render.i_chroma )
583         {
584             case VLC_FOURCC('I','4','2','0'):
585             case VLC_FOURCC('I','Y','U','V'):
586             case VLC_FOURCC('Y','V','1','2'):
587                 /* For BOTTOM field we need to add the first line */
588                 if( i_field == 1 )
589                 {
590                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
591                                               p_pic->p[i_plane].i_pitch );
592                     p_in += p_pic->p[i_plane].i_pitch;
593                     p_out += p_pic->p[i_plane].i_pitch;
594                 }
595
596                 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
597
598                 for( ; p_out < p_out_end ; )
599                 {
600                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
601                                               p_pic->p[i_plane].i_pitch );
602
603                     p_out += p_pic->p[i_plane].i_pitch;
604
605                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
606                                               p_pic->p[i_plane].i_pitch );
607
608                     p_in += 2 * p_pic->p[i_plane].i_pitch;
609                     p_out += p_pic->p[i_plane].i_pitch;
610                 }
611
612                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
613                                           p_pic->p[i_plane].i_pitch );
614
615                 /* For TOP field we need to add the last line */
616                 if( i_field == 0 )
617                 {
618                     p_in += p_pic->p[i_plane].i_pitch;
619                     p_out += p_pic->p[i_plane].i_pitch;
620                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
621                                               p_pic->p[i_plane].i_pitch );
622                 }
623                 break;
624
625             case VLC_FOURCC('I','4','2','2'):
626                 /* For BOTTOM field we need to add the first line */
627                 if( i_field == 1 )
628                 {
629                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
630                                               p_pic->p[i_plane].i_pitch );
631                     p_in += p_pic->p[i_plane].i_pitch;
632                     p_out += p_pic->p[i_plane].i_pitch;
633                 }
634
635                 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
636
637                 if( i_plane == Y_PLANE )
638                 {
639                     for( ; p_out < p_out_end ; )
640                     {
641                         p_vout->p_vlc->pf_memcpy( p_out, p_in,
642                                                   p_pic->p[i_plane].i_pitch );
643
644                         p_out += p_pic->p[i_plane].i_pitch;
645
646                         p_vout->p_vlc->pf_memcpy( p_out, p_in,
647                                                   p_pic->p[i_plane].i_pitch );
648
649                         p_in += 2 * p_pic->p[i_plane].i_pitch;
650                         p_out += p_pic->p[i_plane].i_pitch;
651                     }
652                 }
653                 else
654                 {
655                     for( ; p_out < p_out_end ; )
656                     {
657                         p_vout->p_vlc->pf_memcpy( p_out, p_in,
658                                                   p_pic->p[i_plane].i_pitch );
659
660                         p_out += p_pic->p[i_plane].i_pitch;
661                         p_in += 2 * p_pic->p[i_plane].i_pitch;
662                     }
663                 }
664
665                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
666                                           p_pic->p[i_plane].i_pitch );
667
668                 /* For TOP field we need to add the last line */
669                 if( i_field == 0 )
670                 {
671                     p_in += p_pic->p[i_plane].i_pitch;
672                     p_out += p_pic->p[i_plane].i_pitch;
673                     p_vout->p_vlc->pf_memcpy( p_out, p_in,
674                                               p_pic->p[i_plane].i_pitch );
675                 }
676                 break;
677         }
678     }
679 }
680
681 #define Merge p_vout->p_sys->pf_merge
682 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
683
684 /*****************************************************************************
685  * RenderLinear: BOB with linear interpolation
686  *****************************************************************************/
687 static void RenderLinear( vout_thread_t *p_vout,
688                           picture_t *p_outpic, picture_t *p_pic, int i_field )
689 {
690     int i_plane;
691
692     /* Copy image and skip lines */
693     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
694     {
695         uint8_t *p_in, *p_out_end, *p_out;
696
697         p_in = p_pic->p[i_plane].p_pixels;
698         p_out = p_outpic->p[i_plane].p_pixels;
699         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
700                              * p_outpic->p[i_plane].i_visible_lines;
701
702         /* For BOTTOM field we need to add the first line */
703         if( i_field == 1 )
704         {
705             p_vout->p_vlc->pf_memcpy( p_out, p_in,
706                                       p_pic->p[i_plane].i_pitch );
707             p_in += p_pic->p[i_plane].i_pitch;
708             p_out += p_pic->p[i_plane].i_pitch;
709         }
710
711         p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
712
713         for( ; p_out < p_out_end ; )
714         {
715             p_vout->p_vlc->pf_memcpy( p_out, p_in,
716                                       p_pic->p[i_plane].i_pitch );
717
718             p_out += p_pic->p[i_plane].i_pitch;
719
720             Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
721                    p_pic->p[i_plane].i_pitch );
722
723             p_in += 2 * p_pic->p[i_plane].i_pitch;
724             p_out += p_pic->p[i_plane].i_pitch;
725         }
726
727         p_vout->p_vlc->pf_memcpy( p_out, p_in,
728                                   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_pic->p[i_plane].i_pitch;
735             p_vout->p_vlc->pf_memcpy( p_out, p_in,
736                                       p_pic->p[i_plane].i_pitch );
737         }
738     }
739     EndMerge();
740 }
741
742 static void RenderMean( vout_thread_t *p_vout,
743                         picture_t *p_outpic, picture_t *p_pic )
744 {
745     int i_plane;
746
747     /* Copy image and skip lines */
748     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
749     {
750         uint8_t *p_in, *p_out_end, *p_out;
751
752         p_in = p_pic->p[i_plane].p_pixels;
753
754         p_out = p_outpic->p[i_plane].p_pixels;
755         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
756                              * p_outpic->p[i_plane].i_visible_lines;
757
758         /* All lines: mean value */
759         for( ; p_out < p_out_end ; )
760         {
761             Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
762                    p_pic->p[i_plane].i_pitch );
763
764             p_out += p_pic->p[i_plane].i_pitch;
765             p_in += 2 * p_pic->p[i_plane].i_pitch;
766         }
767     }
768     EndMerge();
769 }
770
771 static void RenderBlend( vout_thread_t *p_vout,
772                          picture_t *p_outpic, picture_t *p_pic )
773 {
774     int i_plane;
775
776     /* Copy image and skip lines */
777     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
778     {
779         uint8_t *p_in, *p_out_end, *p_out;
780
781         p_in = p_pic->p[i_plane].p_pixels;
782
783         p_out = p_outpic->p[i_plane].p_pixels;
784         p_out_end = p_out + p_outpic->p[i_plane].i_pitch
785                              * p_outpic->p[i_plane].i_visible_lines;
786
787         switch( p_vout->render.i_chroma )
788         {
789             case VLC_FOURCC('I','4','2','0'):
790             case VLC_FOURCC('I','Y','U','V'):
791             case VLC_FOURCC('Y','V','1','2'):
792                 /* First line: simple copy */
793                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
794                                           p_pic->p[i_plane].i_pitch );
795                 p_out += p_pic->p[i_plane].i_pitch;
796
797                 /* Remaining lines: mean value */
798                 for( ; p_out < p_out_end ; )
799                 {
800                    Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
801                           p_pic->p[i_plane].i_pitch );
802
803                     p_out += p_pic->p[i_plane].i_pitch;
804                     p_in += p_pic->p[i_plane].i_pitch;
805                 }
806                 break;
807
808             case VLC_FOURCC('I','4','2','2'):
809                 /* First line: simple copy */
810                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
811                                           p_pic->p[i_plane].i_pitch );
812                 p_out += p_pic->p[i_plane].i_pitch;
813
814                 /* Remaining lines: mean value */
815                 if( i_plane == Y_PLANE )
816                 {
817                     for( ; p_out < p_out_end ; )
818                     {
819                         Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
820                                p_pic->p[i_plane].i_pitch );
821
822                         p_out += p_pic->p[i_plane].i_pitch;
823                         p_in += p_pic->p[i_plane].i_pitch;
824                     }
825                 }
826
827                 else
828                 {
829                     for( ; p_out < p_out_end ; )
830                     {
831                         Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
832                                p_pic->p[i_plane].i_pitch );
833
834                         p_out += p_pic->p[i_plane].i_pitch;
835                         p_in += 2*p_pic->p[i_plane].i_pitch;
836                     }
837                 }
838                 break;
839         }
840     }
841     EndMerge();
842 }
843
844 #undef Merge
845
846 static void MergeGeneric( void *_p_dest, const void *_p_s1,
847                           const void *_p_s2, size_t i_bytes )
848 {
849     uint8_t* p_dest = (uint8_t*)_p_dest;
850     const uint8_t *p_s1 = (const uint8_t *)_p_s1;
851     const uint8_t *p_s2 = (const uint8_t *)_p_s2;
852     uint8_t* p_end = p_dest + i_bytes - 8;
853
854     while( p_dest < p_end )
855     {
856         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
857         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
858         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
859         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
860         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
861         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
862         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
863         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
864     }
865
866     p_end += 8;
867
868     while( p_dest < p_end )
869     {
870         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
871     }
872 }
873
874 #if defined(CAN_COMPILE_MMXEXT)
875 static void MergeMMX( void *_p_dest, const void *_p_s1, const void *_p_s2,
876                       size_t i_bytes )
877 {
878     uint8_t* p_dest = (uint8_t*)_p_dest;
879     const uint8_t *p_s1 = (const uint8_t *)_p_s1;
880     const uint8_t *p_s2 = (const uint8_t *)_p_s2;
881     uint8_t* p_end = p_dest + i_bytes - 8;
882     while( p_dest < p_end )
883     {
884         __asm__  __volatile__( "movq %2,%%mm1;"
885                                "pavgb %1, %%mm1;"
886                                "movq %%mm1, %0" :"=m" (*p_dest):
887                                                  "m" (*p_s1),
888                                                  "m" (*p_s2) );
889         p_dest += 8;
890         p_s1 += 8;
891         p_s2 += 8;
892     }
893
894     p_end += 8;
895
896     while( p_dest < p_end )
897     {
898         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
899     }
900 }
901 #endif
902
903 #if defined(CAN_COMPILE_SSE)
904 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
905                        size_t i_bytes )
906 {
907     uint8_t* p_dest = (uint8_t*)_p_dest;
908     const uint8_t *p_s1 = (const uint8_t *)_p_s1;
909     const uint8_t *p_s2 = (const uint8_t *)_p_s2;
910     uint8_t* p_end;
911     while( (int)p_s1 % 16 )
912     {
913         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
914     }        
915     p_end = p_dest + i_bytes - 16;
916     while( p_dest < p_end )
917     {
918         __asm__  __volatile__( "movdqu %2,%%xmm1;"
919                                "pavgb %1, %%xmm1;"
920                                "movdqu %%xmm1, %0" :"=m" (*p_dest):
921                                                  "m" (*p_s1),
922                                                  "m" (*p_s2) );
923         p_dest += 16;
924         p_s1 += 16;
925         p_s2 += 16;
926     }
927
928     p_end += 16;
929
930     while( p_dest < p_end )
931     {
932         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
933     }
934 }
935 #endif
936
937 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
938 static void EndMMX( void )
939 {
940     __asm__ __volatile__( "emms" :: );
941 }
942 #endif
943
944 #ifdef CAN_COMPILE_C_ALTIVEC
945 static void MergeAltivec( void *_p_dest, const void *_p_s1,
946                           const void *_p_s2, size_t i_bytes )
947 {
948     uint8_t *p_dest = (uint8_t *)_p_dest;
949     uint8_t *p_s1   = (uint8_t *)_p_s1;
950     uint8_t *p_s2   = (uint8_t *)_p_s2;
951     uint8_t *p_end  = p_dest + i_bytes - 15;
952
953     /* Use C until the first 16-bytes aligned destination pixel */
954     while( (int)p_dest & 0xF )
955     {
956         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
957     }
958
959     if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
960     {
961         /* Unaligned source */
962         vector unsigned char s1v, s2v, destv;
963         vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
964         vector unsigned char perm1v, perm2v;
965
966         perm1v = vec_lvsl( 0, p_s1 );
967         perm2v = vec_lvsl( 0, p_s2 );
968         s1oldv = vec_ld( 0, p_s1 );
969         s2oldv = vec_ld( 0, p_s2 );
970
971         while( p_dest < p_end )
972         {
973             s1newv = vec_ld( 16, p_s1 );
974             s2newv = vec_ld( 16, p_s2 );
975             s1v    = vec_perm( s1oldv, s1newv, perm1v );
976             s2v    = vec_perm( s2oldv, s2newv, perm2v );
977             s1oldv = s1newv;
978             s2oldv = s2newv;
979             destv  = vec_avg( s1v, s2v );
980             vec_st( destv, 0, p_dest );
981
982             p_s1   += 16;
983             p_s2   += 16;
984             p_dest += 16;
985         }
986     }
987     else
988     {
989         /* Aligned source */
990         vector unsigned char s1v, s2v, destv;
991
992         while( p_dest < p_end )
993         {
994             s1v   = vec_ld( 0, p_s1 );
995             s2v   = vec_ld( 0, p_s2 );
996             destv = vec_avg( s1v, s2v );
997             vec_st( destv, 0, p_dest );
998
999             p_s1   += 16;
1000             p_s2   += 16;
1001             p_dest += 16;
1002         }
1003     }
1004
1005     p_end += 15;
1006
1007     while( p_dest < p_end )
1008     {
1009         *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1010     }
1011 }
1012 #endif
1013
1014 /*****************************************************************************
1015  * SendEvents: forward mouse and keyboard events to the parent p_vout
1016  *****************************************************************************/
1017 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1018                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1019 {
1020     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1021     vlc_value_t sentval = newval;
1022
1023     if( !strcmp( psz_var, "mouse-y" ) )
1024     {
1025         switch( p_vout->p_sys->i_mode )
1026         {
1027             case DEINTERLACE_MEAN:
1028             case DEINTERLACE_DISCARD:
1029                 sentval.i_int *= 2;
1030                 break;
1031         }
1032     }
1033
1034     var_Set( p_vout, psz_var, sentval );
1035
1036     return VLC_SUCCESS;
1037 }
1038
1039 /*****************************************************************************
1040  * FilterCallback: called when changing the deinterlace method on the fly.
1041  *****************************************************************************/
1042 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1043                            vlc_value_t oldval, vlc_value_t newval,
1044                            void *p_data )
1045 {
1046     vout_thread_t * p_vout = (vout_thread_t *)p_this;
1047     int i_old_mode = p_vout->p_sys->i_mode;
1048
1049     msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1050
1051     vlc_mutex_lock( &p_vout->p_sys->filter_lock );
1052
1053     SetFilterMethod( p_vout, newval.psz_string );
1054
1055     switch( p_vout->render.i_chroma )
1056     {
1057     case VLC_FOURCC('I','4','2','2'):
1058         vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1059         return VLC_SUCCESS;
1060         break;
1061
1062     case VLC_FOURCC('I','4','2','0'):
1063     case VLC_FOURCC('I','Y','U','V'):
1064     case VLC_FOURCC('Y','V','1','2'):
1065         switch( p_vout->p_sys->i_mode )
1066         {
1067         case DEINTERLACE_MEAN:
1068         case DEINTERLACE_DISCARD:
1069             if( ( i_old_mode == DEINTERLACE_MEAN )
1070                 || ( i_old_mode == DEINTERLACE_DISCARD ) )
1071             {
1072                 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1073                 return VLC_SUCCESS;
1074             }
1075             break;
1076
1077         case DEINTERLACE_BOB:
1078         case DEINTERLACE_BLEND:
1079         case DEINTERLACE_LINEAR:
1080             if( ( i_old_mode == DEINTERLACE_BOB )
1081                 || ( i_old_mode == DEINTERLACE_BLEND )
1082                 || ( i_old_mode == DEINTERLACE_LINEAR ) )
1083             {
1084                 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1085                 return VLC_SUCCESS;
1086             }
1087             break;
1088         }
1089         break;
1090
1091     default:
1092         break;
1093     }
1094
1095     /* We need to kill the old vout */
1096
1097     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1098
1099     vlc_object_detach( p_vout->p_sys->p_vout );
1100     vout_Destroy( p_vout->p_sys->p_vout );
1101
1102     /* Try to open a new video output */
1103     p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
1104
1105     if( p_vout->p_sys->p_vout == NULL )
1106     {
1107         /* Everything failed */
1108         msg_Err( p_vout, "cannot open vout, aborting" );
1109
1110         vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1111         return VLC_EGENERIC;
1112     }
1113
1114     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
1115
1116     vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
1117     return VLC_SUCCESS;
1118 }
1119
1120 /*****************************************************************************
1121  * SendEventsToChild: forward events to the child/children vout
1122  *****************************************************************************/
1123 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
1124                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1125 {
1126     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1127     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
1128     return VLC_SUCCESS;
1129 }