1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2009 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>
41 #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
56 #define DEINTERLACE_YADIF 7
57 #define DEINTERLACE_YADIF2X 8
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Create ( vlc_object_t * );
63 static void Destroy ( vlc_object_t * );
65 static int Init ( vout_thread_t * );
66 static void End ( vout_thread_t * );
67 static void Render ( vout_thread_t *, picture_t * );
69 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
70 vlc_value_t oldval, vlc_value_t newval, void *p_data );
72 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
73 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
74 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
75 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
76 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
77 static void RenderX ( picture_t *, picture_t * );
78 static void RenderYadif ( vout_thread_t *, picture_t *, picture_t *, int, int );
80 static void MergeGeneric ( void *, const void *, const void *, size_t );
81 #if defined(CAN_COMPILE_C_ALTIVEC)
82 static void MergeAltivec ( void *, const void *, const void *, size_t );
84 #if defined(CAN_COMPILE_MMXEXT)
85 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
87 #if defined(CAN_COMPILE_3DNOW)
88 static void Merge3DNow ( void *, const void *, const void *, size_t );
90 #if defined(CAN_COMPILE_SSE)
91 static void MergeSSE2 ( void *, const void *, const void *, size_t );
93 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
94 static void EndMMX ( void );
96 #if defined(CAN_COMPILE_3DNOW)
97 static void End3DNow ( void );
99 #if defined __ARM_NEON__
100 static void MergeNEON (void *, const void *, const void *, size_t);
103 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method );
104 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout );
106 static int OpenFilter( vlc_object_t *p_this );
107 static void CloseFilter( vlc_object_t *p_this );
109 /*****************************************************************************
110 * Callback prototypes
111 *****************************************************************************/
112 static int FilterCallback( vlc_object_t *, char const *,
113 vlc_value_t, vlc_value_t, void * );
115 /*****************************************************************************
117 *****************************************************************************/
118 #define MODE_TEXT N_("Deinterlace mode")
119 #define MODE_LONGTEXT N_("Deinterlace method to use for local playback.")
121 #define SOUT_MODE_TEXT N_("Streaming deinterlace mode")
122 #define SOUT_MODE_LONGTEXT N_("Deinterlace method to use for streaming.")
124 #define FILTER_CFG_PREFIX "sout-deinterlace-"
126 static const char *const mode_list[] = {
127 "discard", "blend", "mean", "bob", "linear", "x", "yadif", "yadif2x" };
128 static const char *const mode_list_text[] = {
129 N_("Discard"), N_("Blend"), N_("Mean"), N_("Bob"), N_("Linear"), "X", "Yadif", "Yadif (2x)" };
132 set_description( N_("Deinterlacing video filter") )
133 set_shortname( N_("Deinterlace" ))
134 set_capability( "video filter", 0 )
135 set_category( CAT_VIDEO )
136 set_subcategory( SUBCAT_VIDEO_VFILTER )
138 set_section( N_("Display"),NULL)
139 add_string( "filter-deinterlace-mode", "discard", NULL, MODE_TEXT,
140 MODE_LONGTEXT, false )
141 change_string_list( mode_list, mode_list_text, 0 )
144 add_shortcut( "deinterlace" )
145 set_callbacks( Create, Destroy )
148 set_capability( "video filter2", 0 )
149 set_section( N_("Streaming"),NULL)
150 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
151 SOUT_MODE_LONGTEXT, false )
152 change_string_list( mode_list, mode_list_text, 0 )
153 add_shortcut( "deinterlace" )
154 set_callbacks( OpenFilter, CloseFilter )
157 static const char *const ppsz_filter_options[] = {
161 /*****************************************************************************
162 * vout_sys_t: Deinterlace video output method descriptor
163 *****************************************************************************
164 * This structure is part of the video output thread descriptor.
165 * It describes the Deinterlace specific properties of an output thread.
166 *****************************************************************************/
167 #define HISTORY_SIZE (3)
170 int i_mode; /* Deinterlace mode */
171 bool b_double_rate; /* Shall we double the framerate? */
172 bool b_half_height; /* Shall be devide the height by 2 */
177 vout_thread_t *p_vout;
179 vlc_mutex_t filter_lock;
181 void (*pf_merge) ( void *, const void *, const void *, size_t );
182 void (*pf_end_merge) ( void );
185 picture_t *pp_history[HISTORY_SIZE];
188 /*****************************************************************************
189 * Control: control facility for the vout (forwards to child vout)
190 *****************************************************************************/
191 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
193 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
196 /*****************************************************************************
197 * Create: allocates Deinterlace video thread output method
198 *****************************************************************************
199 * This function allocates and initializes a Deinterlace vout method.
200 *****************************************************************************/
201 static int Create( vlc_object_t *p_this )
203 vout_thread_t *p_vout = (vout_thread_t *)p_this;
207 /* Allocate structure */
208 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
209 if( p_vout->p_sys == NULL )
212 p_vout->pf_init = Init;
213 p_vout->pf_end = End;
214 p_vout->pf_manage = NULL;
215 p_vout->pf_render = Render;
216 p_vout->pf_display = NULL;
217 p_vout->pf_control = Control;
219 p_sys->i_mode = DEINTERLACE_DISCARD;
220 p_sys->b_double_rate = false;
221 p_sys->b_half_height = true;
222 p_sys->last_date = 0;
224 vlc_mutex_init( &p_sys->filter_lock );
226 #if defined(CAN_COMPILE_C_ALTIVEC)
227 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
229 p_sys->pf_merge = MergeAltivec;
230 p_sys->pf_end_merge = NULL;
234 #if defined(CAN_COMPILE_SSE)
235 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
237 p_sys->pf_merge = MergeSSE2;
238 p_sys->pf_end_merge = EndMMX;
242 #if defined(CAN_COMPILE_MMXEXT)
243 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
245 p_sys->pf_merge = MergeMMXEXT;
246 p_sys->pf_end_merge = EndMMX;
250 #if defined(CAN_COMPILE_3DNOW)
251 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
253 p_sys->pf_merge = Merge3DNow;
254 p_sys->pf_end_merge = End3DNow;
258 #if defined __ARM_NEON__
259 if( vlc_CPU() & CPU_CAPABILITY_NEON )
261 p_sys->pf_merge = MergeNEON;
262 p_sys->pf_end_merge = NULL;
267 p_sys->pf_merge = MergeGeneric;
268 p_sys->pf_end_merge = NULL;
271 /* Look what method was requested */
272 psz_mode = var_CreateGetString( p_vout, "filter-deinterlace-mode" );
276 msg_Err( p_vout, "configuration variable filter-deinterlace-mode empty" );
277 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
279 psz_mode = strdup( "discard" );
282 SetFilterMethod( p_vout, psz_mode );
289 /*****************************************************************************
290 * SetFilterMethod: setup the deinterlace method to use.
291 *****************************************************************************/
292 static void SetFilterMethod( vout_thread_t *p_vout, const char *psz_method )
294 vout_sys_t *p_sys = p_vout->p_sys;
295 if( !strcmp( psz_method, "mean" ) )
297 p_sys->i_mode = DEINTERLACE_MEAN;
298 p_sys->b_double_rate = false;
299 p_sys->b_half_height = true;
301 else if( !strcmp( psz_method, "blend" )
302 || !strcmp( psz_method, "average" )
303 || !strcmp( psz_method, "combine-fields" ) )
305 p_sys->i_mode = DEINTERLACE_BLEND;
306 p_sys->b_double_rate = false;
307 p_sys->b_half_height = false;
309 else if( !strcmp( psz_method, "bob" )
310 || !strcmp( psz_method, "progressive-scan" ) )
312 p_sys->i_mode = DEINTERLACE_BOB;
313 p_sys->b_double_rate = true;
314 p_sys->b_half_height = false;
316 else if( !strcmp( psz_method, "linear" ) )
318 p_sys->i_mode = DEINTERLACE_LINEAR;
319 p_sys->b_double_rate = true;
320 p_sys->b_half_height = false;
322 else if( !strcmp( psz_method, "x" ) )
324 p_sys->i_mode = DEINTERLACE_X;
325 p_sys->b_double_rate = false;
326 p_sys->b_half_height = false;
328 else if( !strcmp( psz_method, "yadif" ) )
330 p_sys->i_mode = DEINTERLACE_YADIF;
331 p_sys->b_double_rate = false;
332 p_sys->b_half_height = false;
334 else if( !strcmp( psz_method, "yadif2x" ) )
336 p_sys->i_mode = DEINTERLACE_YADIF2X;
337 p_sys->b_double_rate = true;
338 p_sys->b_half_height = false;
342 const bool b_i422 = p_vout->render.i_chroma == VLC_CODEC_I422 ||
343 p_vout->render.i_chroma == VLC_CODEC_J422;
344 if( strcmp( psz_method, "discard" ) )
345 msg_Err( p_vout, "no valid deinterlace mode provided, "
346 "using \"discard\"" );
348 p_sys->i_mode = DEINTERLACE_DISCARD;
349 p_sys->b_double_rate = false;
350 p_sys->b_half_height = !b_i422;
353 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
356 static void GetOutputFormat( vout_thread_t *p_vout,
357 video_format_t *p_dst, const video_format_t *p_src )
361 if( p_vout->p_sys->b_half_height )
363 p_dst->i_height /= 2;
364 p_dst->i_visible_height /= 2;
365 p_dst->i_y_offset /= 2;
366 p_dst->i_sar_den *= 2;
369 if( p_src->i_chroma == VLC_CODEC_I422 ||
370 p_src->i_chroma == VLC_CODEC_J422 )
372 switch( p_vout->p_sys->i_mode )
374 case DEINTERLACE_MEAN:
375 case DEINTERLACE_LINEAR:
377 case DEINTERLACE_YADIF:
378 case DEINTERLACE_YADIF2X:
379 p_dst->i_chroma = p_src->i_chroma;
382 p_dst->i_chroma = p_src->i_chroma == VLC_CODEC_I422 ? VLC_CODEC_I420 :
389 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
391 return i_chroma == VLC_CODEC_I420 ||
392 i_chroma == VLC_CODEC_J420 ||
393 i_chroma == VLC_CODEC_YV12 ||
394 i_chroma == VLC_CODEC_I422 ||
395 i_chroma == VLC_CODEC_J422;
398 /*****************************************************************************
399 * Init: initialize Deinterlace video thread output method
400 *****************************************************************************/
401 static int Init( vout_thread_t *p_vout )
403 I_OUTPUTPICTURES = 0;
405 if( !IsChromaSupported( p_vout->render.i_chroma ) )
406 return VLC_EGENERIC; /* unknown chroma */
408 /* Initialize the output structure, full of directbuffers since we want
409 * the decoder to output directly to our structures. */
410 p_vout->output.i_chroma = p_vout->render.i_chroma;
411 p_vout->output.i_width = p_vout->render.i_width;
412 p_vout->output.i_height = p_vout->render.i_height;
413 p_vout->output.i_aspect = p_vout->render.i_aspect;
414 p_vout->fmt_out = p_vout->fmt_in;
416 /* Try to open the real video output */
417 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
419 if( p_vout->p_sys->p_vout == NULL )
421 /* Everything failed */
422 msg_Err( p_vout, "cannot open vout, aborting" );
427 for( int i = 0; i < HISTORY_SIZE; i++ )
428 p_vout->p_sys->pp_history[i] = NULL;
430 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
432 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
434 var_AddCallback( p_vout, "filter-deinterlace-mode", FilterCallback, NULL );
439 /*****************************************************************************
440 * SpawnRealVout: spawn the real video output.
441 *****************************************************************************/
442 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
444 msg_Dbg( p_vout, "spawning the real video output" );
447 GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
449 return vout_Create( p_vout, &fmt );
452 /*****************************************************************************
453 * End: terminate Deinterlace video thread output method
454 *****************************************************************************/
455 static void End( vout_thread_t *p_vout )
457 vout_sys_t *p_sys = p_vout->p_sys;
459 var_DelCallback( p_vout, "filter-deinterlace-mode", FilterCallback, NULL );
461 for( int i = 0; i < HISTORY_SIZE; i++ )
463 if( p_sys->pp_history[i] )
464 picture_Release( p_sys->pp_history[i] );
469 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
470 vout_CloseAndRelease( p_sys->p_vout );
473 vout_filter_ReleaseDirectBuffers( p_vout );
476 /*****************************************************************************
477 * Destroy: destroy Deinterlace video thread output method
478 *****************************************************************************
479 * Terminate an output method created by DeinterlaceCreateOutputMethod
480 *****************************************************************************/
481 static void Destroy( vlc_object_t *p_this )
483 vout_thread_t *p_vout = (vout_thread_t *)p_this;
484 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
485 free( p_vout->p_sys );
489 * Forward mouse event with proper conversion.
491 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
492 vlc_value_t oldval, vlc_value_t newval, void *p_data )
494 vout_thread_t *p_vout = p_data;
495 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
497 if( !strcmp( psz_var, "mouse-button-down" ) )
498 return var_SetChecked( p_vout, psz_var, VLC_VAR_INTEGER, newval );
500 if( p_vout->p_sys->b_half_height )
501 newval.coords.y *= 2;
502 return var_SetChecked( p_vout, psz_var, VLC_VAR_COORDS, newval );
505 /*****************************************************************************
506 * Render: displays previously rendered output
507 *****************************************************************************
508 * This function send the currently rendered image to Deinterlace image,
509 * waits until it is displayed and switch the two rendering buffers, preparing
511 *****************************************************************************/
512 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
514 vout_sys_t *p_sys = p_vout->p_sys;
515 picture_t *pp_outpic[2];
517 /* FIXME are they needed ? */
518 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
519 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
520 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
521 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
523 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
524 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
525 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
526 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
527 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
528 if( p_vout->p_sys->b_half_height )
530 p_sys->p_vout->fmt_in.i_y_offset /= 2;
531 p_sys->p_vout->fmt_in.i_visible_height /= 2;
534 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
536 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
538 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
539 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
541 video_format_t fmt = p_vout->fmt_out;
542 if( p_vout->p_sys->b_half_height )
544 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
548 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
553 pp_outpic[0] = pp_outpic[1] = NULL;
555 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
557 /* Get a new picture */
558 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
562 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
564 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
567 msleep( VOUT_OUTMEM_SLEEP );
570 pp_outpic[0]->date = p_pic->date;
572 /* If we are using double rate, get an additional new picture */
573 if( p_vout->p_sys->b_double_rate )
575 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
579 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
581 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
582 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
585 msleep( VOUT_OUTMEM_SLEEP );
588 /* 20ms is a bit arbitrary, but it's only for the first image we get */
589 if( !p_vout->p_sys->last_date )
590 pp_outpic[1]->date = p_pic->date + 20000;
592 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
593 p_vout->p_sys->last_date = p_pic->date;
596 switch( p_vout->p_sys->i_mode )
598 case DEINTERLACE_DISCARD:
599 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
600 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
603 case DEINTERLACE_BOB:
604 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
605 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
606 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
607 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
610 case DEINTERLACE_LINEAR:
611 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
612 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
613 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
614 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
617 case DEINTERLACE_MEAN:
618 RenderMean( p_vout, pp_outpic[0], p_pic );
619 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
622 case DEINTERLACE_BLEND:
623 RenderBlend( p_vout, pp_outpic[0], p_pic );
624 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
628 RenderX( pp_outpic[0], p_pic );
629 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
632 case DEINTERLACE_YADIF:
633 RenderYadif( p_vout, pp_outpic[0], p_pic, 0, 0 );
634 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
637 case DEINTERLACE_YADIF2X:
638 RenderYadif( p_vout, pp_outpic[0], p_pic, 0, p_pic->b_top_field_first ? 0 : 1 );
639 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
640 RenderYadif( p_vout, pp_outpic[1], p_pic, 1, p_pic->b_top_field_first ? 1 : 0 );
641 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
644 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
647 /*****************************************************************************
648 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
649 *****************************************************************************/
650 static void RenderDiscard( vout_thread_t *p_vout,
651 picture_t *p_outpic, picture_t *p_pic, int i_field )
655 /* Copy image and skip lines */
656 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
658 uint8_t *p_in, *p_out_end, *p_out;
661 p_in = p_pic->p[i_plane].p_pixels
662 + i_field * p_pic->p[i_plane].i_pitch;
664 p_out = p_outpic->p[i_plane].p_pixels;
665 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
666 * p_outpic->p[i_plane].i_visible_lines;
668 switch( p_vout->render.i_chroma )
674 for( ; p_out < p_out_end ; )
676 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
678 p_out += p_outpic->p[i_plane].i_pitch;
679 p_in += 2 * p_pic->p[i_plane].i_pitch;
686 i_increment = 2 * p_pic->p[i_plane].i_pitch;
688 if( i_plane == Y_PLANE )
690 for( ; p_out < p_out_end ; )
692 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
693 p_out += p_outpic->p[i_plane].i_pitch;
694 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
695 p_out += p_outpic->p[i_plane].i_pitch;
701 for( ; p_out < p_out_end ; )
703 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
704 p_out += p_outpic->p[i_plane].i_pitch;
716 /*****************************************************************************
717 * RenderBob: renders a BOB picture - simple copy
718 *****************************************************************************/
719 static void RenderBob( vout_thread_t *p_vout,
720 picture_t *p_outpic, picture_t *p_pic, int i_field )
724 /* Copy image and skip lines */
725 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
727 uint8_t *p_in, *p_out_end, *p_out;
729 p_in = p_pic->p[i_plane].p_pixels;
730 p_out = p_outpic->p[i_plane].p_pixels;
731 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
732 * p_outpic->p[i_plane].i_visible_lines;
734 switch( p_vout->render.i_chroma )
739 /* For BOTTOM field we need to add the first line */
742 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
743 p_in += p_pic->p[i_plane].i_pitch;
744 p_out += p_outpic->p[i_plane].i_pitch;
747 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
749 for( ; p_out < p_out_end ; )
751 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
753 p_out += p_outpic->p[i_plane].i_pitch;
755 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
757 p_in += 2 * p_pic->p[i_plane].i_pitch;
758 p_out += p_outpic->p[i_plane].i_pitch;
761 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
763 /* For TOP field we need to add the last line */
766 p_in += p_pic->p[i_plane].i_pitch;
767 p_out += p_outpic->p[i_plane].i_pitch;
768 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
774 /* For BOTTOM field we need to add the first line */
777 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
778 p_in += p_pic->p[i_plane].i_pitch;
779 p_out += p_outpic->p[i_plane].i_pitch;
782 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
784 if( i_plane == Y_PLANE )
786 for( ; p_out < p_out_end ; )
788 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
790 p_out += p_outpic->p[i_plane].i_pitch;
792 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
794 p_in += 2 * p_pic->p[i_plane].i_pitch;
795 p_out += p_outpic->p[i_plane].i_pitch;
800 for( ; p_out < p_out_end ; )
802 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
804 p_out += p_outpic->p[i_plane].i_pitch;
805 p_in += 2 * p_pic->p[i_plane].i_pitch;
809 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
811 /* For TOP field we need to add the last line */
814 p_in += p_pic->p[i_plane].i_pitch;
815 p_out += p_outpic->p[i_plane].i_pitch;
816 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
823 #define Merge p_vout->p_sys->pf_merge
824 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
826 /*****************************************************************************
827 * RenderLinear: BOB with linear interpolation
828 *****************************************************************************/
829 static void RenderLinear( vout_thread_t *p_vout,
830 picture_t *p_outpic, picture_t *p_pic, int i_field )
834 /* Copy image and skip lines */
835 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
837 uint8_t *p_in, *p_out_end, *p_out;
839 p_in = p_pic->p[i_plane].p_pixels;
840 p_out = p_outpic->p[i_plane].p_pixels;
841 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
842 * p_outpic->p[i_plane].i_visible_lines;
844 /* For BOTTOM field we need to add the first line */
847 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
848 p_in += p_pic->p[i_plane].i_pitch;
849 p_out += p_outpic->p[i_plane].i_pitch;
852 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
854 for( ; p_out < p_out_end ; )
856 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
858 p_out += p_outpic->p[i_plane].i_pitch;
860 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
861 p_pic->p[i_plane].i_pitch );
863 p_in += 2 * p_pic->p[i_plane].i_pitch;
864 p_out += p_outpic->p[i_plane].i_pitch;
867 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
869 /* For TOP field we need to add the last line */
872 p_in += p_pic->p[i_plane].i_pitch;
873 p_out += p_outpic->p[i_plane].i_pitch;
874 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
880 static void RenderMean( vout_thread_t *p_vout,
881 picture_t *p_outpic, picture_t *p_pic )
885 /* Copy image and skip lines */
886 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
888 uint8_t *p_in, *p_out_end, *p_out;
890 p_in = p_pic->p[i_plane].p_pixels;
892 p_out = p_outpic->p[i_plane].p_pixels;
893 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
894 * p_outpic->p[i_plane].i_visible_lines;
896 /* All lines: mean value */
897 for( ; p_out < p_out_end ; )
899 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
900 p_pic->p[i_plane].i_pitch );
902 p_out += p_outpic->p[i_plane].i_pitch;
903 p_in += 2 * p_pic->p[i_plane].i_pitch;
909 static void RenderBlend( vout_thread_t *p_vout,
910 picture_t *p_outpic, picture_t *p_pic )
914 /* Copy image and skip lines */
915 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
917 uint8_t *p_in, *p_out_end, *p_out;
919 p_in = p_pic->p[i_plane].p_pixels;
921 p_out = p_outpic->p[i_plane].p_pixels;
922 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
923 * p_outpic->p[i_plane].i_visible_lines;
925 switch( p_vout->render.i_chroma )
930 /* First line: simple copy */
931 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
932 p_out += p_outpic->p[i_plane].i_pitch;
934 /* Remaining lines: mean value */
935 for( ; p_out < p_out_end ; )
937 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
938 p_pic->p[i_plane].i_pitch );
940 p_out += p_outpic->p[i_plane].i_pitch;
941 p_in += p_pic->p[i_plane].i_pitch;
947 /* First line: simple copy */
948 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
949 p_out += p_outpic->p[i_plane].i_pitch;
951 /* Remaining lines: mean value */
952 if( i_plane == Y_PLANE )
954 for( ; p_out < p_out_end ; )
956 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
957 p_pic->p[i_plane].i_pitch );
959 p_out += p_outpic->p[i_plane].i_pitch;
960 p_in += p_pic->p[i_plane].i_pitch;
966 for( ; p_out < p_out_end ; )
968 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
969 p_pic->p[i_plane].i_pitch );
971 p_out += p_outpic->p[i_plane].i_pitch;
972 p_in += 2*p_pic->p[i_plane].i_pitch;
983 static void MergeGeneric( void *_p_dest, const void *_p_s1,
984 const void *_p_s2, size_t i_bytes )
986 uint8_t* p_dest = (uint8_t*)_p_dest;
987 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
988 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
989 uint8_t* p_end = p_dest + i_bytes - 8;
991 while( p_dest < p_end )
993 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
994 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
995 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
996 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
997 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
998 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
999 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1000 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1005 while( p_dest < p_end )
1007 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1011 #if defined(CAN_COMPILE_MMXEXT)
1012 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
1015 uint8_t* p_dest = (uint8_t*)_p_dest;
1016 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1017 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1018 uint8_t* p_end = p_dest + i_bytes - 8;
1019 while( p_dest < p_end )
1021 __asm__ __volatile__( "movq %2,%%mm1;"
1023 "movq %%mm1, %0" :"=m" (*p_dest):
1033 while( p_dest < p_end )
1035 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1040 #if defined(CAN_COMPILE_3DNOW)
1041 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
1044 uint8_t* p_dest = (uint8_t*)_p_dest;
1045 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1046 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1047 uint8_t* p_end = p_dest + i_bytes - 8;
1048 while( p_dest < p_end )
1050 __asm__ __volatile__( "movq %2,%%mm1;"
1051 "pavgusb %1, %%mm1;"
1052 "movq %%mm1, %0" :"=m" (*p_dest):
1062 while( p_dest < p_end )
1064 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1069 #if defined(CAN_COMPILE_SSE)
1070 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1073 uint8_t* p_dest = (uint8_t*)_p_dest;
1074 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1075 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1077 while( (uintptr_t)p_s1 % 16 )
1079 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1081 p_end = p_dest + i_bytes - 16;
1082 while( p_dest < p_end )
1084 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1086 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1096 while( p_dest < p_end )
1098 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1103 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1104 static void EndMMX( void )
1106 __asm__ __volatile__( "emms" :: );
1110 #if defined(CAN_COMPILE_3DNOW)
1111 static void End3DNow( void )
1113 __asm__ __volatile__( "femms" :: );
1117 #ifdef CAN_COMPILE_C_ALTIVEC
1118 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1119 const void *_p_s2, size_t i_bytes )
1121 uint8_t *p_dest = (uint8_t *)_p_dest;
1122 uint8_t *p_s1 = (uint8_t *)_p_s1;
1123 uint8_t *p_s2 = (uint8_t *)_p_s2;
1124 uint8_t *p_end = p_dest + i_bytes - 15;
1126 /* Use C until the first 16-bytes aligned destination pixel */
1127 while( (uintptr_t)p_dest & 0xF )
1129 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1132 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1134 /* Unaligned source */
1135 vector unsigned char s1v, s2v, destv;
1136 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1137 vector unsigned char perm1v, perm2v;
1139 perm1v = vec_lvsl( 0, p_s1 );
1140 perm2v = vec_lvsl( 0, p_s2 );
1141 s1oldv = vec_ld( 0, p_s1 );
1142 s2oldv = vec_ld( 0, p_s2 );
1144 while( p_dest < p_end )
1146 s1newv = vec_ld( 16, p_s1 );
1147 s2newv = vec_ld( 16, p_s2 );
1148 s1v = vec_perm( s1oldv, s1newv, perm1v );
1149 s2v = vec_perm( s2oldv, s2newv, perm2v );
1152 destv = vec_avg( s1v, s2v );
1153 vec_st( destv, 0, p_dest );
1162 /* Aligned source */
1163 vector unsigned char s1v, s2v, destv;
1165 while( p_dest < p_end )
1167 s1v = vec_ld( 0, p_s1 );
1168 s2v = vec_ld( 0, p_s2 );
1169 destv = vec_avg( s1v, s2v );
1170 vec_st( destv, 0, p_dest );
1180 while( p_dest < p_end )
1182 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1188 static void MergeNEON (void *restrict out, const void *in1,
1189 const void *in2, size_t n)
1191 uint8_t *outp = out;
1192 const uint8_t *in1p = in1;
1193 const uint8_t *in2p = in2;
1194 size_t mis = ((uintptr_t)outp) & 15;
1198 MergeGeneric (outp, in1p, in2p, mis);
1205 uint8_t *end = outp + (n & ~15);
1207 if ((((uintptr_t)in1p)|((uintptr_t)in2p)) & 15)
1210 "vld1.u8 {q0-q1}, [%[in1]]!\n"
1211 "vld1.u8 {q2-q3}, [%[in2]]!\n"
1212 "vhadd.u8 q4, q0, q2\n"
1213 "vld1.u8 {q6-q7}, [%[in1]]!\n"
1214 "vhadd.u8 q5, q1, q3\n"
1215 "vld1.u8 {q8-q9}, [%[in2]]!\n"
1216 "vhadd.u8 q10, q6, q8\n"
1217 "vhadd.u8 q11, q7, q9\n"
1218 "vst1.u8 {q4-q5}, [%[out],:128]!\n"
1219 "vst1.u8 {q10-q11}, [%[out],:128]!\n"
1220 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1222 : "q0", "q1", "q2", "memory");
1226 "vld1.u8 {q0-q1}, [%[in1],:128]!\n"
1227 "vld1.u8 {q2-q3}, [%[in2],:128]!\n"
1228 "vhadd.u8 q4, q0, q2\n"
1229 "vld1.u8 {q6-q7}, [%[in1],:128]!\n"
1230 "vhadd.u8 q5, q1, q3\n"
1231 "vld1.u8 {q8-q9}, [%[in2],:128]!\n"
1232 "vhadd.u8 q10, q6, q8\n"
1233 "vhadd.u8 q11, q7, q9\n"
1234 "vst1.u8 {q4-q5}, [%[out],:128]!\n"
1235 "vst1.u8 {q10-q11}, [%[out],:128]!\n"
1236 : [out] "+r" (outp), [in1] "+r" (in1p), [in2] "+r" (in2p)
1238 : "q0", "q1", "q2", "memory");
1241 MergeGeneric (outp, in1p, in2p, n);
1245 /*****************************************************************************
1246 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1247 * and apply a process to recreate the bottom field :
1248 * If a 8x8 block is classified as :
1249 * - progressive: it applies a small blend (1,6,1)
1251 * * in the MMX version: we do a ME between the 2 fields, if there is a
1252 * good match we use MC to recreate the bottom field (with a small
1254 * * otherwise: it recreates the bottom field by an edge oriented
1256 *****************************************************************************/
1258 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1259 * XXX: It need to access to 8x10
1260 * We use more than 8 lines to help with scrolling (text)
1261 * (and because XDeint8x8Frame use line 9)
1262 * XXX: smooth/uniform area with noise detection doesn't works well
1263 * but it's not really a problem because they don't have much details anyway
1265 static inline int ssd( int a ) { return a*a; }
1266 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1272 /* Detect interlacing */
1274 for( y = 0; y < 7; y += 2 )
1277 for( x = 0; x < 8; x++ )
1279 fr += ssd(src[ x] - src[1*i_src+x]) +
1280 ssd(src[i_src+x] - src[2*i_src+x]);
1281 ff += ssd(src[ x] - src[2*i_src+x]) +
1282 ssd(src[i_src+x] - src[3*i_src+x]);
1284 if( ff < 6*fr/8 && fr > 32 )
1290 return fc < 1 ? false : true;
1292 #ifdef CAN_COMPILE_MMXEXT
1293 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1300 /* Detect interlacing */
1302 pxor_r2r( mm7, mm7 );
1303 for( y = 0; y < 9; y += 2 )
1306 pxor_r2r( mm5, mm5 );
1307 pxor_r2r( mm6, mm6 );
1308 for( x = 0; x < 8; x+=4 )
1310 movd_m2r( src[ x], mm0 );
1311 movd_m2r( src[1*i_src+x], mm1 );
1312 movd_m2r( src[2*i_src+x], mm2 );
1313 movd_m2r( src[3*i_src+x], mm3 );
1315 punpcklbw_r2r( mm7, mm0 );
1316 punpcklbw_r2r( mm7, mm1 );
1317 punpcklbw_r2r( mm7, mm2 );
1318 punpcklbw_r2r( mm7, mm3 );
1320 movq_r2r( mm0, mm4 );
1322 psubw_r2r( mm1, mm0 );
1323 psubw_r2r( mm2, mm4 );
1325 psubw_r2r( mm1, mm2 );
1326 psubw_r2r( mm1, mm3 );
1328 pmaddwd_r2r( mm0, mm0 );
1329 pmaddwd_r2r( mm4, mm4 );
1330 pmaddwd_r2r( mm2, mm2 );
1331 pmaddwd_r2r( mm3, mm3 );
1332 paddd_r2r( mm0, mm2 );
1333 paddd_r2r( mm4, mm3 );
1334 paddd_r2r( mm2, mm5 );
1335 paddd_r2r( mm3, mm6 );
1338 movq_r2r( mm5, mm0 );
1339 psrlq_i2r( 32, mm0 );
1340 paddd_r2r( mm0, mm5 );
1341 movd_r2m( mm5, fr );
1343 movq_r2r( mm6, mm0 );
1344 psrlq_i2r( 32, mm0 );
1345 paddd_r2r( mm0, mm6 );
1346 movd_r2m( mm6, ff );
1348 if( ff < 6*fr/8 && fr > 32 )
1357 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1358 uint8_t *src1, int i_src1,
1359 uint8_t *src2, int i_src2 )
1364 for( y = 0; y < 8; y += 2 )
1366 memcpy( dst, src1, 8 );
1369 for( x = 0; x < 8; x++ )
1370 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1378 #ifdef CAN_COMPILE_MMXEXT
1379 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1380 uint8_t *src1, int i_src1,
1381 uint8_t *src2, int i_src2 )
1383 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1387 pxor_r2r( mm7, mm7 );
1388 for( y = 0; y < 8; y += 2 )
1390 for( x = 0; x < 8; x +=4 )
1392 movd_m2r( src1[x], mm0 );
1393 movd_r2m( mm0, dst[x] );
1395 movd_m2r( src2[x], mm1 );
1396 movd_m2r( src1[i_src1+x], mm2 );
1398 punpcklbw_r2r( mm7, mm0 );
1399 punpcklbw_r2r( mm7, mm1 );
1400 punpcklbw_r2r( mm7, mm2 );
1401 paddw_r2r( mm1, mm1 );
1402 movq_r2r( mm1, mm3 );
1403 paddw_r2r( mm3, mm3 );
1404 paddw_r2r( mm2, mm0 );
1405 paddw_r2r( mm3, mm1 );
1406 paddw_m2r( m_4, mm1 );
1407 paddw_r2r( mm1, mm0 );
1408 psraw_i2r( 3, mm0 );
1409 packuswb_r2r( mm7, mm0 );
1410 movd_r2m( mm0, dst[i_dst+x] );
1421 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1424 for( y = 0; y < 8; y++ )
1425 memset( &dst[y*i_dst], v, 8 );
1428 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1431 * TODO: a better one for the inner part.
1433 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1434 uint8_t *src, int i_src )
1439 for( y = 0; y < 8; y += 2 )
1441 memcpy( dst, src, 8 );
1444 for( x = 0; x < 8; x++ )
1445 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1450 #ifdef CAN_COMPILE_MMXEXT
1451 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1452 uint8_t *src, int i_src )
1457 for( y = 0; y < 8; y += 2 )
1459 movq_m2r( src[0], mm0 );
1460 movq_r2m( mm0, dst[0] );
1463 movq_m2r( src[2*i_src], mm1 );
1464 pavgb_r2r( mm1, mm0 );
1466 movq_r2m( mm0, dst[0] );
1474 /* XDeint8x8Field: Edge oriented interpolation
1475 * (Need -4 and +5 pixels H, +1 line)
1477 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1478 uint8_t *src, int i_src )
1483 for( y = 0; y < 8; y += 2 )
1485 memcpy( dst, src, 8 );
1488 for( x = 0; x < 8; x++ )
1490 uint8_t *src2 = &src[2*i_src];
1491 /* I use 8 pixels just to match the MMX version, but it's overkill
1492 * 5 would be enough (less isn't good) */
1493 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1494 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1495 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1496 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1498 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1499 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1500 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1501 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1503 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1504 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1505 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1506 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1508 if( c0 < c1 && c1 <= c2 )
1509 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1510 else if( c2 < c1 && c1 <= c0 )
1511 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1513 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1520 #ifdef CAN_COMPILE_MMXEXT
1521 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1522 uint8_t *src, int i_src )
1527 for( y = 0; y < 8; y += 2 )
1529 memcpy( dst, src, 8 );
1532 for( x = 0; x < 8; x++ )
1534 uint8_t *src2 = &src[2*i_src];
1537 movq_m2r( src[x-2], mm0 );
1538 movq_m2r( src[x-3], mm1 );
1539 movq_m2r( src[x-4], mm2 );
1541 psadbw_m2r( src2[x-4], mm0 );
1542 psadbw_m2r( src2[x-3], mm1 );
1543 psadbw_m2r( src2[x-2], mm2 );
1545 movd_r2m( mm0, c2 );
1546 movd_r2m( mm1, c1 );
1547 movd_r2m( mm2, c0 );
1549 if( c0 < c1 && c1 <= c2 )
1550 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1551 else if( c2 < c1 && c1 <= c0 )
1552 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1554 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1563 /* NxN arbitray size (and then only use pixel in the NxN block)
1565 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1566 int i_height, int i_width )
1573 /* Detect interlacing */
1574 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1577 for( y = 0; y < i_height - 2; y += 2 )
1579 const uint8_t *s = &src[y*i_src];
1580 for( x = 0; x < i_width; x++ )
1582 fr += ssd(s[ x] - s[1*i_src+x]);
1583 ff += ssd(s[ x] - s[2*i_src+x]);
1585 if( ff < fr && fr > i_width / 2 )
1589 return fc < 2 ? false : true;
1592 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1593 uint8_t *src, int i_src,
1594 int i_width, int i_height )
1599 for( y = 0; y < i_height; y += 2 )
1601 memcpy( dst, src, i_width );
1604 if( y < i_height - 2 )
1606 for( x = 0; x < i_width; x++ )
1607 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1611 /* Blend last line */
1612 for( x = 0; x < i_width; x++ )
1613 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1620 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1621 uint8_t *src, int i_src,
1622 int i_width, int i_height )
1627 for( y = 0; y < i_height; y += 2 )
1629 memcpy( dst, src, i_width );
1632 if( y < i_height - 2 )
1634 for( x = 0; x < i_width; x++ )
1635 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1639 /* Blend last line */
1640 for( x = 0; x < i_width; x++ )
1641 dst[x] = (src[x] + src[i_src+x]) >> 1;
1648 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1649 int i_width, int i_height )
1651 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1652 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1654 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1658 static inline int median( int a, int b, int c )
1660 int min = a, max =a;
1671 return a + b + c - min - max;
1677 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1678 uint8_t *src, int i_src,
1679 const int i_mbx, int i_modx )
1683 for( x = 0; x < i_mbx; x++ )
1686 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1688 if( x == 0 || x == i_mbx - 1 )
1689 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1691 XDeint8x8FieldC( dst, i_dst, src, i_src );
1695 XDeint8x8MergeC( dst, i_dst,
1696 &src[0*i_src], 2*i_src,
1697 &src[1*i_src], 2*i_src );
1705 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1707 #ifdef CAN_COMPILE_MMXEXT
1708 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1709 uint8_t *src, int i_src,
1710 const int i_mbx, int i_modx )
1714 /* Reset current line */
1715 for( x = 0; x < i_mbx; x++ )
1718 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1720 if( x == 0 || x == i_mbx - 1 )
1721 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1723 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1727 XDeint8x8MergeMMXEXT( dst, i_dst,
1728 &src[0*i_src], 2*i_src,
1729 &src[1*i_src], 2*i_src );
1737 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1741 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1745 /* Copy image and skip lines */
1746 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1748 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1749 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1751 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1752 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1754 const int i_dst = p_outpic->p[i_plane].i_pitch;
1755 const int i_src = p_pic->p[i_plane].i_pitch;
1759 for( y = 0; y < i_mby; y++ )
1761 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1762 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1764 #ifdef CAN_COMPILE_MMXEXT
1765 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1766 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1769 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1772 /* Last line (C only)*/
1775 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1776 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1778 for( x = 0; x < i_mbx; x++ )
1780 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1787 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1791 #ifdef CAN_COMPILE_MMXEXT
1792 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1797 /*****************************************************************************
1798 * Yadif (Yet Another DeInterlacing Filter).
1799 *****************************************************************************/
1803 * 0: Output 1 frame for each frame.
1804 * 1: Output 1 frame for each field.
1805 * 2: Like 0 but skips spatial interlacing check.
1806 * 3: Like 1 but skips spatial interlacing check.
1808 * In vlc, only & 0x02 has meaning, as we do the & 0x01 ourself.
1813 /* I am unsure it is the right one */
1814 typedef intptr_t x86_reg;
1816 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
1817 #define FFMAX(a,b) __MAX(a,b)
1818 #define FFMAX3(a,b,c) FFMAX(FFMAX(a,b),c)
1819 #define FFMIN(a,b) __MIN(a,b)
1820 #define FFMIN3(a,b,c) FFMIN(FFMIN(a,b),c)
1822 /* yadif.h comes from vf_yadif.c of mplayer project */
1825 static void RenderYadif( vout_thread_t *p_vout, picture_t *p_dst, picture_t *p_src, int i_order, int i_field )
1827 vout_sys_t *p_sys = p_vout->p_sys;
1830 assert( i_order == 0 || i_order == 1 );
1831 assert( i_field == 0 || i_field == 1 );
1835 /* Duplicate the picture
1836 * TODO when the vout rework is finished, picture_Hold() might be enough
1837 * but becarefull, the pitches must match */
1838 picture_t *p_dup = picture_NewFromFormat( &p_src->format );
1840 picture_Copy( p_dup, p_src );
1842 /* Slide the history */
1843 if( p_sys->pp_history[0] )
1844 picture_Release( p_sys->pp_history[0] );
1845 for( int i = 1; i < HISTORY_SIZE; i++ )
1846 p_sys->pp_history[i-1] = p_sys->pp_history[i];
1847 p_sys->pp_history[HISTORY_SIZE-1] = p_dup;
1850 /* As the pitches must match, use ONLY pictures coming from picture_New()! */
1851 picture_t *p_prev = p_sys->pp_history[0];
1852 picture_t *p_cur = p_sys->pp_history[1];
1853 picture_t *p_next = p_sys->pp_history[2];
1855 /* Filter if we have all the pictures we need */
1856 if( p_prev && p_cur && p_next )
1859 void (*filter)(struct vf_priv_s *p, uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int refs, int parity);
1860 #if defined(HAVE_YADIF_SSE2)
1861 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
1862 filter = yadif_filter_line_mmx2;
1865 filter = yadif_filter_line_c;
1867 for( int n = 0; n < p_dst->i_planes; n++ )
1869 const plane_t *prevp = &p_prev->p[n];
1870 const plane_t *curp = &p_cur->p[n];
1871 const plane_t *nextp = &p_next->p[n];
1872 plane_t *dstp = &p_dst->p[n];
1874 for( int y = 1; y < dstp->i_visible_lines - 1; y++ )
1876 if( (y % 2) == i_field )
1878 vlc_memcpy( &dstp->p_pixels[y * dstp->i_pitch],
1879 &curp->p_pixels[y * curp->i_pitch], dstp->i_visible_pitch );
1883 struct vf_priv_s cfg;
1884 /* Spatial checks only when enough data */
1885 cfg.mode = (y >= 2 && y < dstp->i_visible_lines - 2) ? 0 : 2;
1887 assert( prevp->i_pitch == curp->i_pitch && curp->i_pitch == nextp->i_pitch );
1889 &dstp->p_pixels[y * dstp->i_pitch],
1890 &prevp->p_pixels[y * prevp->i_pitch],
1891 &curp->p_pixels[y * curp->i_pitch],
1892 &nextp->p_pixels[y * nextp->i_pitch],
1893 dstp->i_visible_pitch,
1895 (i_field ^ (i_order == i_field)) & 1 );
1898 /* We duplicate the first and last lines */
1900 vlc_memcpy(&dstp->p_pixels[(y-1) * dstp->i_pitch], &dstp->p_pixels[y * dstp->i_pitch], dstp->i_pitch);
1901 else if( y == dstp->i_visible_lines - 2 )
1902 vlc_memcpy(&dstp->p_pixels[(y+1) * dstp->i_pitch], &dstp->p_pixels[y * dstp->i_pitch], dstp->i_pitch);
1907 p_dst->date = (p_next->date - p_cur->date) * i_order / 2 + p_cur->date;
1911 /* Fallback to something simple
1912 * XXX it is wrong when we have 2 pictures, we should not output a picture */
1913 RenderX( p_dst, p_src );
1917 /*****************************************************************************
1918 * FilterCallback: called when changing the deinterlace method on the fly.
1919 *****************************************************************************/
1920 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1921 vlc_value_t oldval, vlc_value_t newval,
1924 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1925 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1926 vout_sys_t *p_sys = p_vout->p_sys;
1928 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1930 vlc_mutex_lock( &p_sys->filter_lock );
1931 const bool b_old_half_height = p_sys->b_half_height;
1933 SetFilterMethod( p_vout, newval.psz_string );
1935 if( !b_old_half_height == !p_sys->b_half_height )
1937 vlc_mutex_unlock( &p_sys->filter_lock );
1941 /* We need to kill the old vout */
1944 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
1945 vout_CloseAndRelease( p_sys->p_vout );
1948 /* Try to open a new video output */
1949 p_sys->p_vout = SpawnRealVout( p_vout );
1951 if( p_sys->p_vout == NULL )
1953 /* Everything failed */
1954 msg_Err( p_vout, "cannot open vout, aborting" );
1956 vlc_mutex_unlock( &p_sys->filter_lock );
1957 return VLC_EGENERIC;
1960 vout_filter_AddChild( p_vout, p_sys->p_vout, MouseEvent );
1962 vlc_mutex_unlock( &p_sys->filter_lock );
1966 /*****************************************************************************
1967 * video filter2 functions
1968 *****************************************************************************/
1969 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1971 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1972 picture_t *p_pic_dst;
1974 /* Request output picture */
1975 p_pic_dst = filter_NewPicture( p_filter );
1976 if( p_pic_dst == NULL )
1978 picture_Release( p_pic );
1982 switch( p_vout->p_sys->i_mode )
1984 case DEINTERLACE_DISCARD:
1985 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1988 case DEINTERLACE_BOB:
1990 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1991 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1995 case DEINTERLACE_LINEAR:
1997 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1998 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
2000 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2001 picture_Release( p_pic_dst );
2002 picture_Release( p_pic );
2005 case DEINTERLACE_MEAN:
2006 RenderMean( p_vout, p_pic_dst, p_pic );
2009 case DEINTERLACE_BLEND:
2010 RenderBlend( p_vout, p_pic_dst, p_pic );
2014 RenderX( p_pic_dst, p_pic );
2017 case DEINTERLACE_YADIF:
2018 msg_Err( p_vout, "delaying frames is not supported yet" );
2019 picture_Release( p_pic_dst );
2020 picture_Release( p_pic );
2023 case DEINTERLACE_YADIF2X:
2024 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
2025 picture_Release( p_pic_dst );
2026 picture_Release( p_pic );
2030 picture_CopyProperties( p_pic_dst, p_pic );
2031 p_pic_dst->b_progressive = true;
2033 picture_Release( p_pic );
2037 /*****************************************************************************
2039 *****************************************************************************/
2040 static int OpenFilter( vlc_object_t *p_this )
2042 filter_t *p_filter = (filter_t*)p_this;
2043 vout_thread_t *p_vout;
2046 if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
2047 return VLC_EGENERIC;
2049 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
2051 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
2052 vlc_object_attach( p_vout, p_filter );
2053 p_filter->p_sys = (filter_sys_t *)p_vout;
2054 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
2056 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
2058 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
2060 var_Create( p_filter, "filter-deinterlace-mode", VLC_VAR_STRING );
2061 var_Set( p_filter, "filter-deinterlace-mode", val );
2062 free( val.psz_string );
2064 if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
2066 vlc_object_release( p_vout );
2067 return VLC_EGENERIC;
2071 GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
2072 if( !p_filter->b_allow_fmt_out_change &&
2073 ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
2074 fmt.i_height != p_filter->fmt_in.video.i_height ) )
2076 CloseFilter( VLC_OBJECT(p_filter) );
2077 return VLC_EGENERIC;
2079 p_filter->fmt_out.video = fmt;
2080 p_filter->fmt_out.i_codec = fmt.i_chroma;
2081 p_filter->pf_video_filter = Deinterlace;
2083 msg_Dbg( p_filter, "deinterlacing" );
2088 /*****************************************************************************
2089 * CloseFilter: clean up the filter
2090 *****************************************************************************/
2091 static void CloseFilter( vlc_object_t *p_this )
2093 filter_t *p_filter = (filter_t*)p_this;
2094 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
2096 Destroy( VLC_OBJECT(p_vout) );
2097 vlc_object_release( p_vout );