* ./modules/access/vcd/vcd.c: changed an error to a warning in the probe
[vlc.git] / modules / video_filter / deinterlace / deinterlace.c
1 /*****************************************************************************
2  * deinterlace.c : deinterlacer plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: deinterlace.c,v 1.4 2002/10/16 11:35:52 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33
34 #include "../filter_common.h"
35
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
41
42 /*****************************************************************************
43  * Local protypes
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47
48 static int  Init      ( vout_thread_t * );
49 static void End       ( vout_thread_t * );
50 static void Render    ( vout_thread_t *, picture_t * );
51
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 );
57
58 static void Merge        ( void *, const void *, const void *, size_t );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 #define MODE_TEXT N_("deinterlace mode")
64 #define MODE_LONGTEXT N_("One of \"discard\", \"blend\", \"mean\", \"bob\" or \"linear\"")
65
66 static char *mode_list[] = { "discard", "blend", "mean", "bob", "linear", NULL };
67
68 vlc_module_begin();
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 );
76 vlc_module_end();
77
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  *****************************************************************************/
84 struct vout_sys_t
85 {
86     int        i_mode;        /* Deinterlace mode */
87     vlc_bool_t b_double_rate; /* Shall we double the framerate? */
88
89     mtime_t    last_date;
90     mtime_t    next_date;
91
92     vout_thread_t *p_vout;
93 };
94
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 )
101 {   
102     vout_thread_t *p_vout = (vout_thread_t *)p_this;
103     char *psz_method;
104
105     /* Allocate structure */
106     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
107     if( p_vout->p_sys == NULL )
108     {
109         msg_Err( p_vout, "out of memory" );
110         return 1;
111     }
112
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;
118
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;
122
123     /* Look what method was requested */
124     psz_method = config_GetPsz( p_vout, "deinterlace-mode" );
125
126     if( psz_method == NULL )
127     {
128         msg_Err( p_vout, "configuration variable %s empty",
129                          "deinterlace-mode" );
130         msg_Err( p_vout, "no deinterlace mode provided, using \"discard\"" );
131     }
132     else
133     {
134         if( !strcmp( psz_method, "discard" ) )
135         {
136             p_vout->p_sys->i_mode = DEINTERLACE_DISCARD;
137         }
138         else if( !strcmp( psz_method, "mean" ) )
139         {
140             p_vout->p_sys->i_mode = DEINTERLACE_MEAN;
141         }
142         else if( !strcmp( psz_method, "blend" )
143                   || !strcmp( psz_method, "average" )
144                   || !strcmp( psz_method, "combine-fields" ) )
145         {
146             p_vout->p_sys->i_mode = DEINTERLACE_BLEND;
147         }
148         else if( !strcmp( psz_method, "bob" )
149                   || !strcmp( psz_method, "progressive-scan" ) )
150         {
151             p_vout->p_sys->i_mode = DEINTERLACE_BOB;
152             p_vout->p_sys->b_double_rate = 1;
153         }
154         else if( !strcmp( psz_method, "linear" ) )
155         {
156             p_vout->p_sys->i_mode = DEINTERLACE_LINEAR;
157             p_vout->p_sys->b_double_rate = 1;
158         }
159         else
160         {
161             msg_Err( p_vout, "no valid deinterlace mode provided, "
162                              "using \"discard\"" );
163         }
164
165         free( psz_method );
166     }
167
168     return 0;
169 }
170
171 /*****************************************************************************
172  * Init: initialize Deinterlace video thread output method
173  *****************************************************************************/
174 static int Init( vout_thread_t *p_vout )
175 {
176     int i_index;
177     picture_t *p_pic;
178     
179     I_OUTPUTPICTURES = 0;
180
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 )
184     {
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;
193             break;
194
195         default:
196             return 0; /* unknown chroma */
197             break;
198     }
199
200     /* Try to open the real video output, with half the height our images */
201     msg_Dbg( p_vout, "spawning the real video output" );
202
203     switch( p_vout->render.i_chroma )
204     {
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 )
209         {
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 );
216             break;
217
218         case DEINTERLACE_BOB:
219         case DEINTERLACE_BLEND:
220         case DEINTERLACE_LINEAR:
221             p_vout->p_sys->p_vout =
222                 vout_CreateThread( p_vout,
223                        p_vout->output.i_width, p_vout->output.i_height,
224                        p_vout->output.i_chroma, p_vout->output.i_aspect );
225             break;
226         }
227         break;
228
229     case VLC_FOURCC('I','4','2','2'):
230         p_vout->p_sys->p_vout =
231             vout_CreateThread( 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 );
234         break;
235
236     default:
237         break;
238     }
239
240     /* Everything failed */
241     if( p_vout->p_sys->p_vout == NULL )
242     {
243         msg_Err( p_vout, "cannot open vout, aborting" );
244
245         return 0;
246     }
247  
248     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
249
250     return 0;
251 }
252
253 /*****************************************************************************
254  * End: terminate Deinterlace video thread output method
255  *****************************************************************************/
256 static void End( vout_thread_t *p_vout )
257 {
258     int i_index;
259
260     /* Free the fake output buffers we allocated */
261     for( i_index = I_OUTPUTPICTURES ; i_index ; )
262     {
263         i_index--;
264         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
265     }
266 }
267
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 )
274 {
275     vout_thread_t *p_vout = (vout_thread_t *)p_this;
276
277     vout_DestroyThread( p_vout->p_sys->p_vout );
278
279     free( p_vout->p_sys );
280 }
281
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
287  * next frame.
288  *****************************************************************************/
289 static void Render ( vout_thread_t *p_vout, picture_t *p_pic )
290 {
291     picture_t *pp_outpic[2];
292
293     /* Get a new picture */
294     while( ( pp_outpic[0] = vout_CreatePicture( p_vout->p_sys->p_vout,
295                                              0, 0, 0 ) )
296               == NULL )
297     {
298         if( p_vout->b_die || p_vout->b_error )
299         {
300             return;
301         }
302         msleep( VOUT_OUTMEM_SLEEP );
303     }
304
305     vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[0], p_pic->date );
306
307     /* If we are using double rate, get an additional new picture */
308     if( p_vout->p_sys->b_double_rate )
309     {
310         while( ( pp_outpic[1] = vout_CreatePicture( p_vout->p_sys->p_vout,
311                                                  0, 0, 0 ) )
312                   == NULL )
313         {
314             if( p_vout->b_die || p_vout->b_error )
315             {
316                 vout_DestroyPicture( p_vout->p_sys->p_vout, pp_outpic[0] );
317                 return;
318             }
319             msleep( VOUT_OUTMEM_SLEEP );
320         }   
321
322         /* 20ms is a bit arbitrary, but it's only for the first image we get */
323         if( !p_vout->p_sys->last_date )
324         {
325             vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
326                               p_pic->date + 20000 );
327         }
328         else
329         {
330             vout_DatePicture( p_vout->p_sys->p_vout, pp_outpic[1],
331                       (3 * p_pic->date - p_vout->p_sys->last_date) / 2 );
332         }
333         p_vout->p_sys->last_date = p_pic->date;
334     }
335
336     switch( p_vout->p_sys->i_mode )
337     {
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] );
341             break;
342
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] );
348             break;
349
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] );
355             break;
356
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] );
360             break;
361
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] );
365             break;
366     }
367 }
368
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 )
374 {
375     int i_plane;
376
377     /* Copy image and skip lines */
378     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
379     {
380         u8 *p_in, *p_out_end, *p_out;
381         int i_increment;
382
383         p_in = p_pic->p[i_plane].p_pixels
384                    + i_field * p_pic->p[i_plane].i_pitch;
385
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;
389
390         switch( p_vout->render.i_chroma )
391         {
392         case VLC_FOURCC('I','4','2','0'):
393         case VLC_FOURCC('I','Y','U','V'):
394         case VLC_FOURCC('Y','V','1','2'):
395
396             for( ; p_out < p_out_end ; )
397             {
398                 p_vout->p_vlc->pf_memcpy( p_out, p_in,
399                                           p_pic->p[i_plane].i_pitch );
400
401                 p_out += p_pic->p[i_plane].i_pitch;
402                 p_in += 2 * p_pic->p[i_plane].i_pitch;
403             }
404             break;
405
406         case VLC_FOURCC('I','4','2','2'):
407
408             i_increment = 2 * p_pic->p[i_plane].i_pitch;
409
410             if( i_plane == Y_PLANE )
411             {
412                 for( ; p_out < p_out_end ; )
413                 {
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;
420                     p_in += i_increment;
421                 }
422             }
423             else
424             {
425                 for( ; p_out < p_out_end ; )
426                 {
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;
430                     p_in += i_increment;
431                 }
432             }
433             break;
434
435         default:
436             break;
437         }
438     }
439 }
440
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 )
446 {
447     int i_plane;  
448
449     /* Copy image and skip lines */
450     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
451     {
452         u8 *p_in, *p_out_end, *p_out;
453
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;
458
459         /* For BOTTOM field we need to add the first line */
460         if( i_field == 1 )
461         {
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;
465         }
466                   
467         p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
468     
469         for( ; p_out < p_out_end ; )
470         {
471             p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
472      
473             p_out += p_pic->p[i_plane].i_pitch;
474
475             p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
476         
477             p_in += 2 * p_pic->p[i_plane].i_pitch;
478             p_out += p_pic->p[i_plane].i_pitch;
479         }
480  
481         p_vout->p_vlc->pf_memcpy( p_out, p_in, p_pic->p[i_plane].i_pitch );
482          
483         /* For TOP field we need to add the last line */
484         if( i_field == 0 )
485         {
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 );
489         }         
490     }
491 }
492
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 )
498 {
499     int i_plane;
500
501     /* Copy image and skip lines */
502     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
503     {
504         u8 *p_in, *p_out_end, *p_out;
505
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;
510
511         /* For BOTTOM field we need to add the first line */
512         if( i_field == 1 )
513         {
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;
518         }
519
520         p_out_end -= 2 * p_outpic->p[i_plane].i_pitch;
521
522         for( ; p_out < p_out_end ; )
523         {
524             p_vout->p_vlc->pf_memcpy( p_out, p_in,
525                                       p_pic->p[i_plane].i_pitch );
526
527             p_out += p_pic->p[i_plane].i_pitch;
528
529             Merge( p_out, p_in, p_in + 2 * p_pic->p[i_plane].i_pitch,
530                    p_pic->p[i_plane].i_pitch );
531
532             p_in += 2 * p_pic->p[i_plane].i_pitch;
533             p_out += p_pic->p[i_plane].i_pitch;
534         }
535
536         p_vout->p_vlc->pf_memcpy( p_out, p_in,
537                                   p_pic->p[i_plane].i_pitch );
538
539         /* For TOP field we need to add the last line */
540         if( i_field == 0 )
541         {
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 );
546         }
547     }
548 }
549
550 static void RenderMean( vout_thread_t *p_vout,
551                         picture_t *p_outpic, picture_t *p_pic )
552 {
553     int i_plane;
554
555     /* Copy image and skip lines */
556     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
557     {
558         u8 *p_in, *p_out_end, *p_out;
559
560         p_in = p_pic->p[i_plane].p_pixels;
561
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;
565
566         /* All lines: mean value */
567         for( ; p_out < p_out_end ; )
568         {
569             Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
570                    p_pic->p[i_plane].i_pitch );
571
572             p_out += p_pic->p[i_plane].i_pitch;
573             p_in += 2 * p_pic->p[i_plane].i_pitch;
574         }
575     }
576 }
577
578 static void RenderBlend( vout_thread_t *p_vout,
579                          picture_t *p_outpic, picture_t *p_pic )
580 {
581     int i_plane;
582
583     /* Copy image and skip lines */
584     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
585     {
586         u8 *p_in, *p_out_end, *p_out;
587
588         p_in = p_pic->p[i_plane].p_pixels;
589
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;
593
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;
598
599         /* Remaining lines: mean value */
600         for( ; p_out < p_out_end ; )
601         {
602             Merge( p_out, p_in, p_in + p_pic->p[i_plane].i_pitch,
603                    p_pic->p[i_plane].i_pitch );
604
605             p_out += p_pic->p[i_plane].i_pitch;
606             p_in += p_pic->p[i_plane].i_pitch;
607         }
608     }
609 }
610
611 static void Merge( void *p_dest, const void *p_s1,
612                    const void *p_s2, size_t i_bytes )
613 {
614     u8* p_end = (u8*)p_dest + i_bytes - 8;
615
616     while( (u8*)p_dest < p_end )
617     {
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;
626     }
627
628     p_end += 8;
629
630     while( (u8*)p_dest < p_end )
631     {
632         *(u8*)p_dest++ = ( (u16)(*(u8*)p_s1++) + (u16)(*(u8*)p_s2++) ) >> 1;
633     }
634 }