]> git.sesse.net Git - mlt/blob - src/modules/core/filter_deinterlace.c
Filter optimisations and cleanup part 1
[mlt] / src / modules / core / filter_deinterlace.c
1 /*
2  * filter_deinterlace.c -- deinterlace filter
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "filter_deinterlace.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 /* Linear Blend filter - C version contributed by Rogerio Brito.
29    This algorithm has the same interface as the other functions.
30
31    The destination "screen" (pdst) is constructed from the source
32    screen (psrc[0]) line by line.
33
34    The i-th line of the destination screen is the average of 3 lines
35    from the source screen: the (i-1)-th, i-th and (i+1)-th lines, with
36    the i-th line having weight 2 in the computation.
37
38    Remarks:
39    * each line on pdst doesn't depend on previous lines;
40    * due to the way the algorithm is defined, the first & last lines of the
41      screen aren't deinterlaced.
42
43 */
44 static void deinterlace_yuv( uint8_t *pdst, uint8_t *psrc, int width, int height )
45 {
46         register int x, y;
47         register uint8_t *l0, *l1, *l2, *l3;
48
49         l0 = pdst;                      // target line
50         l1 = psrc;                      // 1st source line
51         l2 = l1 + width;        // 2nd source line = line that follows l1
52         l3 = l2 + width;        // 3rd source line = line that follows l2
53
54         // Copy the first line
55         memcpy(l0, l1, width);
56         l0 += width;
57
58         for (y = 1; y < height-1; ++y) 
59         {
60                 // computes avg of: l1 + 2*l2 + l3
61                 for (x = 0; x < width; ++x)
62                         l0[x] = (l1[x] + (l2[x]<<1) + l3[x]) >> 2;
63
64                 // updates the line pointers
65                 l1 = l2; l2 = l3; l3 += width;
66                 l0 += width;
67         }
68
69         // Copy the last line
70         memcpy(l0, l1, width);
71 }
72
73 /** Do it :-).
74 */
75
76 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
77 {
78         // Get the input image
79         int error = mlt_frame_get_image( this, image, format, width, height, 1 );
80
81         // Only continue if we have no error and the right colour space
82         if ( error == 0 && *format == mlt_image_yuv422 )
83         {
84                 // Check that we want progressive and we aren't already progressive
85                 if ( !mlt_properties_get_int( mlt_frame_properties( this ), "progressive" ) &&
86                          mlt_properties_get_int( mlt_frame_properties( this ), "consumer_progressive" ) )
87                 {
88                         // Deinterlace the image
89                         deinterlace_yuv( *image, *image, *width * 2, *height );
90
91                         // Make sure that others know the frame is deinterlaced
92                         mlt_properties_set_int( mlt_frame_properties( this ), "progressive", 1 );
93                 }
94         }
95
96         return error;
97 }
98
99 /** Deinterlace filter processing - this should be lazy evaluation here...
100 */
101
102 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
103 {
104         mlt_frame_push_get_image( frame, filter_get_image );
105         return frame;
106 }
107
108 /** Constructor for the filter.
109 */
110
111 mlt_filter filter_deinterlace_init( void *arg )
112 {
113         mlt_filter this = mlt_filter_new( );
114         if ( this != NULL )
115                 this->process = deinterlace_process;
116         return this;
117 }
118