1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 the VideoLAN team
7 * Author: Sam Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
42 #include "vlc_filter.h"
44 #ifdef CAN_COMPILE_MMXEXT
48 #include "filter_common.h"
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
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Create ( vlc_object_t * );
61 static void Destroy ( vlc_object_t * );
63 static int Init ( vout_thread_t * );
64 static void End ( vout_thread_t * );
65 static void Render ( vout_thread_t *, picture_t * );
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 * );
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 );
78 #if defined(CAN_COMPILE_MMXEXT)
79 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
81 #if defined(CAN_COMPILE_3DNOW)
82 static void Merge3DNow ( void *, const void *, const void *, size_t );
84 #if defined(CAN_COMPILE_SSE)
85 static void MergeSSE2 ( void *, const void *, const void *, size_t );
87 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
88 static void EndMMX ( void );
90 #if defined(CAN_COMPILE_3DNOW)
91 static void End3DNow ( void );
94 static int SendEvents ( vlc_object_t *, char const *,
95 vlc_value_t, vlc_value_t, void * );
97 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method );
98 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
100 static int OpenFilter( vlc_object_t *p_this );
101 static void CloseFilter( vlc_object_t *p_this );
103 /*****************************************************************************
104 * Callback prototypes
105 *****************************************************************************/
106 static int FilterCallback ( vlc_object_t *, char const *,
107 vlc_value_t, vlc_value_t, void * );
109 /*****************************************************************************
111 *****************************************************************************/
112 #define MODE_TEXT N_("Deinterlace mode")
113 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
115 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
116 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
118 #define FILTER_CFG_PREFIX "sout-deinterlace-"
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" };
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 );
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 );
137 add_shortcut( "deinterlace" );
138 set_callbacks( Create, Destroy );
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 );
149 static const char *const ppsz_filter_options[] = {
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 *****************************************************************************/
161 int i_mode; /* Deinterlace mode */
162 bool b_double_rate; /* Shall we double the framerate? */
167 vout_thread_t *p_vout;
169 vlc_mutex_t filter_lock;
171 void (*pf_merge) ( void *, const void *, const void *, size_t );
172 void (*pf_end_merge) ( void );
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 )
180 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
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 )
190 vout_thread_t *p_vout = (vout_thread_t *)p_this;
193 /* Allocate structure */
194 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
195 if( p_vout->p_sys == NULL )
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;
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 );
211 #if defined(CAN_COMPILE_C_ALTIVEC)
212 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
214 p_vout->p_sys->pf_merge = MergeAltivec;
215 p_vout->p_sys->pf_end_merge = NULL;
219 #if defined(CAN_COMPILE_SSE)
220 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
222 p_vout->p_sys->pf_merge = MergeSSE2;
223 p_vout->p_sys->pf_end_merge = EndMMX;
227 #if defined(CAN_COMPILE_MMXEXT)
228 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
230 p_vout->p_sys->pf_merge = MergeMMXEXT;
231 p_vout->p_sys->pf_end_merge = EndMMX;
235 #if defined(CAN_COMPILE_3DNOW)
236 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
238 p_vout->p_sys->pf_merge = Merge3DNow;
239 p_vout->p_sys->pf_end_merge = End3DNow;
244 p_vout->p_sys->pf_merge = MergeGeneric;
245 p_vout->p_sys->pf_end_merge = NULL;
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 );
252 if( val.psz_string == NULL )
254 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
255 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
257 val.psz_string = strdup( "discard" );
260 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
262 SetFilterMethod( p_vout, val.psz_string );
264 free( val.psz_string );
269 /*****************************************************************************
270 * SetFilterMethod: setup the deinterlace method to use.
271 *****************************************************************************/
272 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
274 if( !strcmp( psz_method, "discard" ) )
276 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
277 p_vout->p_sys->b_double_rate = false;
279 else if( !strcmp( psz_method, "mean" ) )
281 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
282 p_vout->p_sys->b_double_rate = false;
284 else if( !strcmp( psz_method, "blend" )
285 || !strcmp( psz_method, "average" )
286 || !strcmp( psz_method, "combine-fields" ) )
288 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
289 p_vout->p_sys->b_double_rate = false;
291 else if( !strcmp( psz_method, "bob" )
292 || !strcmp( psz_method, "progressive-scan" ) )
294 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
295 p_vout->p_sys->b_double_rate = true;
297 else if( !strcmp( psz_method, "linear" ) )
299 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
300 p_vout->p_sys->b_double_rate = true;
302 else if( !strcmp( psz_method, "x" ) )
304 p_vout->p_sys->i_mode = DEINTERLACE_X;
305 p_vout->p_sys->b_double_rate = false;
309 msg_Err( p_vout, "no valid deinterlace mode provided, "
310 "using \"discard\"" );
313 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
316 /*****************************************************************************
317 * Init: initialize Deinterlace video thread output method
318 *****************************************************************************/
319 static int Init( vout_thread_t *p_vout )
324 I_OUTPUTPICTURES = 0;
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 )
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;
342 return VLC_EGENERIC; /* unknown chroma */
346 /* Try to open the real video output */
347 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
349 if( p_vout->p_sys->p_vout == NULL )
351 /* Everything failed */
352 msg_Err( p_vout, "cannot open vout, aborting" );
357 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
359 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
361 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
363 ADD_PARENT_CALLBACKS( SendEventsToChild );
368 /*****************************************************************************
369 * SpawnRealVout: spawn the real video output.
370 *****************************************************************************/
371 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
373 vout_thread_t *p_real_vout = NULL;
375 memset( &fmt, 0, sizeof( video_format_t ) );
377 msg_Dbg( p_vout, "spawning the real video output" );
379 fmt = p_vout->fmt_out;
381 switch( p_vout->render.i_chroma )
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 )
388 case DEINTERLACE_MEAN:
389 case DEINTERLACE_DISCARD:
390 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
392 p_real_vout = vout_Create( p_vout, &fmt );
395 case DEINTERLACE_BOB:
396 case DEINTERLACE_BLEND:
397 case DEINTERLACE_LINEAR:
399 p_real_vout = vout_Create( p_vout, &fmt );
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 );
416 /*****************************************************************************
417 * End: terminate Deinterlace video thread output method
418 *****************************************************************************/
419 static void End( vout_thread_t *p_vout )
423 DEL_PARENT_CALLBACKS( SendEventsToChild );
425 if( p_vout->p_sys->p_vout )
426 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
428 /* Free the fake output buffers we allocated */
429 for( i_index = I_OUTPUTPICTURES ; i_index ; )
432 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
435 if( p_vout->p_sys->p_vout )
436 vout_CloseAndRelease( p_vout->p_sys->p_vout );
439 /*****************************************************************************
440 * Destroy: destroy Deinterlace video thread output method
441 *****************************************************************************
442 * Terminate an output method created by DeinterlaceCreateOutputMethod
443 *****************************************************************************/
444 static void Destroy( vlc_object_t *p_this )
446 vout_thread_t *p_vout = (vout_thread_t *)p_this;
447 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
448 free( p_vout->p_sys );
451 /*****************************************************************************
452 * Render: displays previously rendered output
453 *****************************************************************************
454 * This function send the currently rendered image to Deinterlace image,
455 * waits until it is displayed and switch the two rendering buffers, preparing
457 *****************************************************************************/
458 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
460 vout_sys_t *p_sys = p_vout->p_sys;
461 picture_t *pp_outpic[2];
463 p_vout->fmt_out.i_x_offset = p_sys->p_vout->fmt_in.i_x_offset =
464 p_vout->fmt_in.i_x_offset;
465 p_vout->fmt_out.i_y_offset = p_sys->p_vout->fmt_in.i_y_offset =
466 p_vout->fmt_in.i_y_offset;
467 p_vout->fmt_out.i_visible_width = p_sys->p_vout->fmt_in.i_visible_width =
468 p_vout->fmt_in.i_visible_width;
469 p_vout->fmt_out.i_visible_height = p_sys->p_vout->fmt_in.i_visible_height =
470 p_vout->fmt_in.i_visible_height;
471 if( p_vout->p_sys->i_mode == DEINTERLACE_MEAN ||
472 p_vout->p_sys->i_mode == DEINTERLACE_DISCARD )
474 p_vout->fmt_out.i_y_offset /= 2; p_sys->p_vout->fmt_in.i_y_offset /= 2;
475 p_vout->fmt_out.i_visible_height /= 2;
476 p_sys->p_vout->fmt_in.i_visible_height /= 2;
479 pp_outpic[0] = pp_outpic[1] = NULL;
481 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
483 /* Get a new picture */
484 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
488 if( !vlc_object_alive (p_vout) || p_vout->b_error )
490 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
493 msleep( VOUT_OUTMEM_SLEEP );
496 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
498 /* If we are using double rate, get an additional new picture */
499 if( p_vout->p_sys->b_double_rate )
501 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
505 if( !vlc_object_alive (p_vout) || p_vout->b_error )
507 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
508 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
511 msleep( VOUT_OUTMEM_SLEEP );
514 /* 20ms is a bit arbitrary, but it's only for the first image we get */
515 if( !p_vout->p_sys->last_date )
517 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
518 p_pic->date + 20000 );
522 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
523 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
525 p_vout->p_sys->last_date = p_pic->date;
528 switch( p_vout->p_sys->i_mode )
530 case DEINTERLACE_DISCARD:
531 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
532 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
535 case DEINTERLACE_BOB:
536 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
537 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
538 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
539 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
542 case DEINTERLACE_LINEAR:
543 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
544 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
545 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
546 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
549 case DEINTERLACE_MEAN:
550 RenderMean( p_vout, pp_outpic[0], p_pic );
551 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
554 case DEINTERLACE_BLEND:
555 RenderBlend( p_vout, pp_outpic[0], p_pic );
556 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
560 RenderX( pp_outpic[0], p_pic );
561 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
564 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
567 /*****************************************************************************
568 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
569 *****************************************************************************/
570 static void RenderDiscard( vout_thread_t *p_vout,
571 picture_t *p_outpic, picture_t *p_pic, int i_field )
575 /* Copy image and skip lines */
576 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
578 uint8_t *p_in, *p_out_end, *p_out;
581 p_in = p_pic->p[i_plane].p_pixels
582 + i_field * p_pic->p[i_plane].i_pitch;
584 p_out = p_outpic->p[i_plane].p_pixels;
585 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
586 * p_outpic->p[i_plane].i_visible_lines;
588 switch( p_vout->render.i_chroma )
590 case VLC_FOURCC('I','4','2','0'):
591 case VLC_FOURCC('I','Y','U','V'):
592 case VLC_FOURCC('Y','V','1','2'):
594 for( ; p_out < p_out_end ; )
596 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
598 p_out += p_outpic->p[i_plane].i_pitch;
599 p_in += 2 * p_pic->p[i_plane].i_pitch;
603 case VLC_FOURCC('I','4','2','2'):
605 i_increment = 2 * p_pic->p[i_plane].i_pitch;
607 if( i_plane == Y_PLANE )
609 for( ; p_out < p_out_end ; )
611 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
612 p_out += p_outpic->p[i_plane].i_pitch;
613 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
614 p_out += p_outpic->p[i_plane].i_pitch;
620 for( ; p_out < p_out_end ; )
622 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
623 p_out += p_outpic->p[i_plane].i_pitch;
635 /*****************************************************************************
636 * RenderBob: renders a BOB picture - simple copy
637 *****************************************************************************/
638 static void RenderBob( vout_thread_t *p_vout,
639 picture_t *p_outpic, picture_t *p_pic, int i_field )
643 /* Copy image and skip lines */
644 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
646 uint8_t *p_in, *p_out_end, *p_out;
648 p_in = p_pic->p[i_plane].p_pixels;
649 p_out = p_outpic->p[i_plane].p_pixels;
650 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
651 * p_outpic->p[i_plane].i_visible_lines;
653 switch( p_vout->render.i_chroma )
655 case VLC_FOURCC('I','4','2','0'):
656 case VLC_FOURCC('I','Y','U','V'):
657 case VLC_FOURCC('Y','V','1','2'):
658 /* For BOTTOM field we need to add the first line */
661 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
662 p_in += p_pic->p[i_plane].i_pitch;
663 p_out += p_outpic->p[i_plane].i_pitch;
666 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
668 for( ; p_out < p_out_end ; )
670 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
672 p_out += p_outpic->p[i_plane].i_pitch;
674 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
676 p_in += 2 * p_pic->p[i_plane].i_pitch;
677 p_out += p_outpic->p[i_plane].i_pitch;
680 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
682 /* For TOP field we need to add the last line */
685 p_in += p_pic->p[i_plane].i_pitch;
686 p_out += p_outpic->p[i_plane].i_pitch;
687 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
691 case VLC_FOURCC('I','4','2','2'):
692 /* For BOTTOM field we need to add the first line */
695 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
696 p_in += p_pic->p[i_plane].i_pitch;
697 p_out += p_outpic->p[i_plane].i_pitch;
700 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
702 if( i_plane == Y_PLANE )
704 for( ; p_out < p_out_end ; )
706 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
708 p_out += p_outpic->p[i_plane].i_pitch;
710 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
712 p_in += 2 * p_pic->p[i_plane].i_pitch;
713 p_out += p_outpic->p[i_plane].i_pitch;
718 for( ; p_out < p_out_end ; )
720 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
722 p_out += p_outpic->p[i_plane].i_pitch;
723 p_in += 2 * p_pic->p[i_plane].i_pitch;
727 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
729 /* For TOP field we need to add the last line */
732 p_in += p_pic->p[i_plane].i_pitch;
733 p_out += p_outpic->p[i_plane].i_pitch;
734 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
741 #define Merge p_vout->p_sys->pf_merge
742 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
744 /*****************************************************************************
745 * RenderLinear: BOB with linear interpolation
746 *****************************************************************************/
747 static void RenderLinear( vout_thread_t *p_vout,
748 picture_t *p_outpic, picture_t *p_pic, int i_field )
752 /* Copy image and skip lines */
753 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
755 uint8_t *p_in, *p_out_end, *p_out;
757 p_in = p_pic->p[i_plane].p_pixels;
758 p_out = p_outpic->p[i_plane].p_pixels;
759 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
760 * p_outpic->p[i_plane].i_visible_lines;
762 /* For BOTTOM field we need to add the first line */
765 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
766 p_in += p_pic->p[i_plane].i_pitch;
767 p_out += p_outpic->p[i_plane].i_pitch;
770 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
772 for( ; p_out < p_out_end ; )
774 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
776 p_out += p_outpic->p[i_plane].i_pitch;
778 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
779 p_pic->p[i_plane].i_pitch );
781 p_in += 2 * p_pic->p[i_plane].i_pitch;
782 p_out += p_outpic->p[i_plane].i_pitch;
785 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
787 /* For TOP field we need to add the last line */
790 p_in += p_pic->p[i_plane].i_pitch;
791 p_out += p_outpic->p[i_plane].i_pitch;
792 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
798 static void RenderMean( vout_thread_t *p_vout,
799 picture_t *p_outpic, picture_t *p_pic )
803 /* Copy image and skip lines */
804 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
806 uint8_t *p_in, *p_out_end, *p_out;
808 p_in = p_pic->p[i_plane].p_pixels;
810 p_out = p_outpic->p[i_plane].p_pixels;
811 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
812 * p_outpic->p[i_plane].i_visible_lines;
814 /* All lines: mean value */
815 for( ; p_out < p_out_end ; )
817 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
818 p_pic->p[i_plane].i_pitch );
820 p_out += p_outpic->p[i_plane].i_pitch;
821 p_in += 2 * p_pic->p[i_plane].i_pitch;
827 static void RenderBlend( vout_thread_t *p_vout,
828 picture_t *p_outpic, picture_t *p_pic )
832 /* Copy image and skip lines */
833 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
835 uint8_t *p_in, *p_out_end, *p_out;
837 p_in = p_pic->p[i_plane].p_pixels;
839 p_out = p_outpic->p[i_plane].p_pixels;
840 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
841 * p_outpic->p[i_plane].i_visible_lines;
843 switch( p_vout->render.i_chroma )
845 case VLC_FOURCC('I','4','2','0'):
846 case VLC_FOURCC('I','Y','U','V'):
847 case VLC_FOURCC('Y','V','1','2'):
848 /* First line: simple copy */
849 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
850 p_out += p_outpic->p[i_plane].i_pitch;
852 /* Remaining lines: mean value */
853 for( ; p_out < p_out_end ; )
855 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
856 p_pic->p[i_plane].i_pitch );
858 p_out += p_outpic->p[i_plane].i_pitch;
859 p_in += p_pic->p[i_plane].i_pitch;
863 case VLC_FOURCC('I','4','2','2'):
864 /* First line: simple copy */
865 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
866 p_out += p_outpic->p[i_plane].i_pitch;
868 /* Remaining lines: mean value */
869 if( i_plane == Y_PLANE )
871 for( ; p_out < p_out_end ; )
873 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
874 p_pic->p[i_plane].i_pitch );
876 p_out += p_outpic->p[i_plane].i_pitch;
877 p_in += p_pic->p[i_plane].i_pitch;
883 for( ; p_out < p_out_end ; )
885 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
886 p_pic->p[i_plane].i_pitch );
888 p_out += p_outpic->p[i_plane].i_pitch;
889 p_in += 2*p_pic->p[i_plane].i_pitch;
900 static void MergeGeneric( void *_p_dest, const void *_p_s1,
901 const void *_p_s2, size_t i_bytes )
903 uint8_t* p_dest = (uint8_t*)_p_dest;
904 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
905 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
906 uint8_t* p_end = p_dest + i_bytes - 8;
908 while( p_dest < p_end )
910 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
911 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
912 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
913 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
914 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
915 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
916 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
917 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
922 while( p_dest < p_end )
924 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
928 #if defined(CAN_COMPILE_MMXEXT)
929 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
932 uint8_t* p_dest = (uint8_t*)_p_dest;
933 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
934 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
935 uint8_t* p_end = p_dest + i_bytes - 8;
936 while( p_dest < p_end )
938 __asm__ __volatile__( "movq %2,%%mm1;"
940 "movq %%mm1, %0" :"=m" (*p_dest):
950 while( p_dest < p_end )
952 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
957 #if defined(CAN_COMPILE_3DNOW)
958 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
961 uint8_t* p_dest = (uint8_t*)_p_dest;
962 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
963 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
964 uint8_t* p_end = p_dest + i_bytes - 8;
965 while( p_dest < p_end )
967 __asm__ __volatile__( "movq %2,%%mm1;"
969 "movq %%mm1, %0" :"=m" (*p_dest):
979 while( p_dest < p_end )
981 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
986 #if defined(CAN_COMPILE_SSE)
987 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
990 uint8_t* p_dest = (uint8_t*)_p_dest;
991 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
992 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
994 while( (uintptr_t)p_s1 % 16 )
996 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
998 p_end = p_dest + i_bytes - 16;
999 while( p_dest < p_end )
1001 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1003 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1013 while( p_dest < p_end )
1015 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1020 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1021 static void EndMMX( void )
1023 __asm__ __volatile__( "emms" :: );
1027 #if defined(CAN_COMPILE_3DNOW)
1028 static void End3DNow( void )
1030 __asm__ __volatile__( "femms" :: );
1034 #ifdef CAN_COMPILE_C_ALTIVEC
1035 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1036 const void *_p_s2, size_t i_bytes )
1038 uint8_t *p_dest = (uint8_t *)_p_dest;
1039 uint8_t *p_s1 = (uint8_t *)_p_s1;
1040 uint8_t *p_s2 = (uint8_t *)_p_s2;
1041 uint8_t *p_end = p_dest + i_bytes - 15;
1043 /* Use C until the first 16-bytes aligned destination pixel */
1044 while( (int)p_dest & 0xF )
1046 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1049 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1051 /* Unaligned source */
1052 vector unsigned char s1v, s2v, destv;
1053 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1054 vector unsigned char perm1v, perm2v;
1056 perm1v = vec_lvsl( 0, p_s1 );
1057 perm2v = vec_lvsl( 0, p_s2 );
1058 s1oldv = vec_ld( 0, p_s1 );
1059 s2oldv = vec_ld( 0, p_s2 );
1061 while( p_dest < p_end )
1063 s1newv = vec_ld( 16, p_s1 );
1064 s2newv = vec_ld( 16, p_s2 );
1065 s1v = vec_perm( s1oldv, s1newv, perm1v );
1066 s2v = vec_perm( s2oldv, s2newv, perm2v );
1069 destv = vec_avg( s1v, s2v );
1070 vec_st( destv, 0, p_dest );
1079 /* Aligned source */
1080 vector unsigned char s1v, s2v, destv;
1082 while( p_dest < p_end )
1084 s1v = vec_ld( 0, p_s1 );
1085 s2v = vec_ld( 0, p_s2 );
1086 destv = vec_avg( s1v, s2v );
1087 vec_st( destv, 0, p_dest );
1097 while( p_dest < p_end )
1099 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1104 /*****************************************************************************
1105 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1106 * and apply a process to recreate the bottom field :
1107 * If a 8x8 block is classified as :
1108 * - progressive: it applies a small blend (1,6,1)
1110 * * in the MMX version: we do a ME between the 2 fields, if there is a
1111 * good match we use MC to recreate the bottom field (with a small
1113 * * otherwise: it recreates the bottom field by an edge oriented
1115 *****************************************************************************/
1117 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1118 * XXX: It need to access to 8x10
1119 * We use more than 8 lines to help with scrolling (text)
1120 * (and because XDeint8x8Frame use line 9)
1121 * XXX: smooth/uniform area with noise detection doesn't works well
1122 * but it's not really a problem because they don't have much details anyway
1124 static inline int ssd( int a ) { return a*a; }
1125 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1131 /* Detect interlacing */
1133 for( y = 0; y < 7; y += 2 )
1136 for( x = 0; x < 8; x++ )
1138 fr += ssd(src[ x] - src[1*i_src+x]) +
1139 ssd(src[i_src+x] - src[2*i_src+x]);
1140 ff += ssd(src[ x] - src[2*i_src+x]) +
1141 ssd(src[i_src+x] - src[3*i_src+x]);
1143 if( ff < 6*fr/8 && fr > 32 )
1149 return fc < 1 ? false : true;
1151 #ifdef CAN_COMPILE_MMXEXT
1152 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1159 /* Detect interlacing */
1161 pxor_r2r( mm7, mm7 );
1162 for( y = 0; y < 9; y += 2 )
1165 pxor_r2r( mm5, mm5 );
1166 pxor_r2r( mm6, mm6 );
1167 for( x = 0; x < 8; x+=4 )
1169 movd_m2r( src[ x], mm0 );
1170 movd_m2r( src[1*i_src+x], mm1 );
1171 movd_m2r( src[2*i_src+x], mm2 );
1172 movd_m2r( src[3*i_src+x], mm3 );
1174 punpcklbw_r2r( mm7, mm0 );
1175 punpcklbw_r2r( mm7, mm1 );
1176 punpcklbw_r2r( mm7, mm2 );
1177 punpcklbw_r2r( mm7, mm3 );
1179 movq_r2r( mm0, mm4 );
1181 psubw_r2r( mm1, mm0 );
1182 psubw_r2r( mm2, mm4 );
1184 psubw_r2r( mm1, mm2 );
1185 psubw_r2r( mm1, mm3 );
1187 pmaddwd_r2r( mm0, mm0 );
1188 pmaddwd_r2r( mm4, mm4 );
1189 pmaddwd_r2r( mm2, mm2 );
1190 pmaddwd_r2r( mm3, mm3 );
1191 paddd_r2r( mm0, mm2 );
1192 paddd_r2r( mm4, mm3 );
1193 paddd_r2r( mm2, mm5 );
1194 paddd_r2r( mm3, mm6 );
1197 movq_r2r( mm5, mm0 );
1198 psrlq_i2r( 32, mm0 );
1199 paddd_r2r( mm0, mm5 );
1200 movd_r2m( mm5, fr );
1202 movq_r2r( mm6, mm0 );
1203 psrlq_i2r( 32, mm0 );
1204 paddd_r2r( mm0, mm6 );
1205 movd_r2m( mm6, ff );
1207 if( ff < 6*fr/8 && fr > 32 )
1216 /* XDeint8x8Frame: apply a small blend between field (1,6,1).
1217 * This won't destroy details, and help if there is a bit of interlacing.
1218 * (It helps with paning to avoid flickers)
1222 static inline void XDeint8x8FrameC( uint8_t *dst, int i_dst,
1223 uint8_t *src, int i_src )
1228 for( y = 0; y < 8; y += 2 )
1230 memcpy( dst, src, 8 );
1233 for( x = 0; x < 8; x++ )
1234 dst[x] = (src[x] + 6*src[1*i_src+x] + src[2*i_src+x] + 4 ) >> 3;
1240 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1241 uint8_t *src1, int i_src1,
1242 uint8_t *src2, int i_src2 )
1247 for( y = 0; y < 8; y += 2 )
1249 memcpy( dst, src1, 8 );
1252 for( x = 0; x < 8; x++ )
1253 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1261 #ifdef CAN_COMPILE_MMXEXT
1262 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1263 uint8_t *src1, int i_src1,
1264 uint8_t *src2, int i_src2 )
1266 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1270 pxor_r2r( mm7, mm7 );
1271 for( y = 0; y < 8; y += 2 )
1273 for( x = 0; x < 8; x +=4 )
1275 movd_m2r( src1[x], mm0 );
1276 movd_r2m( mm0, dst[x] );
1278 movd_m2r( src2[x], mm1 );
1279 movd_m2r( src1[i_src1+x], mm2 );
1281 punpcklbw_r2r( mm7, mm0 );
1282 punpcklbw_r2r( mm7, mm1 );
1283 punpcklbw_r2r( mm7, mm2 );
1284 paddw_r2r( mm1, mm1 );
1285 movq_r2r( mm1, mm3 );
1286 paddw_r2r( mm3, mm3 );
1287 paddw_r2r( mm2, mm0 );
1288 paddw_r2r( mm3, mm1 );
1289 paddw_m2r( m_4, mm1 );
1290 paddw_r2r( mm1, mm0 );
1291 psraw_i2r( 3, mm0 );
1292 packuswb_r2r( mm7, mm0 );
1293 movd_r2m( mm0, dst[i_dst+x] );
1304 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1307 for( y = 0; y < 8; y++ )
1308 memset( &dst[y*i_dst], v, 8 );
1311 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1314 * TODO: a better one for the inner part.
1316 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1317 uint8_t *src, int i_src )
1322 for( y = 0; y < 8; y += 2 )
1324 memcpy( dst, src, 8 );
1327 for( x = 0; x < 8; x++ )
1328 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1333 #ifdef CAN_COMPILE_MMXEXT
1334 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1335 uint8_t *src, int i_src )
1340 for( y = 0; y < 8; y += 2 )
1342 movq_m2r( src[0], mm0 );
1343 movq_r2m( mm0, dst[0] );
1346 movq_m2r( src[2*i_src], mm1 );
1347 pavgb_r2r( mm1, mm0 );
1349 movq_r2m( mm0, dst[0] );
1357 /* XDeint8x8Field: Edge oriented interpolation
1358 * (Need -4 and +5 pixels H, +1 line)
1360 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1361 uint8_t *src, int i_src )
1366 for( y = 0; y < 8; y += 2 )
1368 memcpy( dst, src, 8 );
1371 for( x = 0; x < 8; x++ )
1373 uint8_t *src2 = &src[2*i_src];
1374 /* I use 8 pixels just to match the MMX version, but it's overkill
1375 * 5 would be enough (less isn't good) */
1376 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1377 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1378 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1379 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1381 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1382 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1383 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1384 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1386 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1387 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1388 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1389 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1391 if( c0 < c1 && c1 <= c2 )
1392 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1393 else if( c2 < c1 && c1 <= c0 )
1394 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1396 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1403 #ifdef CAN_COMPILE_MMXEXT
1404 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1405 uint8_t *src, int i_src )
1410 for( y = 0; y < 8; y += 2 )
1412 memcpy( dst, src, 8 );
1415 for( x = 0; x < 8; x++ )
1417 uint8_t *src2 = &src[2*i_src];
1420 movq_m2r( src[x-2], mm0 );
1421 movq_m2r( src[x-3], mm1 );
1422 movq_m2r( src[x-4], mm2 );
1424 psadbw_m2r( src2[x-4], mm0 );
1425 psadbw_m2r( src2[x-3], mm1 );
1426 psadbw_m2r( src2[x-2], mm2 );
1428 movd_r2m( mm0, c2 );
1429 movd_r2m( mm1, c1 );
1430 movd_r2m( mm2, c0 );
1432 if( c0 < c1 && c1 <= c2 )
1433 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1434 else if( c2 < c1 && c1 <= c0 )
1435 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1437 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1447 static inline int XDeint8x8SsdC( uint8_t *pix1, int i_pix1,
1448 uint8_t *pix2, int i_pix2 )
1453 for( y = 0; y < 8; y++ )
1454 for( x = 0; x < 8; x++ )
1455 s += ssd( pix1[y*i_pix1+x] - pix2[y*i_pix2+x] );
1459 #ifdef CAN_COMPILE_MMXEXT
1460 static inline int XDeint8x8SsdMMXEXT( uint8_t *pix1, int i_pix1,
1461 uint8_t *pix2, int i_pix2 )
1466 pxor_r2r( mm7, mm7 );
1467 pxor_r2r( mm6, mm6 );
1469 for( y = 0; y < 8; y++ )
1471 movq_m2r( pix1[0], mm0 );
1472 movq_m2r( pix2[0], mm1 );
1474 movq_r2r( mm0, mm2 );
1475 movq_r2r( mm1, mm3 );
1477 punpcklbw_r2r( mm7, mm0 );
1478 punpckhbw_r2r( mm7, mm2 );
1479 punpcklbw_r2r( mm7, mm1 );
1480 punpckhbw_r2r( mm7, mm3 );
1482 psubw_r2r( mm1, mm0 );
1483 psubw_r2r( mm3, mm2 );
1485 pmaddwd_r2r( mm0, mm0 );
1486 pmaddwd_r2r( mm2, mm2 );
1488 paddd_r2r( mm2, mm0 );
1489 paddd_r2r( mm0, mm6 );
1495 movq_r2r( mm6, mm7 );
1496 psrlq_i2r( 32, mm7 );
1497 paddd_r2r( mm6, mm7 );
1506 /* A little try with motion, but doesn't work better that pure intra (and slow) */
1507 #ifdef CAN_COMPILE_MMXEXT
1510 * TODO: mmx version (easier in sse2)
1512 static inline void XDeintMC( uint8_t *dst, int i_dst,
1513 uint8_t *src, int i_src,
1515 int i_width, int i_height )
1517 const int d4x = mvx&0x03;
1518 const int d4y = mvy&0x03;
1520 const int cA = (4-d4x)*(4-d4y);
1521 const int cB = d4x *(4-d4y);
1522 const int cC = (4-d4x)*d4y;
1523 const int cD = d4x *d4y;
1529 src += (mvy >> 2) * i_src + (mvx >> 2);
1532 for( y = 0; y < i_height; y++ )
1534 for( x = 0; x < i_width; x++ )
1536 dst[x] = ( cA*src[x] + cB*src[x+1] +
1537 cC*srcp[x] + cD*srcp[x+1] + 8 ) >> 4;
1545 static int XDeint8x4SadMMXEXT( uint8_t *pix1, int i_pix1,
1546 uint8_t *pix2, int i_pix2 )
1550 movq_m2r( pix1[0*i_pix1], mm0 );
1551 movq_m2r( pix1[1*i_pix1], mm1 );
1553 psadbw_m2r( pix2[0*i_pix2], mm0 );
1554 psadbw_m2r( pix2[1*i_pix2], mm1 );
1556 movq_m2r( pix1[2*i_pix1], mm2 );
1557 movq_m2r( pix1[3*i_pix1], mm3 );
1558 psadbw_m2r( pix2[2*i_pix2], mm2 );
1559 psadbw_m2r( pix2[3*i_pix2], mm3 );
1561 paddd_r2r( mm1, mm0 );
1562 paddd_r2r( mm3, mm2 );
1563 paddd_r2r( mm2, mm0 );
1569 static inline int XDeint8x4TestQpel( uint8_t *src, int i_src,
1570 uint8_t *ref, int i_stride,
1572 int xmax, int ymax )
1574 uint8_t buffer[8*4];
1576 if( abs(mx) >= 4*xmax || abs(my) >= 4*ymax )
1579 XDeintMC( buffer, 8, ref, i_stride, mx, my, 8, 4 );
1580 return XDeint8x4SadMMXEXT( src, i_src, buffer, 8 );
1582 static inline int XDeint8x4TestInt( uint8_t *src, int i_src,
1583 uint8_t *ref, int i_stride,
1585 int xmax, int ymax )
1587 if( abs(mx) >= xmax || abs(my) >= ymax )
1590 return XDeint8x4SadMMXEXT( src, i_src, &ref[my*i_stride+mx], i_stride );
1593 static inline void XDeint8x8FieldMotion( uint8_t *dst, int i_dst,
1594 uint8_t *src, int i_src,
1596 int xmax, int ymax )
1598 static const int dx[8] = { 0, 0, -1, 1, -1, -1, 1, 1 };
1599 static const int dy[8] = {-1, 1, 0, 0, -1, 1, -1, 1 };
1600 uint8_t *next = &src[i_src];
1601 const int i_src2 = 2*i_src;
1606 uint8_t *rec = &dst[i_dst];
1608 /* We construct with intra method the missing field */
1609 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1611 /* Now we will try to find a match with ME with the other field */
1613 /* ME: A small/partial EPZS
1614 * We search only for small MV (with high motion intra will be perfect */
1615 if( xmax > 4 ) xmax = 4;
1616 if( ymax > 4 ) ymax = 4;
1618 /* Init with NULL Mv */
1620 mvs = XDeint8x4SadMMXEXT( rec, i_src2, next, i_src2 );
1622 /* Try predicted Mv */
1623 if( (s=XDeint8x4TestInt( rec, i_src2, next, i_src2, *mpx, *mpy, xmax, ymax)) < mvs )
1629 /* Search interger pel (small mv) */
1630 for( i_step = 0; i_step < 4; i_step++ )
1636 for( i = 0; i < 4; i++ )
1638 s = XDeint8x4TestInt( rec, i_src2,
1639 next, i_src2, mvx+dx[i], mvy+dy[i],
1659 if( mvs > 4 && mvs < 256 )
1662 /* XXX: for now only HPEL (too slow) */
1663 for( i_step = 0; i_step < 4; i_step++ )
1669 for( i = 0; i < 8; i++ )
1671 s = XDeint8x4TestQpel( rec, i_src2, next, i_src2,
1672 mvx+dx[i], mvy+dy[i],
1690 uint8_t buffer[8*4];
1691 XDeintMC( buffer, 8, next, i_src2, mvx, mvy, 8, 4 );
1692 XDeint8x8MergeMMXEXT( dst, i_dst, src, 2*i_src, buffer, 8 );
1694 //XDeint8x8Set( dst, i_dst, 0 );
1701 /* Kernel interpolation (1,-5,20,20,-5,1)
1702 * Lose a bit more details+add aliasing than edge interpol but avoid
1705 static inline uint8_t clip1( int a )
1714 static inline void XDeint8x8Field( uint8_t *dst, int i_dst,
1715 uint8_t *src, int i_src )
1720 for( y = 0; y < 8; y += 2 )
1722 const int i_src2 = i_src*2;
1724 memcpy( dst, src, 8 );
1727 for( x = 0; x < 8; x++ )
1731 pix = 1*(src[-2*i_src2+x]+src[3*i_src2+x]) +
1732 -5*(src[-1*i_src2+x]+src[2*i_src2+x])
1733 +20*(src[ 0*i_src2+x]+src[1*i_src2+x]);
1735 dst[x] = clip1( ( pix + 16 ) >> 5 );
1745 /* NxN arbitray size (and then only use pixel in the NxN block)
1747 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1748 int i_height, int i_width )
1755 /* Detect interlacing */
1756 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1759 for( y = 0; y < i_height - 2; y += 2 )
1761 const uint8_t *s = &src[y*i_src];
1762 for( x = 0; x < i_width; x++ )
1764 fr += ssd(s[ x] - s[1*i_src+x]);
1765 ff += ssd(s[ x] - s[2*i_src+x]);
1767 if( ff < fr && fr > i_width / 2 )
1771 return fc < 2 ? false : true;
1774 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1775 uint8_t *src, int i_src,
1776 int i_width, int i_height )
1781 for( y = 0; y < i_height; y += 2 )
1783 memcpy( dst, src, i_width );
1786 if( y < i_height - 2 )
1788 for( x = 0; x < i_width; x++ )
1789 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1793 /* Blend last line */
1794 for( x = 0; x < i_width; x++ )
1795 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1802 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1803 uint8_t *src, int i_src,
1804 int i_width, int i_height )
1809 for( y = 0; y < i_height; y += 2 )
1811 memcpy( dst, src, i_width );
1814 if( y < i_height - 2 )
1816 for( x = 0; x < i_width; x++ )
1817 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1821 /* Blend last line */
1822 for( x = 0; x < i_width; x++ )
1823 dst[x] = (src[x] + src[i_src+x]) >> 1;
1830 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1831 int i_width, int i_height )
1833 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1834 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1836 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1840 static inline int median( int a, int b, int c )
1842 int min = a, max =a;
1853 return a + b + c - min - max;
1859 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1860 uint8_t *src, int i_src,
1861 const int i_mbx, int i_modx )
1865 for( x = 0; x < i_mbx; x++ )
1868 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1870 if( x == 0 || x == i_mbx - 1 )
1871 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1873 XDeint8x8FieldC( dst, i_dst, src, i_src );
1877 XDeint8x8MergeC( dst, i_dst,
1878 &src[0*i_src], 2*i_src,
1879 &src[1*i_src], 2*i_src );
1887 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1889 #ifdef CAN_COMPILE_MMXEXT
1890 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1891 uint8_t *src, int i_src,
1892 const int i_mbx, int i_modx )
1896 /* Reset current line */
1897 for( x = 0; x < i_mbx; x++ )
1900 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1902 if( x == 0 || x == i_mbx - 1 )
1903 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1905 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1909 XDeint8x8MergeMMXEXT( dst, i_dst,
1910 &src[0*i_src], 2*i_src,
1911 &src[1*i_src], 2*i_src );
1919 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1923 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1927 /* Copy image and skip lines */
1928 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1930 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1931 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1933 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1934 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1936 const int i_dst = p_outpic->p[i_plane].i_pitch;
1937 const int i_src = p_pic->p[i_plane].i_pitch;
1941 for( y = 0; y < i_mby; y++ )
1943 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1944 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1946 #ifdef CAN_COMPILE_MMXEXT
1947 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1948 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1951 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1954 /* Last line (C only)*/
1957 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1958 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1960 for( x = 0; x < i_mbx; x++ )
1962 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1969 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1973 #ifdef CAN_COMPILE_MMXEXT
1974 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1979 /*****************************************************************************
1980 * SendEvents: forward mouse and keyboard events to the parent p_vout
1981 *****************************************************************************/
1982 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
1983 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
1985 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
1986 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
1987 vlc_value_t sentval = newval;
1989 if( !strcmp( psz_var, "mouse-y" ) )
1991 switch( p_vout->p_sys->i_mode )
1993 case DEINTERLACE_MEAN:
1994 case DEINTERLACE_DISCARD:
2000 var_Set( p_vout, psz_var, sentval );
2005 /*****************************************************************************
2006 * FilterCallback: called when changing the deinterlace method on the fly.
2007 *****************************************************************************/
2008 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
2009 vlc_value_t oldval, vlc_value_t newval,
2012 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2013 vout_thread_t * p_vout = (vout_thread_t *)p_this;
2014 int i_old_mode = p_vout->p_sys->i_mode;
2016 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
2018 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
2020 SetFilterMethod( p_vout, newval.psz_string );
2022 switch( p_vout->render.i_chroma )
2024 case VLC_FOURCC('I','4','2','2'):
2025 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2029 case VLC_FOURCC('I','4','2','0'):
2030 case VLC_FOURCC('I','Y','U','V'):
2031 case VLC_FOURCC('Y','V','1','2'):
2032 switch( p_vout->p_sys->i_mode )
2034 case DEINTERLACE_MEAN:
2035 case DEINTERLACE_DISCARD:
2036 if( ( i_old_mode == DEINTERLACE_MEAN )
2037 || ( i_old_mode == DEINTERLACE_DISCARD ) )
2039 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2044 case DEINTERLACE_BOB:
2045 case DEINTERLACE_BLEND:
2046 case DEINTERLACE_LINEAR:
2047 if( ( i_old_mode == DEINTERLACE_BOB )
2048 || ( i_old_mode == DEINTERLACE_BLEND )
2049 || ( i_old_mode == DEINTERLACE_LINEAR ) )
2051 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2062 /* We need to kill the old vout */
2063 if( p_vout->p_sys->p_vout )
2065 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2066 vout_CloseAndRelease( p_vout->p_sys->p_vout );
2069 /* Try to open a new video output */
2070 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
2072 if( p_vout->p_sys->p_vout == NULL )
2074 /* Everything failed */
2075 msg_Err( p_vout, "cannot open vout, aborting" );
2077 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2078 return VLC_EGENERIC;
2081 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
2083 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
2087 /*****************************************************************************
2088 * SendEventsToChild: forward events to the child/children vout
2089 *****************************************************************************/
2090 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
2091 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2093 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
2094 vout_thread_t *p_vout = (vout_thread_t *)p_this;
2095 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
2100 /*****************************************************************************
2101 * video filter2 functions
2102 *****************************************************************************/
2103 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
2105 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2106 picture_t *p_pic_dst;
2108 /* Request output picture */
2109 p_pic_dst = filter_NewPicture( p_filter );
2110 if( p_pic_dst == NULL )
2113 switch( p_vout->p_sys->i_mode )
2115 case DEINTERLACE_DISCARD:
2117 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
2119 msg_Err( p_vout, "discarding lines is not supported yet" );
2120 picture_Release( p_pic_dst );
2124 case DEINTERLACE_BOB:
2126 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
2127 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
2131 case DEINTERLACE_LINEAR:
2133 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
2134 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2136 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2137 picture_Release( p_pic_dst );
2141 case DEINTERLACE_MEAN:
2142 RenderMean( p_vout, p_pic_dst, p_pic );
2145 case DEINTERLACE_BLEND:
2146 RenderBlend( p_vout, p_pic_dst, p_pic );
2150 RenderX( p_pic_dst, p_pic );
2154 picture_CopyProperties( p_pic_dst, p_pic );
2155 p_pic_dst->b_progressive = true;
2157 picture_Release( p_pic );
2161 /*****************************************************************************
2163 *****************************************************************************/
2164 static int OpenFilter( vlc_object_t *p_this )
2166 filter_t *p_filter = (filter_t*)p_this;
2167 vout_thread_t *p_vout;
2170 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
2171 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') &&
2172 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') ) ||
2173 p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
2175 return VLC_EGENERIC;
2178 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2180 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2181 vlc_object_attach( p_vout, p_filter );
2182 p_filter->p_sys = (filter_sys_t *)p_vout;
2183 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2185 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2187 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2188 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
2189 var_Set( p_filter, "deinterlace-mode", val );
2191 if ( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2193 vlc_object_detach( p_vout );
2194 vlc_object_release( p_vout );
2195 return VLC_EGENERIC;
2198 p_filter->pf_video_filter = Deinterlace;
2200 msg_Dbg( p_filter, "deinterlacing" );
2205 /*****************************************************************************
2206 * CloseFilter: clean up the filter
2207 *****************************************************************************/
2208 static void CloseFilter( vlc_object_t *p_this )
2210 filter_t *p_filter = (filter_t*)p_this;
2211 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2213 Destroy( VLC_OBJECT(p_vout) );
2214 vlc_object_detach( p_vout );
2215 vlc_object_release( p_vout );