1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: deinterlace.c,v 1.20 2002/07/31 20:56:51 sam Exp $
7 * Authors: Samuel 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
34 #include "filter_common.h"
36 #define DEINTERLACE_DISCARD 1
37 #define DEINTERLACE_MEAN 2
38 #define DEINTERLACE_BLEND 3
39 #define DEINTERLACE_BOB 4
40 #define DEINTERLACE_LINEAR 5
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
48 static int Init ( vout_thread_t * );
49 static void End ( vout_thread_t * );
50 static void Render ( vout_thread_t *, picture_t * );
52 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
53 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
54 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
55 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
57 static void Merge ( void *, const void *, const void *, size_t );
59 /*****************************************************************************
61 *****************************************************************************/
62 #define MODE_TEXT N_("Deinterlace mode")
63 #define MODE_LONGTEXT N_("one of \"discard\", \"blend\", \"mean\", \"bob\" or \"linear\"")
65 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", NULL };
68 add_category_hint( N_("Miscellaneous"), NULL );
69 add_string_from_list( "deinterlace-mode", "discard", mode_list, NULL,
70 MODE_TEXT, MODE_LONGTEXT );
71 set_description( _("deinterlacing module") );
72 set_capability( "video filter", 0 );
73 add_shortcut( "deinterlace" );
74 set_callbacks( Create, Destroy );
77 /*****************************************************************************
78 * vout_sys_t: Deinterlace video output method descriptor
79 *****************************************************************************
80 * This structure is part of the video output thread descriptor.
81 * It describes the Deinterlace specific properties of an output thread.
82 *****************************************************************************/
85 int i_mode; /* Deinterlace mode */
86 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
91 vout_thread_t *p_vout;
94 /*****************************************************************************
95 * Create: allocates Deinterlace video thread output method
96 *****************************************************************************
97 * This function allocates and initializes a Deinterlace vout method.
98 *****************************************************************************/
99 static int Create( vlc_object_t *p_this )
101 vout_thread_t *p_vout = (vout_thread_t *)p_this;
104 /* Allocate structure */
105 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
106 if( p_vout->p_sys == NULL )
108 msg_Err( p_vout, "out of memory" );
112 p_vout->pf_init = Init;
113 p_vout->pf_end = End;
114 p_vout->pf_manage = NULL;
115 p_vout->pf_render = Render;
116 p_vout->pf_display = NULL;
118 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
119 p_vout->p_sys->b_double_rate = 0;
120 p_vout->p_sys->last_date = 0;
122 /* Look what method was requested */
123 psz_method = config_GetPsz( p_vout, "deinterlace-mode" );
125 if( psz_method == NULL )
127 msg_Err( p_vout, "configuration variable %s empty",
128 "deinterlace-mode" );
129 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
133 if( !strcmp( psz_method, "discard" ) )
135 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
137 else if( !strcmp( psz_method, "mean" ) )
139 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
141 else if( !strcmp( psz_method, "blend" )
142 || !strcmp( psz_method, "average" )
143 || !strcmp( psz_method, "combine-fields" ) )
145 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
147 else if( !strcmp( psz_method, "bob" )
148 || !strcmp( psz_method, "progressive-scan" ) )
150 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
151 p_vout->p_sys->b_double_rate = 1;
153 else if( !strcmp( psz_method, "linear" ) )
155 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
156 p_vout->p_sys->b_double_rate = 1;
160 msg_Err( p_vout, "no valid deinterlace mode provided, "
161 "using \"discard\"" );
170 /*****************************************************************************
171 * Init: initialize Deinterlace video thread output method
172 *****************************************************************************/
173 static int Init( vout_thread_t *p_vout )
178 I_OUTPUTPICTURES = 0;
180 /* Initialize the output structure, full of directbuffers since we want
181 * the decoder to output directly to our structures. */
182 switch( p_vout->render.i_chroma )
184 case VLC_FOURCC('I','4','2','0'):
185 case VLC_FOURCC('I','Y','U','V'):
186 case VLC_FOURCC('Y','V','1','2'):
187 case VLC_FOURCC('I','4','2','2'):
188 p_vout->output.i_chroma = p_vout->render.i_chroma;
189 p_vout->output.i_width = p_vout->render.i_width;
190 p_vout->output.i_height = p_vout->render.i_height;
191 p_vout->output.i_aspect = p_vout->render.i_aspect;
195 return 0; /* unknown chroma */
199 /* Try to open the real video output, with half the height our images */
200 msg_Dbg( p_vout, "spawning the real video output" );
202 switch( p_vout->render.i_chroma )
204 case VLC_FOURCC('I','4','2','0'):
205 case VLC_FOURCC('I','Y','U','V'):
206 case VLC_FOURCC('Y','V','1','2'):
207 switch( p_vout->p_sys->i_mode )
209 case DEINTERLACE_BOB:
210 case DEINTERLACE_MEAN:
211 case DEINTERLACE_DISCARD:
212 p_vout->p_sys->p_vout =
213 vout_CreateThread( p_vout,
214 p_vout->output.i_width, p_vout->output.i_height / 2,
215 p_vout->output.i_chroma, p_vout->output.i_aspect );
218 case DEINTERLACE_BLEND:
219 case DEINTERLACE_LINEAR:
220 p_vout->p_sys->p_vout =
221 vout_CreateThread( p_vout,
222 p_vout->output.i_width, p_vout->output.i_height,
223 p_vout->output.i_chroma, p_vout->output.i_aspect );
228 case VLC_FOURCC('I','4','2','2'):
229 p_vout->p_sys->p_vout =
230 vout_CreateThread( p_vout,
231 p_vout->output.i_width, p_vout->output.i_height,
232 VLC_FOURCC('I','4','2','0'), p_vout->output.i_aspect );
239 /* Everything failed */
240 if( p_vout->p_sys->p_vout == NULL )
242 msg_Err( p_vout, "cannot open vout, aborting" );
247 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
252 /*****************************************************************************
253 * End: terminate Deinterlace video thread output method
254 *****************************************************************************/
255 static void End( vout_thread_t *p_vout )
259 /* Free the fake output buffers we allocated */
260 for( i_index = I_OUTPUTPICTURES ; i_index ; )
263 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
267 /*****************************************************************************
268 * Destroy: destroy Deinterlace video thread output method
269 *****************************************************************************
270 * Terminate an output method created by DeinterlaceCreateOutputMethod
271 *****************************************************************************/
272 static void Destroy( vlc_object_t *p_this )
274 vout_thread_t *p_vout = (vout_thread_t *)p_this;
276 vout_DestroyThread( p_vout->p_sys->p_vout );
278 free( p_vout->p_sys );
281 /*****************************************************************************
282 * Render: displays previously rendered output
283 *****************************************************************************
284 * This function send the currently rendered image to Deinterlace image,
285 * waits until it is displayed and switch the two rendering buffers, preparing
287 *****************************************************************************/
288 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
290 picture_t *pp_outpic[2];
292 /* Get a new picture */
293 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
297 if( p_vout->b_die || p_vout->b_error )
301 msleep( VOUT_OUTMEM_SLEEP );
304 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
306 /* If we are using double rate, get an additional new picture */
307 if( p_vout->p_sys->b_double_rate )
309 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
313 if( p_vout->b_die || p_vout->b_error )
315 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
318 msleep( VOUT_OUTMEM_SLEEP );
321 /* 20ms is a bit arbitrary, but it's only for the first image we get */
322 if( !p_vout->p_sys->last_date )
324 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
325 p_pic->date + 20000 );
329 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
330 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
332 p_vout->p_sys->last_date = p_pic->date;
335 switch( p_vout->p_sys->i_mode )
337 case DEINTERLACE_DISCARD:
338 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
339 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
342 case DEINTERLACE_BOB:
343 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
344 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
345 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
346 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
349 case DEINTERLACE_LINEAR:
350 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
351 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
352 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
353 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
356 case DEINTERLACE_MEAN:
357 RenderMean( p_vout, pp_outpic[0], p_pic );
358 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
361 case DEINTERLACE_BLEND:
362 RenderBlend( p_vout, pp_outpic[0], p_pic );
363 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
368 /*****************************************************************************
369 * RenderBob: renders a bob picture
370 *****************************************************************************/
371 static void RenderBob( vout_thread_t *p_vout,
372 picture_t *p_outpic, picture_t *p_pic, int i_field )
376 /* Copy image and skip lines */
377 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
379 u8 *p_in, *p_out_end, *p_out;
382 p_in = p_pic->p[i_plane].p_pixels
383 + i_field * p_pic->p[i_plane].i_pitch;
385 p_out = p_outpic->p[i_plane].p_pixels;
386 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
387 * p_outpic->p[i_plane].i_lines;
389 switch( p_vout->render.i_chroma )
391 case VLC_FOURCC('I','4','2','0'):
392 case VLC_FOURCC('I','Y','U','V'):
393 case VLC_FOURCC('Y','V','1','2'):
395 for( ; p_out < p_out_end ; )
397 p_vout->p_vlc->pf_memcpy( p_out, p_in,
398 p_pic->p[i_plane].i_pitch );
400 p_out += p_pic->p[i_plane].i_pitch;
401 p_in += 2 * p_pic->p[i_plane].i_pitch;
405 case VLC_FOURCC('I','4','2','2'):
407 i_increment = 2 * p_pic->p[i_plane].i_pitch;
409 if( i_plane == Y_PLANE )
411 for( ; p_out < p_out_end ; )
413 p_vout->p_vlc->pf_memcpy( p_out, p_in,
414 p_pic->p[i_plane].i_pitch );
415 p_out += p_pic->p[i_plane].i_pitch;
416 p_vout->p_vlc->pf_memcpy( p_out, p_in,
417 p_pic->p[i_plane].i_pitch );
418 p_out += p_pic->p[i_plane].i_pitch;
424 for( ; p_out < p_out_end ; )
426 p_vout->p_vlc->pf_memcpy( p_out, p_in,
427 p_pic->p[i_plane].i_pitch );
428 p_out += p_pic->p[i_plane].i_pitch;
440 /*****************************************************************************
441 * RenderLinear: displays previously rendered output
442 *****************************************************************************/
443 static void RenderLinear( vout_thread_t *p_vout,
444 picture_t *p_outpic, picture_t *p_pic, int i_field )
448 /* Copy image and skip lines */
449 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
451 u8 *p_in, *p_out_end, *p_out;
453 p_in = p_pic->p[i_plane].p_pixels
454 + i_field * p_pic->p[i_plane].i_pitch;
456 p_out = p_outpic->p[i_plane].p_pixels;
457 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
458 * p_outpic->p[i_plane].i_lines;
462 p_vout->p_vlc->pf_memcpy( p_out, p_in,
463 p_pic->p[i_plane].i_pitch );
464 p_in += 2 * p_pic->p[i_plane].i_pitch;
465 p_out += p_pic->p[i_plane].i_pitch;
468 p_out_end -= p_outpic->p[i_plane].i_pitch;
470 for( ; p_out < p_out_end ; )
472 p_vout->p_vlc->pf_memcpy( p_out, p_in,
473 p_pic->p[i_plane].i_pitch );
475 p_out += p_pic->p[i_plane].i_pitch;
477 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
478 p_pic->p[i_plane].i_pitch );
480 p_in += 2 * p_pic->p[i_plane].i_pitch;
481 p_out += p_pic->p[i_plane].i_pitch;
487 p_in -= 2 * p_pic->p[i_plane].i_pitch;
488 p_vout->p_vlc->pf_memcpy( p_out, p_in,
489 p_pic->p[i_plane].i_pitch );
495 static void RenderMean( vout_thread_t *p_vout,
496 picture_t *p_outpic, picture_t *p_pic )
500 /* Copy image and skip lines */
501 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
503 u8 *p_in, *p_out_end, *p_out;
505 p_in = p_pic->p[i_plane].p_pixels;
507 p_out = p_outpic->p[i_plane].p_pixels;
508 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
509 * p_outpic->p[i_plane].i_lines;
511 /* All lines: mean value */
512 for( ; p_out < p_out_end ; )
514 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
515 p_pic->p[i_plane].i_pitch );
517 p_out += p_pic->p[i_plane].i_pitch;
518 p_in += 2 * p_pic->p[i_plane].i_pitch;
523 static void RenderBlend( vout_thread_t *p_vout,
524 picture_t *p_outpic, picture_t *p_pic )
528 /* Copy image and skip lines */
529 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
531 u8 *p_in, *p_out_end, *p_out;
533 p_in = p_pic->p[i_plane].p_pixels;
535 p_out = p_outpic->p[i_plane].p_pixels;
536 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
537 * p_outpic->p[i_plane].i_lines;
539 /* First line: simple copy */
540 p_vout->p_vlc->pf_memcpy( p_out, p_in,
541 p_pic->p[i_plane].i_pitch );
542 p_out += p_pic->p[i_plane].i_pitch;
544 /* Remaining lines: mean value */
545 for( ; p_out < p_out_end ; )
547 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
548 p_pic->p[i_plane].i_pitch );
550 p_out += p_pic->p[i_plane].i_pitch;
551 p_in += p_pic->p[i_plane].i_pitch;
556 static void Merge( void *p_dest, const void *p_s1,
557 const void *p_s2, size_t i_bytes )
559 u8* p_end = (u8*)p_dest + i_bytes - 8;
561 while( (u8*)p_dest < p_end )
563 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
564 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
565 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
566 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
567 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
568 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
569 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
570 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
575 while( (u8*)p_dest < p_end )
577 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;