8916647b144fba4e1432980a8ca7bfdc7e71d7cf
[melted] / src / modules / xine / 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 #include "deinterlace.h"
23
24 #include <framework/mlt_frame.h>
25
26 #include <string.h>
27 #include <stdlib.h>
28
29 /* Linear Blend filter - C version contributed by Rogerio Brito.
30 This algorithm has the same interface as the other functions.
31
32 The destination "screen" (pdst) is constructed from the source
33 screen (psrc[0]) line by line.
34
35 The i-th line of the destination screen is the average of 3 lines
36 from the source screen: the (i-1)-th, i-th and (i+1)-th lines, with
37 the i-th line having weight 2 in the computation.
38
39 Remarks:
40 * each line on pdst doesn't depend on previous lines;
41 * due to the way the algorithm is defined, the first & last lines of the
42 screen aren't deinterlaced.
43
44 */
45 #if 0
46 static void deinterlace_yuv( uint8_t *pdst, uint8_t *psrc, int width, int height )
47 {
48 register int x, y;
49 register uint8_t *l0, *l1, *l2, *l3;
50
51 l0 = pdst; // target line
52 l1 = psrc; // 1st source line
53 l2 = l1 + width; // 2nd source line = line that follows l1
54 l3 = l2 + width; // 3rd source line = line that follows l2
55
56 // Copy the first line
57 memcpy(l0, l1, width);
58 l0 += width;
59
60 for (y = 1; y < height-1; ++y)
61 {
62 // computes avg of: l1 + 2*l2 + l3
63 for (x = 0; x < width; ++x)
64 l0[x] = (l1[x] + (l2[x]<<1) + l3[x]) >> 2;
65
66 // updates the line pointers
67 l1 = l2; l2 = l3; l3 += width;
68 l0 += width;
69 }
70
71 // Copy the last line
72 memcpy(l0, l1, width);
73 }
74 #endif
75
76 /** Do it :-).
77 */
78
79 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
80 {
81 int error = 0;
82 int deinterlace = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "consumer_deinterlace" );
83
84 // Pop the service off the stack
85 mlt_filter filter = mlt_frame_pop_service( this );
86
87 // Determine if we need a writable version or not
88 if ( deinterlace && !writable )
89 writable = !mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "progressive" );
90
91 // Get the input image
92 error = mlt_frame_get_image( this, image, format, width, height, writable );
93
94 // Check that we want progressive and we aren't already progressive
95 if ( deinterlace && *format == mlt_image_yuv422 && *image != NULL )
96 {
97 // Determine deinterlace method
98 char *method_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "method" );
99 int method = DEINTERLACE_LINEARBLEND;
100 char *frame_method_str = mlt_properties_get( MLT_FRAME_PROPERTIES( this ), "deinterlace_method" );
101
102 if ( frame_method_str != NULL )
103 method_str = frame_method_str;
104
105 if ( method_str == NULL )
106 method = DEINTERLACE_LINEARBLEND;
107 else if ( strcmp( method_str, "bob" ) == 0 )
108 method = DEINTERLACE_BOB;
109 else if ( strcmp( method_str, "weave" ) == 0 )
110 method = DEINTERLACE_BOB;
111 else if ( strcmp( method_str, "greedy" ) == 0 )
112 method = DEINTERLACE_GREEDY;
113 else if ( strcmp( method_str, "onefield" ) == 0 )
114 method = DEINTERLACE_ONEFIELD;
115
116 // Deinterlace the image
117 deinterlace_yuv( *image, image, *width * 2, *height, method );
118
119 // Make sure that others know the frame is deinterlaced
120 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "progressive", 1 );
121 }
122
123 return error;
124 }
125
126 /** Deinterlace filter processing - this should be lazy evaluation here...
127 */
128
129 static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
130 {
131 // Push this on to the service stack
132 mlt_frame_push_service( frame, this );
133
134 // Push the get_image method on to the stack
135 mlt_frame_push_get_image( frame, filter_get_image );
136
137 return frame;
138 }
139
140 /** Constructor for the filter.
141 */
142
143 mlt_filter filter_deinterlace_init( void *arg )
144 {
145 mlt_filter this = mlt_filter_new( );
146 if ( this != NULL )
147 {
148 this->process = deinterlace_process;
149 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "method", arg );
150 }
151 return this;
152 }
153