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>
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 int MouseEvent( vlc_object_t *p_this, char const *psz_var,
68 vlc_value_t oldval, vlc_value_t newval, void *p_data );
70 static void RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
71 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
72 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
73 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
74 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
75 static void RenderX ( picture_t *, picture_t * );
77 static void MergeGeneric ( void *, const void *, const void *, size_t );
78 #if defined(CAN_COMPILE_C_ALTIVEC)
79 static void MergeAltivec ( void *, const void *, const void *, size_t );
81 #if defined(CAN_COMPILE_MMXEXT)
82 static void MergeMMXEXT ( void *, const void *, const void *, size_t );
84 #if defined(CAN_COMPILE_3DNOW)
85 static void Merge3DNow ( void *, const void *, const void *, size_t );
87 #if defined(CAN_COMPILE_SSE)
88 static void MergeSSE2 ( void *, const void *, const void *, size_t );
90 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
91 static void EndMMX ( void );
93 #if defined(CAN_COMPILE_3DNOW)
94 static void End3DNow ( 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 )
138 add_shortcut( "deinterlace" )
139 set_callbacks( Create, Destroy )
142 set_capability( "video filter2", 0 )
143 set_section( N_("Streaming"),NULL)
144 add_string( FILTER_CFG_PREFIX "mode", "blend", NULL, SOUT_MODE_TEXT,
145 SOUT_MODE_LONGTEXT, false )
146 change_string_list( mode_list, mode_list_text, 0 )
147 add_shortcut( "deinterlace" )
148 set_callbacks( OpenFilter, CloseFilter )
151 static const char *const ppsz_filter_options[] = {
155 /*****************************************************************************
156 * vout_sys_t: Deinterlace video output method descriptor
157 *****************************************************************************
158 * This structure is part of the video output thread descriptor.
159 * It describes the Deinterlace specific properties of an output thread.
160 *****************************************************************************/
163 int i_mode; /* Deinterlace mode */
164 bool b_double_rate; /* Shall we double the framerate? */
165 bool b_half_height; /* Shall be devide the height by 2 */
170 vout_thread_t *p_vout;
172 vlc_mutex_t filter_lock;
174 void (*pf_merge) ( void *, const void *, const void *, size_t );
175 void (*pf_end_merge) ( void );
178 /*****************************************************************************
179 * Control: control facility for the vout (forwards to child vout)
180 *****************************************************************************/
181 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
183 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
186 /*****************************************************************************
187 * Create: allocates Deinterlace video thread output method
188 *****************************************************************************
189 * This function allocates and initializes a Deinterlace vout method.
190 *****************************************************************************/
191 static int Create( vlc_object_t *p_this )
193 vout_thread_t *p_vout = (vout_thread_t *)p_this;
197 /* Allocate structure */
198 p_sys = p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
199 if( p_vout->p_sys == NULL )
202 p_vout->pf_init = Init;
203 p_vout->pf_end = End;
204 p_vout->pf_manage = NULL;
205 p_vout->pf_render = Render;
206 p_vout->pf_display = NULL;
207 p_vout->pf_control = Control;
209 p_sys->i_mode = DEINTERLACE_DISCARD;
210 p_sys->b_double_rate = false;
211 p_sys->b_half_height = true;
212 p_sys->last_date = 0;
214 vlc_mutex_init( &p_sys->filter_lock );
216 #if defined(CAN_COMPILE_C_ALTIVEC)
217 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
219 p_sys->pf_merge = MergeAltivec;
220 p_sys->pf_end_merge = NULL;
224 #if defined(CAN_COMPILE_SSE)
225 if( vlc_CPU() & CPU_CAPABILITY_SSE2 )
227 p_sys->pf_merge = MergeSSE2;
228 p_sys->pf_end_merge = EndMMX;
232 #if defined(CAN_COMPILE_MMXEXT)
233 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
235 p_sys->pf_merge = MergeMMXEXT;
236 p_sys->pf_end_merge = EndMMX;
240 #if defined(CAN_COMPILE_3DNOW)
241 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
243 p_sys->pf_merge = Merge3DNow;
244 p_sys->pf_end_merge = End3DNow;
249 p_sys->pf_merge = MergeGeneric;
250 p_sys->pf_end_merge = NULL;
253 /* Look what method was requested */
254 var_Create( p_vout, "deinterlace-mode", VLC_VAR_STRING );
255 var_Change( p_vout, "deinterlace-mode", VLC_VAR_INHERITVALUE, &val, NULL );
257 if( val.psz_string == NULL )
259 msg_Err( p_vout, "configuration variable deinterlace-mode empty" );
260 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
262 val.psz_string = strdup( "discard" );
265 msg_Dbg( p_vout, "using %s deinterlace mode", val.psz_string );
267 SetFilterMethod( p_vout, val.psz_string );
269 free( val.psz_string );
274 /*****************************************************************************
275 * SetFilterMethod: setup the deinterlace method to use.
276 *****************************************************************************/
277 static void SetFilterMethod( vout_thread_t *p_vout, char *psz_method )
279 vout_sys_t *p_sys = p_vout->p_sys;
280 if( !strcmp( psz_method, "mean" ) )
282 p_sys->i_mode = DEINTERLACE_MEAN;
283 p_sys->b_double_rate = false;
284 p_sys->b_half_height = true;
286 else if( !strcmp( psz_method, "blend" )
287 || !strcmp( psz_method, "average" )
288 || !strcmp( psz_method, "combine-fields" ) )
290 p_sys->i_mode = DEINTERLACE_BLEND;
291 p_sys->b_double_rate = false;
292 p_sys->b_half_height = false;
294 else if( !strcmp( psz_method, "bob" )
295 || !strcmp( psz_method, "progressive-scan" ) )
297 p_sys->i_mode = DEINTERLACE_BOB;
298 p_sys->b_double_rate = true;
299 p_sys->b_half_height = false;
301 else if( !strcmp( psz_method, "linear" ) )
303 p_sys->i_mode = DEINTERLACE_LINEAR;
304 p_sys->b_double_rate = true;
305 p_sys->b_half_height = false;
307 else if( !strcmp( psz_method, "x" ) )
309 p_sys->i_mode = DEINTERLACE_X;
310 p_sys->b_double_rate = false;
311 p_sys->b_half_height = false;
315 const bool b_i422 = p_vout->render.i_chroma == VLC_CODEC_I422;
316 if( strcmp( psz_method, "discard" ) )
317 msg_Err( p_vout, "no valid deinterlace mode provided, "
318 "using \"discard\"" );
320 p_sys->i_mode = DEINTERLACE_DISCARD;
321 p_sys->b_double_rate = false;
322 p_sys->b_half_height = !b_i422;
325 msg_Dbg( p_vout, "using %s deinterlace method", psz_method );
328 static void GetOutputFormat( vout_thread_t *p_vout,
329 video_format_t *p_dst, const video_format_t *p_src )
333 if( p_vout->p_sys->b_half_height )
335 p_dst->i_height /= 2;
336 p_dst->i_visible_height /= 2;
337 p_dst->i_y_offset /= 2;
338 p_dst->i_sar_den *= 2;
341 if( p_src->i_chroma == VLC_CODEC_I422 )
343 switch( p_vout->p_sys->i_mode )
345 case DEINTERLACE_MEAN:
346 case DEINTERLACE_LINEAR:
348 p_dst->i_chroma = VLC_CODEC_I422;
351 p_dst->i_chroma = VLC_CODEC_I420;
357 static bool IsChromaSupported( vlc_fourcc_t i_chroma )
359 return i_chroma == VLC_CODEC_I420 ||
360 i_chroma == VLC_CODEC_YV12 ||
361 i_chroma == VLC_CODEC_I422;
364 /*****************************************************************************
365 * Init: initialize Deinterlace video thread output method
366 *****************************************************************************/
367 static int Init( vout_thread_t *p_vout )
369 I_OUTPUTPICTURES = 0;
371 if( !IsChromaSupported( p_vout->render.i_chroma ) )
372 return VLC_EGENERIC; /* unknown chroma */
374 /* Initialize the output structure, full of directbuffers since we want
375 * the decoder to output directly to our structures. */
376 p_vout->output.i_chroma = p_vout->render.i_chroma;
377 p_vout->output.i_width = p_vout->render.i_width;
378 p_vout->output.i_height = p_vout->render.i_height;
379 p_vout->output.i_aspect = p_vout->render.i_aspect;
380 p_vout->fmt_out = p_vout->fmt_in;
382 /* Try to open the real video output */
383 p_vout->p_sys->p_vout = SpawnRealVout( p_vout );
385 if( p_vout->p_sys->p_vout == NULL )
387 /* Everything failed */
388 msg_Err( p_vout, "cannot open vout, aborting" );
393 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
395 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
397 var_AddCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
402 /*****************************************************************************
403 * SpawnRealVout: spawn the real video output.
404 *****************************************************************************/
405 static vout_thread_t *SpawnRealVout( vout_thread_t *p_vout )
407 msg_Dbg( p_vout, "spawning the real video output" );
410 GetOutputFormat( p_vout, &fmt, &p_vout->fmt_out );
412 return vout_Create( p_vout, &fmt );
415 /*****************************************************************************
416 * End: terminate Deinterlace video thread output method
417 *****************************************************************************/
418 static void End( vout_thread_t *p_vout )
420 vout_sys_t *p_sys = p_vout->p_sys;
422 var_DelCallback( p_vout, "deinterlace-mode", FilterCallback, NULL );
426 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
427 vout_CloseAndRelease( p_sys->p_vout );
430 vout_filter_ReleaseDirectBuffers( p_vout );
433 /*****************************************************************************
434 * Destroy: destroy Deinterlace video thread output method
435 *****************************************************************************
436 * Terminate an output method created by DeinterlaceCreateOutputMethod
437 *****************************************************************************/
438 static void Destroy( vlc_object_t *p_this )
440 vout_thread_t *p_vout = (vout_thread_t *)p_this;
441 vlc_mutex_destroy( &p_vout->p_sys->filter_lock );
442 free( p_vout->p_sys );
446 * Forward mouse event with proper conversion.
448 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
449 vlc_value_t oldval, vlc_value_t newval, void *p_data )
451 vout_thread_t *p_vout = p_data;
452 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
454 if( !strcmp( psz_var, "mouse-y" ) && p_vout->p_sys->b_half_height )
457 return var_Set( p_vout, psz_var, newval );
460 /*****************************************************************************
461 * Render: displays previously rendered output
462 *****************************************************************************
463 * This function send the currently rendered image to Deinterlace image,
464 * waits until it is displayed and switch the two rendering buffers, preparing
466 *****************************************************************************/
467 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
469 vout_sys_t *p_sys = p_vout->p_sys;
470 picture_t *pp_outpic[2];
472 /* FIXME are they needed ? */
473 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
474 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
475 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
476 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
478 /* FIXME p_sys->p_vout->* should NOT be changed FIXME */
479 p_sys->p_vout->fmt_in.i_x_offset = p_vout->fmt_out.i_x_offset;
480 p_sys->p_vout->fmt_in.i_y_offset = p_vout->fmt_out.i_y_offset;
481 p_sys->p_vout->fmt_in.i_visible_width = p_vout->fmt_out.i_visible_width;
482 p_sys->p_vout->fmt_in.i_visible_height = p_vout->fmt_in.i_visible_height;
483 if( p_vout->p_sys->b_half_height )
485 p_sys->p_vout->fmt_in.i_y_offset /= 2;
486 p_sys->p_vout->fmt_in.i_visible_height /= 2;
489 if( p_vout->i_changes & VOUT_ASPECT_CHANGE )
491 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
493 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
494 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
495 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
497 video_format_t fmt = p_vout->fmt_out;
498 if( p_vout->p_sys->b_half_height )
500 fmt.i_height /= 2; fmt.i_visible_height /= 2; fmt.i_y_offset /= 2;
504 p_sys->p_vout = vout_Request( p_vout, p_sys->p_vout, &fmt );
509 pp_outpic[0] = pp_outpic[1] = NULL;
511 vlc_mutex_lock( &p_vout->p_sys->filter_lock );
513 /* Get a new picture */
514 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
518 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
520 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
523 msleep( VOUT_OUTMEM_SLEEP );
526 pp_outpic[0]->date = p_pic->date;
528 /* If we are using double rate, get an additional new picture */
529 if( p_vout->p_sys->b_double_rate )
531 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
535 if( !vlc_object_alive( p_vout ) || p_vout->b_error )
537 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
538 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
541 msleep( VOUT_OUTMEM_SLEEP );
544 /* 20ms is a bit arbitrary, but it's only for the first image we get */
545 if( !p_vout->p_sys->last_date )
546 pp_outpic[1]->date = p_pic->date + 20000;
548 pp_outpic[1]->date = (3 * p_pic->date - p_vout->p_sys->last_date) / 2;
549 p_vout->p_sys->last_date = p_pic->date;
552 switch( p_vout->p_sys->i_mode )
554 case DEINTERLACE_DISCARD:
555 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
556 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
559 case DEINTERLACE_BOB:
560 RenderBob( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
561 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
562 RenderBob( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
563 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
566 case DEINTERLACE_LINEAR:
567 RenderLinear( p_vout, pp_outpic[0], p_pic, p_pic->b_top_field_first ? 0 : 1 );
568 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
569 RenderLinear( p_vout, pp_outpic[1], p_pic, p_pic->b_top_field_first ? 1 : 0 );
570 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
573 case DEINTERLACE_MEAN:
574 RenderMean( p_vout, pp_outpic[0], p_pic );
575 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
578 case DEINTERLACE_BLEND:
579 RenderBlend( p_vout, pp_outpic[0], p_pic );
580 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
584 RenderX( pp_outpic[0], p_pic );
585 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
588 vlc_mutex_unlock( &p_vout->p_sys->filter_lock );
591 /*****************************************************************************
592 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
593 *****************************************************************************/
594 static void RenderDiscard( vout_thread_t *p_vout,
595 picture_t *p_outpic, picture_t *p_pic, int i_field )
599 /* Copy image and skip lines */
600 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
602 uint8_t *p_in, *p_out_end, *p_out;
605 p_in = p_pic->p[i_plane].p_pixels
606 + i_field * p_pic->p[i_plane].i_pitch;
608 p_out = p_outpic->p[i_plane].p_pixels;
609 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
610 * p_outpic->p[i_plane].i_visible_lines;
612 switch( p_vout->render.i_chroma )
617 for( ; p_out < p_out_end ; )
619 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
621 p_out += p_outpic->p[i_plane].i_pitch;
622 p_in += 2 * p_pic->p[i_plane].i_pitch;
628 i_increment = 2 * p_pic->p[i_plane].i_pitch;
630 if( i_plane == Y_PLANE )
632 for( ; p_out < p_out_end ; )
634 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
635 p_out += p_outpic->p[i_plane].i_pitch;
636 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
637 p_out += p_outpic->p[i_plane].i_pitch;
643 for( ; p_out < p_out_end ; )
645 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
646 p_out += p_outpic->p[i_plane].i_pitch;
658 /*****************************************************************************
659 * RenderBob: renders a BOB picture - simple copy
660 *****************************************************************************/
661 static void RenderBob( vout_thread_t *p_vout,
662 picture_t *p_outpic, picture_t *p_pic, int i_field )
666 /* Copy image and skip lines */
667 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
669 uint8_t *p_in, *p_out_end, *p_out;
671 p_in = p_pic->p[i_plane].p_pixels;
672 p_out = p_outpic->p[i_plane].p_pixels;
673 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
674 * p_outpic->p[i_plane].i_visible_lines;
676 switch( p_vout->render.i_chroma )
680 /* For BOTTOM field we need to add the first line */
683 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
684 p_in += p_pic->p[i_plane].i_pitch;
685 p_out += p_outpic->p[i_plane].i_pitch;
688 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
690 for( ; p_out < p_out_end ; )
692 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
694 p_out += p_outpic->p[i_plane].i_pitch;
696 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
698 p_in += 2 * p_pic->p[i_plane].i_pitch;
699 p_out += p_outpic->p[i_plane].i_pitch;
702 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
704 /* For TOP field we need to add the last line */
707 p_in += p_pic->p[i_plane].i_pitch;
708 p_out += p_outpic->p[i_plane].i_pitch;
709 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
714 /* For BOTTOM field we need to add the first line */
717 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
718 p_in += p_pic->p[i_plane].i_pitch;
719 p_out += p_outpic->p[i_plane].i_pitch;
722 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
724 if( i_plane == Y_PLANE )
726 for( ; p_out < p_out_end ; )
728 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
730 p_out += p_outpic->p[i_plane].i_pitch;
732 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
734 p_in += 2 * p_pic->p[i_plane].i_pitch;
735 p_out += p_outpic->p[i_plane].i_pitch;
740 for( ; p_out < p_out_end ; )
742 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
744 p_out += p_outpic->p[i_plane].i_pitch;
745 p_in += 2 * p_pic->p[i_plane].i_pitch;
749 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
751 /* For TOP field we need to add the last line */
754 p_in += p_pic->p[i_plane].i_pitch;
755 p_out += p_outpic->p[i_plane].i_pitch;
756 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
763 #define Merge p_vout->p_sys->pf_merge
764 #define EndMerge if(p_vout->p_sys->pf_end_merge) p_vout->p_sys->pf_end_merge
766 /*****************************************************************************
767 * RenderLinear: BOB with linear interpolation
768 *****************************************************************************/
769 static void RenderLinear( vout_thread_t *p_vout,
770 picture_t *p_outpic, picture_t *p_pic, int i_field )
774 /* Copy image and skip lines */
775 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
777 uint8_t *p_in, *p_out_end, *p_out;
779 p_in = p_pic->p[i_plane].p_pixels;
780 p_out = p_outpic->p[i_plane].p_pixels;
781 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
782 * p_outpic->p[i_plane].i_visible_lines;
784 /* For BOTTOM field we need to add the first line */
787 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
788 p_in += p_pic->p[i_plane].i_pitch;
789 p_out += p_outpic->p[i_plane].i_pitch;
792 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
794 for( ; p_out < p_out_end ; )
796 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
798 p_out += p_outpic->p[i_plane].i_pitch;
800 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
801 p_pic->p[i_plane].i_pitch );
803 p_in += 2 * p_pic->p[i_plane].i_pitch;
804 p_out += p_outpic->p[i_plane].i_pitch;
807 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
809 /* For TOP field we need to add the last line */
812 p_in += p_pic->p[i_plane].i_pitch;
813 p_out += p_outpic->p[i_plane].i_pitch;
814 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
820 static void RenderMean( vout_thread_t *p_vout,
821 picture_t *p_outpic, picture_t *p_pic )
825 /* Copy image and skip lines */
826 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
828 uint8_t *p_in, *p_out_end, *p_out;
830 p_in = p_pic->p[i_plane].p_pixels;
832 p_out = p_outpic->p[i_plane].p_pixels;
833 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
834 * p_outpic->p[i_plane].i_visible_lines;
836 /* All lines: mean value */
837 for( ; p_out < p_out_end ; )
839 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
840 p_pic->p[i_plane].i_pitch );
842 p_out += p_outpic->p[i_plane].i_pitch;
843 p_in += 2 * p_pic->p[i_plane].i_pitch;
849 static void RenderBlend( vout_thread_t *p_vout,
850 picture_t *p_outpic, picture_t *p_pic )
854 /* Copy image and skip lines */
855 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
857 uint8_t *p_in, *p_out_end, *p_out;
859 p_in = p_pic->p[i_plane].p_pixels;
861 p_out = p_outpic->p[i_plane].p_pixels;
862 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
863 * p_outpic->p[i_plane].i_visible_lines;
865 switch( p_vout->render.i_chroma )
869 /* First line: simple copy */
870 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
871 p_out += p_outpic->p[i_plane].i_pitch;
873 /* Remaining lines: mean value */
874 for( ; p_out < p_out_end ; )
876 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
877 p_pic->p[i_plane].i_pitch );
879 p_out += p_outpic->p[i_plane].i_pitch;
880 p_in += p_pic->p[i_plane].i_pitch;
885 /* First line: simple copy */
886 vlc_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
887 p_out += p_outpic->p[i_plane].i_pitch;
889 /* Remaining lines: mean value */
890 if( i_plane == Y_PLANE )
892 for( ; p_out < p_out_end ; )
894 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
895 p_pic->p[i_plane].i_pitch );
897 p_out += p_outpic->p[i_plane].i_pitch;
898 p_in += p_pic->p[i_plane].i_pitch;
904 for( ; p_out < p_out_end ; )
906 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
907 p_pic->p[i_plane].i_pitch );
909 p_out += p_outpic->p[i_plane].i_pitch;
910 p_in += 2*p_pic->p[i_plane].i_pitch;
921 static void MergeGeneric( void *_p_dest, const void *_p_s1,
922 const void *_p_s2, size_t i_bytes )
924 uint8_t* p_dest = (uint8_t*)_p_dest;
925 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
926 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
927 uint8_t* p_end = p_dest + i_bytes - 8;
929 while( p_dest < p_end )
931 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
932 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
933 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
934 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
935 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
936 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
937 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
938 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
943 while( p_dest < p_end )
945 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
949 #if defined(CAN_COMPILE_MMXEXT)
950 static void MergeMMXEXT( void *_p_dest, const void *_p_s1, const void *_p_s2,
953 uint8_t* p_dest = (uint8_t*)_p_dest;
954 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
955 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
956 uint8_t* p_end = p_dest + i_bytes - 8;
957 while( p_dest < p_end )
959 __asm__ __volatile__( "movq %2,%%mm1;"
961 "movq %%mm1, %0" :"=m" (*p_dest):
971 while( p_dest < p_end )
973 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
978 #if defined(CAN_COMPILE_3DNOW)
979 static void Merge3DNow( void *_p_dest, const void *_p_s1, const void *_p_s2,
982 uint8_t* p_dest = (uint8_t*)_p_dest;
983 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
984 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
985 uint8_t* p_end = p_dest + i_bytes - 8;
986 while( p_dest < p_end )
988 __asm__ __volatile__( "movq %2,%%mm1;"
990 "movq %%mm1, %0" :"=m" (*p_dest):
1000 while( p_dest < p_end )
1002 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1007 #if defined(CAN_COMPILE_SSE)
1008 static void MergeSSE2( void *_p_dest, const void *_p_s1, const void *_p_s2,
1011 uint8_t* p_dest = (uint8_t*)_p_dest;
1012 const uint8_t *p_s1 = (const uint8_t *)_p_s1;
1013 const uint8_t *p_s2 = (const uint8_t *)_p_s2;
1015 while( (uintptr_t)p_s1 % 16 )
1017 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1019 p_end = p_dest + i_bytes - 16;
1020 while( p_dest < p_end )
1022 __asm__ __volatile__( "movdqu %2,%%xmm1;"
1024 "movdqu %%xmm1, %0" :"=m" (*p_dest):
1034 while( p_dest < p_end )
1036 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1041 #if defined(CAN_COMPILE_MMXEXT) || defined(CAN_COMPILE_SSE)
1042 static void EndMMX( void )
1044 __asm__ __volatile__( "emms" :: );
1048 #if defined(CAN_COMPILE_3DNOW)
1049 static void End3DNow( void )
1051 __asm__ __volatile__( "femms" :: );
1055 #ifdef CAN_COMPILE_C_ALTIVEC
1056 static void MergeAltivec( void *_p_dest, const void *_p_s1,
1057 const void *_p_s2, size_t i_bytes )
1059 uint8_t *p_dest = (uint8_t *)_p_dest;
1060 uint8_t *p_s1 = (uint8_t *)_p_s1;
1061 uint8_t *p_s2 = (uint8_t *)_p_s2;
1062 uint8_t *p_end = p_dest + i_bytes - 15;
1064 /* Use C until the first 16-bytes aligned destination pixel */
1065 while( (int)p_dest & 0xF )
1067 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1070 if( ( (int)p_s1 & 0xF ) | ( (int)p_s2 & 0xF ) )
1072 /* Unaligned source */
1073 vector unsigned char s1v, s2v, destv;
1074 vector unsigned char s1oldv, s2oldv, s1newv, s2newv;
1075 vector unsigned char perm1v, perm2v;
1077 perm1v = vec_lvsl( 0, p_s1 );
1078 perm2v = vec_lvsl( 0, p_s2 );
1079 s1oldv = vec_ld( 0, p_s1 );
1080 s2oldv = vec_ld( 0, p_s2 );
1082 while( p_dest < p_end )
1084 s1newv = vec_ld( 16, p_s1 );
1085 s2newv = vec_ld( 16, p_s2 );
1086 s1v = vec_perm( s1oldv, s1newv, perm1v );
1087 s2v = vec_perm( s2oldv, s2newv, perm2v );
1090 destv = vec_avg( s1v, s2v );
1091 vec_st( destv, 0, p_dest );
1100 /* Aligned source */
1101 vector unsigned char s1v, s2v, destv;
1103 while( p_dest < p_end )
1105 s1v = vec_ld( 0, p_s1 );
1106 s2v = vec_ld( 0, p_s2 );
1107 destv = vec_avg( s1v, s2v );
1108 vec_st( destv, 0, p_dest );
1118 while( p_dest < p_end )
1120 *p_dest++ = ( (uint16_t)(*p_s1++) + (uint16_t)(*p_s2++) ) >> 1;
1125 /*****************************************************************************
1126 * RenderX: This algo works on a 8x8 block basic, it copies the top field
1127 * and apply a process to recreate the bottom field :
1128 * If a 8x8 block is classified as :
1129 * - progressive: it applies a small blend (1,6,1)
1131 * * in the MMX version: we do a ME between the 2 fields, if there is a
1132 * good match we use MC to recreate the bottom field (with a small
1134 * * otherwise: it recreates the bottom field by an edge oriented
1136 *****************************************************************************/
1138 /* XDeint8x8Detect: detect if a 8x8 block is interlaced.
1139 * XXX: It need to access to 8x10
1140 * We use more than 8 lines to help with scrolling (text)
1141 * (and because XDeint8x8Frame use line 9)
1142 * XXX: smooth/uniform area with noise detection doesn't works well
1143 * but it's not really a problem because they don't have much details anyway
1145 static inline int ssd( int a ) { return a*a; }
1146 static inline int XDeint8x8DetectC( uint8_t *src, int i_src )
1152 /* Detect interlacing */
1154 for( y = 0; y < 7; y += 2 )
1157 for( x = 0; x < 8; x++ )
1159 fr += ssd(src[ x] - src[1*i_src+x]) +
1160 ssd(src[i_src+x] - src[2*i_src+x]);
1161 ff += ssd(src[ x] - src[2*i_src+x]) +
1162 ssd(src[i_src+x] - src[3*i_src+x]);
1164 if( ff < 6*fr/8 && fr > 32 )
1170 return fc < 1 ? false : true;
1172 #ifdef CAN_COMPILE_MMXEXT
1173 static inline int XDeint8x8DetectMMXEXT( uint8_t *src, int i_src )
1180 /* Detect interlacing */
1182 pxor_r2r( mm7, mm7 );
1183 for( y = 0; y < 9; y += 2 )
1186 pxor_r2r( mm5, mm5 );
1187 pxor_r2r( mm6, mm6 );
1188 for( x = 0; x < 8; x+=4 )
1190 movd_m2r( src[ x], mm0 );
1191 movd_m2r( src[1*i_src+x], mm1 );
1192 movd_m2r( src[2*i_src+x], mm2 );
1193 movd_m2r( src[3*i_src+x], mm3 );
1195 punpcklbw_r2r( mm7, mm0 );
1196 punpcklbw_r2r( mm7, mm1 );
1197 punpcklbw_r2r( mm7, mm2 );
1198 punpcklbw_r2r( mm7, mm3 );
1200 movq_r2r( mm0, mm4 );
1202 psubw_r2r( mm1, mm0 );
1203 psubw_r2r( mm2, mm4 );
1205 psubw_r2r( mm1, mm2 );
1206 psubw_r2r( mm1, mm3 );
1208 pmaddwd_r2r( mm0, mm0 );
1209 pmaddwd_r2r( mm4, mm4 );
1210 pmaddwd_r2r( mm2, mm2 );
1211 pmaddwd_r2r( mm3, mm3 );
1212 paddd_r2r( mm0, mm2 );
1213 paddd_r2r( mm4, mm3 );
1214 paddd_r2r( mm2, mm5 );
1215 paddd_r2r( mm3, mm6 );
1218 movq_r2r( mm5, mm0 );
1219 psrlq_i2r( 32, mm0 );
1220 paddd_r2r( mm0, mm5 );
1221 movd_r2m( mm5, fr );
1223 movq_r2r( mm6, mm0 );
1224 psrlq_i2r( 32, mm0 );
1225 paddd_r2r( mm0, mm6 );
1226 movd_r2m( mm6, ff );
1228 if( ff < 6*fr/8 && fr > 32 )
1237 static inline void XDeint8x8MergeC( uint8_t *dst, int i_dst,
1238 uint8_t *src1, int i_src1,
1239 uint8_t *src2, int i_src2 )
1244 for( y = 0; y < 8; y += 2 )
1246 memcpy( dst, src1, 8 );
1249 for( x = 0; x < 8; x++ )
1250 dst[x] = (src1[x] + 6*src2[x] + src1[i_src1+x] + 4 ) >> 3;
1258 #ifdef CAN_COMPILE_MMXEXT
1259 static inline void XDeint8x8MergeMMXEXT( uint8_t *dst, int i_dst,
1260 uint8_t *src1, int i_src1,
1261 uint8_t *src2, int i_src2 )
1263 static const uint64_t m_4 = INT64_C(0x0004000400040004);
1267 pxor_r2r( mm7, mm7 );
1268 for( y = 0; y < 8; y += 2 )
1270 for( x = 0; x < 8; x +=4 )
1272 movd_m2r( src1[x], mm0 );
1273 movd_r2m( mm0, dst[x] );
1275 movd_m2r( src2[x], mm1 );
1276 movd_m2r( src1[i_src1+x], mm2 );
1278 punpcklbw_r2r( mm7, mm0 );
1279 punpcklbw_r2r( mm7, mm1 );
1280 punpcklbw_r2r( mm7, mm2 );
1281 paddw_r2r( mm1, mm1 );
1282 movq_r2r( mm1, mm3 );
1283 paddw_r2r( mm3, mm3 );
1284 paddw_r2r( mm2, mm0 );
1285 paddw_r2r( mm3, mm1 );
1286 paddw_m2r( m_4, mm1 );
1287 paddw_r2r( mm1, mm0 );
1288 psraw_i2r( 3, mm0 );
1289 packuswb_r2r( mm7, mm0 );
1290 movd_r2m( mm0, dst[i_dst+x] );
1301 static inline void XDeint8x8Set( uint8_t *dst, int i_dst, uint8_t v )
1304 for( y = 0; y < 8; y++ )
1305 memset( &dst[y*i_dst], v, 8 );
1308 /* XDeint8x8FieldE: Stupid deinterlacing (1,0,1) for block that miss a
1311 * TODO: a better one for the inner part.
1313 static inline void XDeint8x8FieldEC( uint8_t *dst, int i_dst,
1314 uint8_t *src, int i_src )
1319 for( y = 0; y < 8; y += 2 )
1321 memcpy( dst, src, 8 );
1324 for( x = 0; x < 8; x++ )
1325 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1330 #ifdef CAN_COMPILE_MMXEXT
1331 static inline void XDeint8x8FieldEMMXEXT( uint8_t *dst, int i_dst,
1332 uint8_t *src, int i_src )
1337 for( y = 0; y < 8; y += 2 )
1339 movq_m2r( src[0], mm0 );
1340 movq_r2m( mm0, dst[0] );
1343 movq_m2r( src[2*i_src], mm1 );
1344 pavgb_r2r( mm1, mm0 );
1346 movq_r2m( mm0, dst[0] );
1354 /* XDeint8x8Field: Edge oriented interpolation
1355 * (Need -4 and +5 pixels H, +1 line)
1357 static inline void XDeint8x8FieldC( uint8_t *dst, int i_dst,
1358 uint8_t *src, int i_src )
1363 for( y = 0; y < 8; y += 2 )
1365 memcpy( dst, src, 8 );
1368 for( x = 0; x < 8; x++ )
1370 uint8_t *src2 = &src[2*i_src];
1371 /* I use 8 pixels just to match the MMX version, but it's overkill
1372 * 5 would be enough (less isn't good) */
1373 const int c0 = abs(src[x-4]-src2[x-2]) + abs(src[x-3]-src2[x-1]) +
1374 abs(src[x-2]-src2[x+0]) + abs(src[x-1]-src2[x+1]) +
1375 abs(src[x+0]-src2[x+2]) + abs(src[x+1]-src2[x+3]) +
1376 abs(src[x+2]-src2[x+4]) + abs(src[x+3]-src2[x+5]);
1378 const int c1 = abs(src[x-3]-src2[x-3]) + abs(src[x-2]-src2[x-2]) +
1379 abs(src[x-1]-src2[x-1]) + abs(src[x+0]-src2[x+0]) +
1380 abs(src[x+1]-src2[x+1]) + abs(src[x+2]-src2[x+2]) +
1381 abs(src[x+3]-src2[x+3]) + abs(src[x+4]-src2[x+4]);
1383 const int c2 = abs(src[x-2]-src2[x-4]) + abs(src[x-1]-src2[x-3]) +
1384 abs(src[x+0]-src2[x-2]) + abs(src[x+1]-src2[x-1]) +
1385 abs(src[x+2]-src2[x+0]) + abs(src[x+3]-src2[x+1]) +
1386 abs(src[x+4]-src2[x+2]) + abs(src[x+5]-src2[x+3]);
1388 if( c0 < c1 && c1 <= c2 )
1389 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1390 else if( c2 < c1 && c1 <= c0 )
1391 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1393 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1400 #ifdef CAN_COMPILE_MMXEXT
1401 static inline void XDeint8x8FieldMMXEXT( uint8_t *dst, int i_dst,
1402 uint8_t *src, int i_src )
1407 for( y = 0; y < 8; y += 2 )
1409 memcpy( dst, src, 8 );
1412 for( x = 0; x < 8; x++ )
1414 uint8_t *src2 = &src[2*i_src];
1417 movq_m2r( src[x-2], mm0 );
1418 movq_m2r( src[x-3], mm1 );
1419 movq_m2r( src[x-4], mm2 );
1421 psadbw_m2r( src2[x-4], mm0 );
1422 psadbw_m2r( src2[x-3], mm1 );
1423 psadbw_m2r( src2[x-2], mm2 );
1425 movd_r2m( mm0, c2 );
1426 movd_r2m( mm1, c1 );
1427 movd_r2m( mm2, c0 );
1429 if( c0 < c1 && c1 <= c2 )
1430 dst[x] = (src[x-1] + src2[x+1]) >> 1;
1431 else if( c2 < c1 && c1 <= c0 )
1432 dst[x] = (src[x+1] + src2[x-1]) >> 1;
1434 dst[x] = (src[x+0] + src2[x+0]) >> 1;
1443 /* NxN arbitray size (and then only use pixel in the NxN block)
1445 static inline int XDeintNxNDetect( uint8_t *src, int i_src,
1446 int i_height, int i_width )
1453 /* Detect interlacing */
1454 /* FIXME way too simple, need to be more like XDeint8x8Detect */
1457 for( y = 0; y < i_height - 2; y += 2 )
1459 const uint8_t *s = &src[y*i_src];
1460 for( x = 0; x < i_width; x++ )
1462 fr += ssd(s[ x] - s[1*i_src+x]);
1463 ff += ssd(s[ x] - s[2*i_src+x]);
1465 if( ff < fr && fr > i_width / 2 )
1469 return fc < 2 ? false : true;
1472 static inline void XDeintNxNFrame( uint8_t *dst, int i_dst,
1473 uint8_t *src, int i_src,
1474 int i_width, int i_height )
1479 for( y = 0; y < i_height; y += 2 )
1481 memcpy( dst, src, i_width );
1484 if( y < i_height - 2 )
1486 for( x = 0; x < i_width; x++ )
1487 dst[x] = (src[x] + 2*src[1*i_src+x] + src[2*i_src+x] + 2 ) >> 2;
1491 /* Blend last line */
1492 for( x = 0; x < i_width; x++ )
1493 dst[x] = (src[x] + src[1*i_src+x] ) >> 1;
1500 static inline void XDeintNxNField( uint8_t *dst, int i_dst,
1501 uint8_t *src, int i_src,
1502 int i_width, int i_height )
1507 for( y = 0; y < i_height; y += 2 )
1509 memcpy( dst, src, i_width );
1512 if( y < i_height - 2 )
1514 for( x = 0; x < i_width; x++ )
1515 dst[x] = (src[x] + src[2*i_src+x] ) >> 1;
1519 /* Blend last line */
1520 for( x = 0; x < i_width; x++ )
1521 dst[x] = (src[x] + src[i_src+x]) >> 1;
1528 static inline void XDeintNxN( uint8_t *dst, int i_dst, uint8_t *src, int i_src,
1529 int i_width, int i_height )
1531 if( XDeintNxNDetect( src, i_src, i_width, i_height ) )
1532 XDeintNxNField( dst, i_dst, src, i_src, i_width, i_height );
1534 XDeintNxNFrame( dst, i_dst, src, i_src, i_width, i_height );
1538 static inline int median( int a, int b, int c )
1540 int min = a, max =a;
1551 return a + b + c - min - max;
1557 static inline void XDeintBand8x8C( uint8_t *dst, int i_dst,
1558 uint8_t *src, int i_src,
1559 const int i_mbx, int i_modx )
1563 for( x = 0; x < i_mbx; x++ )
1566 if( ( s = XDeint8x8DetectC( src, i_src ) ) )
1568 if( x == 0 || x == i_mbx - 1 )
1569 XDeint8x8FieldEC( dst, i_dst, src, i_src );
1571 XDeint8x8FieldC( dst, i_dst, src, i_src );
1575 XDeint8x8MergeC( dst, i_dst,
1576 &src[0*i_src], 2*i_src,
1577 &src[1*i_src], 2*i_src );
1585 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1587 #ifdef CAN_COMPILE_MMXEXT
1588 static inline void XDeintBand8x8MMXEXT( uint8_t *dst, int i_dst,
1589 uint8_t *src, int i_src,
1590 const int i_mbx, int i_modx )
1594 /* Reset current line */
1595 for( x = 0; x < i_mbx; x++ )
1598 if( ( s = XDeint8x8DetectMMXEXT( src, i_src ) ) )
1600 if( x == 0 || x == i_mbx - 1 )
1601 XDeint8x8FieldEMMXEXT( dst, i_dst, src, i_src );
1603 XDeint8x8FieldMMXEXT( dst, i_dst, src, i_src );
1607 XDeint8x8MergeMMXEXT( dst, i_dst,
1608 &src[0*i_src], 2*i_src,
1609 &src[1*i_src], 2*i_src );
1617 XDeintNxN( dst, i_dst, src, i_src, i_modx, 8 );
1621 static void RenderX( picture_t *p_outpic, picture_t *p_pic )
1625 /* Copy image and skip lines */
1626 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
1628 const int i_mby = ( p_outpic->p[i_plane].i_visible_lines + 7 )/8 - 1;
1629 const int i_mbx = p_outpic->p[i_plane].i_visible_pitch/8;
1631 const int i_mody = p_outpic->p[i_plane].i_visible_lines - 8*i_mby;
1632 const int i_modx = p_outpic->p[i_plane].i_visible_pitch - 8*i_mbx;
1634 const int i_dst = p_outpic->p[i_plane].i_pitch;
1635 const int i_src = p_pic->p[i_plane].i_pitch;
1639 for( y = 0; y < i_mby; y++ )
1641 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1642 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1644 #ifdef CAN_COMPILE_MMXEXT
1645 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1646 XDeintBand8x8MMXEXT( dst, i_dst, src, i_src, i_mbx, i_modx );
1649 XDeintBand8x8C( dst, i_dst, src, i_src, i_mbx, i_modx );
1652 /* Last line (C only)*/
1655 uint8_t *dst = &p_outpic->p[i_plane].p_pixels[8*y*i_dst];
1656 uint8_t *src = &p_pic->p[i_plane].p_pixels[8*y*i_src];
1658 for( x = 0; x < i_mbx; x++ )
1660 XDeintNxN( dst, i_dst, src, i_src, 8, i_mody );
1667 XDeintNxN( dst, i_dst, src, i_src, i_modx, i_mody );
1671 #ifdef CAN_COMPILE_MMXEXT
1672 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
1677 /*****************************************************************************
1678 * FilterCallback: called when changing the deinterlace method on the fly.
1679 *****************************************************************************/
1680 static int FilterCallback( vlc_object_t *p_this, char const *psz_cmd,
1681 vlc_value_t oldval, vlc_value_t newval,
1684 VLC_UNUSED(psz_cmd); VLC_UNUSED(p_data); VLC_UNUSED(oldval);
1685 vout_thread_t * p_vout = (vout_thread_t *)p_this;
1686 vout_sys_t *p_sys = p_vout->p_sys;
1688 msg_Dbg( p_vout, "using %s deinterlace mode", newval.psz_string );
1690 vlc_mutex_lock( &p_sys->filter_lock );
1691 const bool b_old_half_height = p_sys->b_half_height;
1693 SetFilterMethod( p_vout, newval.psz_string );
1695 if( !b_old_half_height == !p_sys->b_half_height )
1697 vlc_mutex_unlock( &p_sys->filter_lock );
1701 /* We need to kill the old vout */
1704 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
1705 vout_CloseAndRelease( p_sys->p_vout );
1708 /* Try to open a new video output */
1709 p_sys->p_vout = SpawnRealVout( p_vout );
1711 if( p_sys->p_vout == NULL )
1713 /* Everything failed */
1714 msg_Err( p_vout, "cannot open vout, aborting" );
1716 vlc_mutex_unlock( &p_sys->filter_lock );
1717 return VLC_EGENERIC;
1720 vout_filter_AddChild( p_vout, p_sys->p_vout, MouseEvent );
1722 vlc_mutex_unlock( &p_sys->filter_lock );
1726 /*****************************************************************************
1727 * video filter2 functions
1728 *****************************************************************************/
1729 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
1731 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1732 picture_t *p_pic_dst;
1734 /* Request output picture */
1735 p_pic_dst = filter_NewPicture( p_filter );
1736 if( p_pic_dst == NULL )
1738 picture_Release( p_pic );
1742 switch( p_vout->p_sys->i_mode )
1744 case DEINTERLACE_DISCARD:
1745 RenderDiscard( p_vout, p_pic_dst, p_pic, 0 );
1748 case DEINTERLACE_BOB:
1750 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
1751 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
1755 case DEINTERLACE_LINEAR:
1757 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
1758 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
1760 msg_Err( p_vout, "doubling the frame rate is not supported yet" );
1761 picture_Release( p_pic_dst );
1762 picture_Release( p_pic );
1765 case DEINTERLACE_MEAN:
1766 RenderMean( p_vout, p_pic_dst, p_pic );
1769 case DEINTERLACE_BLEND:
1770 RenderBlend( p_vout, p_pic_dst, p_pic );
1774 RenderX( p_pic_dst, p_pic );
1778 picture_CopyProperties( p_pic_dst, p_pic );
1779 p_pic_dst->b_progressive = true;
1781 picture_Release( p_pic );
1785 /*****************************************************************************
1787 *****************************************************************************/
1788 static int OpenFilter( vlc_object_t *p_this )
1790 filter_t *p_filter = (filter_t*)p_this;
1791 vout_thread_t *p_vout;
1794 if( !IsChromaSupported( p_filter->fmt_in.video.i_chroma ) )
1795 return VLC_EGENERIC;
1797 /* Impossible to use VLC_OBJECT_VOUT here because it would be used
1799 p_vout = vlc_object_create( p_filter, sizeof(vout_thread_t) );
1800 vlc_object_attach( p_vout, p_filter );
1801 p_filter->p_sys = (filter_sys_t *)p_vout;
1802 p_vout->render.i_chroma = p_filter->fmt_in.video.i_chroma;
1804 config_ChainParse( p_filter, FILTER_CFG_PREFIX, ppsz_filter_options,
1806 var_Get( p_filter, FILTER_CFG_PREFIX "mode", &val );
1808 var_Create( p_filter, "deinterlace-mode", VLC_VAR_STRING );
1809 var_Set( p_filter, "deinterlace-mode", val );
1810 free( val.psz_string );
1812 if( Create( VLC_OBJECT(p_vout) ) != VLC_SUCCESS )
1814 vlc_object_detach( p_vout );
1815 vlc_object_release( p_vout );
1816 return VLC_EGENERIC;
1820 GetOutputFormat( p_vout, &fmt, &p_filter->fmt_in.video );
1821 if( !p_filter->b_allow_fmt_out_change &&
1822 ( fmt.i_chroma != p_filter->fmt_in.video.i_chroma ||
1823 fmt.i_height != p_filter->fmt_in.video.i_height ) )
1825 CloseFilter( VLC_OBJECT(p_filter) );
1826 return VLC_EGENERIC;
1828 p_filter->fmt_out.video = fmt;
1829 p_filter->fmt_out.i_codec = fmt.i_chroma;
1830 p_filter->pf_video_filter = Deinterlace;
1832 msg_Dbg( p_filter, "deinterlacing" );
1837 /*****************************************************************************
1838 * CloseFilter: clean up the filter
1839 *****************************************************************************/
1840 static void CloseFilter( vlc_object_t *p_this )
1842 filter_t *p_filter = (filter_t*)p_this;
1843 vout_thread_t *p_vout = (vout_thread_t *)p_filter->p_sys;
1845 Destroy( VLC_OBJECT(p_vout) );
1846 vlc_object_detach( p_vout );
1847 vlc_object_release( p_vout );