1 /*****************************************************************************
2 * deinterlace.c : deinterlacer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: deinterlace.c,v 1.5 2002/11/28 17:35:00 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 RenderDiscard( vout_thread_t *, picture_t *, picture_t *, int );
53 static void RenderBob ( vout_thread_t *, picture_t *, picture_t *, int );
54 static void RenderMean ( vout_thread_t *, picture_t *, picture_t * );
55 static void RenderBlend ( vout_thread_t *, picture_t *, picture_t * );
56 static void RenderLinear ( vout_thread_t *, picture_t *, picture_t *, int );
58 static void Merge ( void *, const void *, const void *, size_t );
60 /*****************************************************************************
62 *****************************************************************************/
63 #define MODE_TEXT N_("deinterlace mode")
64 #define MODE_LONGTEXT N_("One of \"discard\", \"blend\", \"mean\", \"bob\" or \"linear\"")
66 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", NULL };
69 add_category_hint( N_("Miscellaneous"), NULL );
70 add_string_from_list( "deinterlace-mode", "discard", mode_list, NULL,
71 MODE_TEXT, MODE_LONGTEXT );
72 set_description( _("deinterlacing module") );
73 set_capability( "video filter", 0 );
74 add_shortcut( "deinterlace" );
75 set_callbacks( Create, Destroy );
78 /*****************************************************************************
79 * vout_sys_t: Deinterlace video output method descriptor
80 *****************************************************************************
81 * This structure is part of the video output thread descriptor.
82 * It describes the Deinterlace specific properties of an output thread.
83 *****************************************************************************/
86 int i_mode; /* Deinterlace mode */
87 vlc_bool_t b_double_rate; /* Shall we double the framerate? */
92 vout_thread_t *p_vout;
95 /*****************************************************************************
96 * Create: allocates Deinterlace video thread output method
97 *****************************************************************************
98 * This function allocates and initializes a Deinterlace vout method.
99 *****************************************************************************/
100 static int Create( vlc_object_t *p_this )
102 vout_thread_t *p_vout = (vout_thread_t *)p_this;
105 /* Allocate structure */
106 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
107 if( p_vout->p_sys == NULL )
109 msg_Err( p_vout, "out of memory" );
113 p_vout->pf_init = Init;
114 p_vout->pf_end = End;
115 p_vout->pf_manage = NULL;
116 p_vout->pf_render = Render;
117 p_vout->pf_display = NULL;
119 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
120 p_vout->p_sys->b_double_rate = 0;
121 p_vout->p_sys->last_date = 0;
123 /* Look what method was requested */
124 psz_method = config_GetPsz( p_vout, "deinterlace-mode" );
126 if( psz_method == NULL )
128 msg_Err( p_vout, "configuration variable %s empty",
129 "deinterlace-mode" );
130 msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
134 if( !strcmp( psz_method, "discard" ) )
136 p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
138 else if( !strcmp( psz_method, "mean" ) )
140 p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
142 else if( !strcmp( psz_method, "blend" )
143 || !strcmp( psz_method, "average" )
144 || !strcmp( psz_method, "combine-fields" ) )
146 p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
148 else if( !strcmp( psz_method, "bob" )
149 || !strcmp( psz_method, "progressive-scan" ) )
151 p_vout->p_sys->i_mode = DEINTERLACE_BOB;
152 p_vout->p_sys->b_double_rate = 1;
154 else if( !strcmp( psz_method, "linear" ) )
156 p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
157 p_vout->p_sys->b_double_rate = 1;
161 msg_Err( p_vout, "no valid deinterlace mode provided, "
162 "using \"discard\"" );
171 /*****************************************************************************
172 * Init: initialize Deinterlace video thread output method
173 *****************************************************************************/
174 static int Init( vout_thread_t *p_vout )
179 I_OUTPUTPICTURES = 0;
181 /* Initialize the output structure, full of directbuffers since we want
182 * the decoder to output directly to our structures. */
183 switch( p_vout->render.i_chroma )
185 case VLC_FOURCC('I','4','2','0'):
186 case VLC_FOURCC('I','Y','U','V'):
187 case VLC_FOURCC('Y','V','1','2'):
188 case VLC_FOURCC('I','4','2','2'):
189 p_vout->output.i_chroma = p_vout->render.i_chroma;
190 p_vout->output.i_width = p_vout->render.i_width;
191 p_vout->output.i_height = p_vout->render.i_height;
192 p_vout->output.i_aspect = p_vout->render.i_aspect;
196 return 0; /* unknown chroma */
200 /* Try to open the real video output, with half the height our images */
201 msg_Dbg( p_vout, "spawning the real video output" );
203 switch( p_vout->render.i_chroma )
205 case VLC_FOURCC('I','4','2','0'):
206 case VLC_FOURCC('I','Y','U','V'):
207 case VLC_FOURCC('Y','V','1','2'):
208 switch( p_vout->p_sys->i_mode )
210 case DEINTERLACE_MEAN:
211 case DEINTERLACE_DISCARD:
212 p_vout->p_sys->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_BOB:
219 case DEINTERLACE_BLEND:
220 case DEINTERLACE_LINEAR:
221 p_vout->p_sys->p_vout =
223 p_vout->output.i_width, p_vout->output.i_height,
224 p_vout->output.i_chroma, p_vout->output.i_aspect );
229 case VLC_FOURCC('I','4','2','2'):
230 p_vout->p_sys->p_vout =
232 p_vout->output.i_width, p_vout->output.i_height,
233 VLC_FOURCC('I','4','2','0'), p_vout->output.i_aspect );
240 /* Everything failed */
241 if( p_vout->p_sys->p_vout == NULL )
243 msg_Err( p_vout, "cannot open vout, aborting" );
248 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
253 /*****************************************************************************
254 * End: terminate Deinterlace video thread output method
255 *****************************************************************************/
256 static void End( vout_thread_t *p_vout )
260 /* Free the fake output buffers we allocated */
261 for( i_index = I_OUTPUTPICTURES ; i_index ; )
264 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
268 /*****************************************************************************
269 * Destroy: destroy Deinterlace video thread output method
270 *****************************************************************************
271 * Terminate an output method created by DeinterlaceCreateOutputMethod
272 *****************************************************************************/
273 static void Destroy( vlc_object_t *p_this )
275 vout_thread_t *p_vout = (vout_thread_t *)p_this;
277 vout_Destroy( p_vout->p_sys->p_vout );
279 free( p_vout->p_sys );
282 /*****************************************************************************
283 * Render: displays previously rendered output
284 *****************************************************************************
285 * This function send the currently rendered image to Deinterlace image,
286 * waits until it is displayed and switch the two rendering buffers, preparing
288 *****************************************************************************/
289 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
291 picture_t *pp_outpic[2];
293 /* Get a new picture */
294 while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
298 if( p_vout->b_die || p_vout->b_error )
302 msleep( VOUT_OUTMEM_SLEEP );
305 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
307 /* If we are using double rate, get an additional new picture */
308 if( p_vout->p_sys->b_double_rate )
310 while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
314 if( p_vout->b_die || p_vout->b_error )
316 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
319 msleep( VOUT_OUTMEM_SLEEP );
322 /* 20ms is a bit arbitrary, but it's only for the first image we get */
323 if( !p_vout->p_sys->last_date )
325 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
326 p_pic->date + 20000 );
330 vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
331 (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
333 p_vout->p_sys->last_date = p_pic->date;
336 switch( p_vout->p_sys->i_mode )
338 case DEINTERLACE_DISCARD:
339 RenderDiscard( p_vout, pp_outpic[0], p_pic, 0 );
340 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
343 case DEINTERLACE_BOB:
344 RenderBob( p_vout, pp_outpic[0], p_pic, 0 );
345 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
346 RenderBob( p_vout, pp_outpic[1], p_pic, 1 );
347 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
350 case DEINTERLACE_LINEAR:
351 RenderLinear( p_vout, pp_outpic[0], p_pic, 0 );
352 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
353 RenderLinear( p_vout, pp_outpic[1], p_pic, 1 );
354 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[1] );
357 case DEINTERLACE_MEAN:
358 RenderMean( p_vout, pp_outpic[0], p_pic );
359 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
362 case DEINTERLACE_BLEND:
363 RenderBlend( p_vout, pp_outpic[0], p_pic );
364 vout_DisplayPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
369 /*****************************************************************************
370 * RenderDiscard: only keep TOP or BOTTOM field, discard the other.
371 *****************************************************************************/
372 static void RenderDiscard( vout_thread_t *p_vout,
373 picture_t *p_outpic, picture_t *p_pic, int i_field )
377 /* Copy image and skip lines */
378 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
380 u8 *p_in, *p_out_end, *p_out;
383 p_in = p_pic->p[i_plane].p_pixels
384 + i_field * p_pic->p[i_plane].i_pitch;
386 p_out = p_outpic->p[i_plane].p_pixels;
387 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
388 * p_outpic->p[i_plane].i_lines;
390 switch( p_vout->render.i_chroma )
392 case VLC_FOURCC('I','4','2','0'):
393 case VLC_FOURCC('I','Y','U','V'):
394 case VLC_FOURCC('Y','V','1','2'):
396 for( ; p_out < p_out_end ; )
398 p_vout->p_vlc->pf_memcpy( p_out, p_in,
399 p_pic->p[i_plane].i_pitch );
401 p_out += p_pic->p[i_plane].i_pitch;
402 p_in += 2 * p_pic->p[i_plane].i_pitch;
406 case VLC_FOURCC('I','4','2','2'):
408 i_increment = 2 * p_pic->p[i_plane].i_pitch;
410 if( i_plane == Y_PLANE )
412 for( ; p_out < p_out_end ; )
414 p_vout->p_vlc->pf_memcpy( p_out, p_in,
415 p_pic->p[i_plane].i_pitch );
416 p_out += p_pic->p[i_plane].i_pitch;
417 p_vout->p_vlc->pf_memcpy( p_out, p_in,
418 p_pic->p[i_plane].i_pitch );
419 p_out += p_pic->p[i_plane].i_pitch;
425 for( ; p_out < p_out_end ; )
427 p_vout->p_vlc->pf_memcpy( p_out, p_in,
428 p_pic->p[i_plane].i_pitch );
429 p_out += p_pic->p[i_plane].i_pitch;
441 /*****************************************************************************
442 * RenderBob: renders a BOB picture - simple copy
443 *****************************************************************************/
444 static void RenderBob( vout_thread_t *p_vout,
445 picture_t *p_outpic, picture_t *p_pic, int i_field )
449 /* Copy image and skip lines */
450 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
452 u8 *p_in, *p_out_end, *p_out;
454 p_in = p_pic->p[i_plane].p_pixels;
455 p_out = p_outpic->p[i_plane].p_pixels;
456 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
457 * p_outpic->p[i_plane].i_lines;
459 /* For BOTTOM field we need to add the first line */
462 p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
463 p_in += p_pic->p[i_plane].i_pitch;
464 p_out += p_pic->p[i_plane].i_pitch;
467 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
469 for( ; p_out < p_out_end ; )
471 p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
473 p_out += p_pic->p[i_plane].i_pitch;
475 p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
477 p_in += 2 * p_pic->p[i_plane].i_pitch;
478 p_out += p_pic->p[i_plane].i_pitch;
481 p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
483 /* For TOP field we need to add the last line */
486 p_in += p_pic->p[i_plane].i_pitch;
487 p_out += p_pic->p[i_plane].i_pitch;
488 p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
493 /*****************************************************************************
494 * RenderLinear: BOB with linear interpolation
495 *****************************************************************************/
496 static void RenderLinear( vout_thread_t *p_vout,
497 picture_t *p_outpic, picture_t *p_pic, int i_field )
501 /* Copy image and skip lines */
502 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
504 u8 *p_in, *p_out_end, *p_out;
506 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 /* For BOTTOM field we need to add the first line */
514 p_vout->p_vlc->pf_memcpy( p_out, p_in,
515 p_pic->p[i_plane].i_pitch );
516 p_in += p_pic->p[i_plane].i_pitch;
517 p_out += p_pic->p[i_plane].i_pitch;
520 p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
522 for( ; p_out < p_out_end ; )
524 p_vout->p_vlc->pf_memcpy( p_out, p_in,
525 p_pic->p[i_plane].i_pitch );
527 p_out += p_pic->p[i_plane].i_pitch;
529 Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
530 p_pic->p[i_plane].i_pitch );
532 p_in += 2 * p_pic->p[i_plane].i_pitch;
533 p_out += p_pic->p[i_plane].i_pitch;
536 p_vout->p_vlc->pf_memcpy( p_out, p_in,
537 p_pic->p[i_plane].i_pitch );
539 /* For TOP field we need to add the last line */
542 p_in += p_pic->p[i_plane].i_pitch;
543 p_out += p_pic->p[i_plane].i_pitch;
544 p_vout->p_vlc->pf_memcpy( p_out, p_in,
545 p_pic->p[i_plane].i_pitch );
550 static void RenderMean( vout_thread_t *p_vout,
551 picture_t *p_outpic, picture_t *p_pic )
555 /* Copy image and skip lines */
556 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
558 u8 *p_in, *p_out_end, *p_out;
560 p_in = p_pic->p[i_plane].p_pixels;
562 p_out = p_outpic->p[i_plane].p_pixels;
563 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
564 * p_outpic->p[i_plane].i_lines;
566 /* All lines: mean value */
567 for( ; p_out < p_out_end ; )
569 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
570 p_pic->p[i_plane].i_pitch );
572 p_out += p_pic->p[i_plane].i_pitch;
573 p_in += 2 * p_pic->p[i_plane].i_pitch;
578 static void RenderBlend( vout_thread_t *p_vout,
579 picture_t *p_outpic, picture_t *p_pic )
583 /* Copy image and skip lines */
584 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
586 u8 *p_in, *p_out_end, *p_out;
588 p_in = p_pic->p[i_plane].p_pixels;
590 p_out = p_outpic->p[i_plane].p_pixels;
591 p_out_end = p_out + p_outpic->p[i_plane].i_pitch
592 * p_outpic->p[i_plane].i_lines;
594 /* First line: simple copy */
595 p_vout->p_vlc->pf_memcpy( p_out, p_in,
596 p_pic->p[i_plane].i_pitch );
597 p_out += p_pic->p[i_plane].i_pitch;
599 /* Remaining lines: mean value */
600 for( ; p_out < p_out_end ; )
602 Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
603 p_pic->p[i_plane].i_pitch );
605 p_out += p_pic->p[i_plane].i_pitch;
606 p_in += p_pic->p[i_plane].i_pitch;
611 static void Merge( void *p_dest, const void *p_s1,
612 const void *p_s2, size_t i_bytes )
614 u8* p_end = (u8*)p_dest + i_bytes - 8;
616 while( (u8*)p_dest < p_end )
618 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
619 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
620 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
621 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
622 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
623 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
624 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
625 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
630 while( (u8*)p_dest < p_end )
632 *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;