]> git.sesse.net Git - vlc/blob - modules/video_filter/deinterlace/algo_phosphor.c
deinterlace: u_cpu is used only if MMXEXT can be compiled
[vlc] / modules / video_filter / deinterlace / algo_phosphor.c
1 /*****************************************************************************
2  * algo_phosphor.c : Phosphor algorithm for the VLC deinterlacer
3  *****************************************************************************
4  * Copyright (C) 2011 the VideoLAN team
5  * $Id$
6  *
7  * Author: Juha Jeronen <juha.jeronen@jyu.fi>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #   include "config.h"
26 #endif
27
28 #ifdef CAN_COMPILE_MMXEXT
29 #   include "mmx.h"
30 #endif
31
32 #include <stdint.h>
33 #include <assert.h>
34
35 #include <vlc_common.h>
36 #include <vlc_cpu.h>
37 #include <vlc_picture.h>
38 #include <vlc_filter.h>
39
40 #include "deinterlace.h" /* filter_sys_t */
41 #include "helpers.h"     /* ComposeFrame() */
42
43 #include "algo_phosphor.h"
44
45 /*****************************************************************************
46  * Internal functions
47  *****************************************************************************/
48
49 /**
50  * Internal helper function: dims (darkens) the given field
51  * of the given picture.
52  *
53  * This is used for simulating CRT light output decay in RenderPhosphor().
54  *
55  * The strength "1" is recommended. It's a matter of taste,
56  * so it's parametrized.
57  *
58  * Note on chroma formats:
59  *   - If input is 4:2:2, all planes are processed.
60  *   - If input is 4:2:0, only the luma plane is processed, because both fields
61  *     have the same chroma. This will distort colours, especially for high
62  *     filter strengths, especially for pixels whose U and/or V values are
63  *     far away from the origin (which is at 128 in uint8 format).
64  *
65  * @param p_dst Input/output picture. Will be modified in-place.
66  * @param i_field Darken which field? 0 = top, 1 = bottom.
67  * @param i_strength Strength of effect: 1, 2 or 3 (division by 2, 4 or 8).
68  * @see RenderPhosphor()
69  * @see ComposeFrame()
70  */
71 static void DarkenField( picture_t *p_dst, const int i_field,
72                                            const int i_strength )
73 {
74     assert( p_dst != NULL );
75     assert( i_field == 0 || i_field == 1 );
76     assert( i_strength >= 1 && i_strength <= 3 );
77
78     /* Bitwise ANDing with this clears the i_strength highest bits
79        of each byte */
80 #ifdef CAN_COMPILE_MMXEXT
81     unsigned u_cpu = vlc_CPU();
82     uint64_t i_strength_u64 = i_strength; /* for MMX version (needs to know
83                                              number of bits) */
84 #endif
85     const uint8_t  remove_high_u8 = 0xFF >> i_strength;
86     const uint64_t remove_high_u64 = remove_high_u8 *
87                                             INT64_C(0x0101010101010101);
88
89     /* Process luma.
90
91        For luma, the operation is just a shift + bitwise AND, so we vectorize
92        even in the C version.
93
94        There is an MMX version, too, because it performs about twice faster.
95     */
96     int i_plane = Y_PLANE;
97     uint8_t *p_out, *p_out_end;
98     int w = p_dst->p[i_plane].i_visible_pitch;
99     p_out = p_dst->p[i_plane].p_pixels;
100     p_out_end = p_out + p_dst->p[i_plane].i_pitch
101                       * p_dst->p[i_plane].i_visible_lines;
102
103     /* skip first line for bottom field */
104     if( i_field == 1 )
105         p_out += p_dst->p[i_plane].i_pitch;
106
107     int wm8 = w % 8;   /* remainder */
108     int w8  = w - wm8; /* part of width that is divisible by 8 */
109     for( ; p_out < p_out_end ; p_out += 2*p_dst->p[i_plane].i_pitch )
110     {
111         uint64_t *po = (uint64_t *)p_out;
112 #ifdef CAN_COMPILE_MMXEXT
113         if( u_cpu & CPU_CAPABILITY_MMXEXT )
114         {
115             movq_m2r( i_strength_u64,  mm1 );
116             movq_m2r( remove_high_u64, mm2 );
117             for( int x = 0 ; x < w8; x += 8 )
118             {
119                 movq_m2r( (*po), mm0 );
120
121                 psrlq_r2r( mm1, mm0 );
122                 pand_r2r(  mm2, mm0 );
123
124                 movq_r2m( mm0, (*po++) );
125             }
126         }
127         else
128         {
129 #endif
130             for( int x = 0 ; x < w8; x += 8, ++po )
131                 (*po) = ( ((*po) >> i_strength) & remove_high_u64 );
132 #ifdef CAN_COMPILE_MMXEXT
133         }
134 #endif
135         /* handle the width remainder */
136         if( wm8 )
137         {
138             uint8_t *po_temp = (uint8_t *)po;
139             for( int x = 0 ; x < wm8; ++x, ++po_temp )
140                 (*po_temp) = ( ((*po_temp) >> i_strength) & remove_high_u8 );
141         }
142     }
143
144     /* Process chroma if the field chromas are independent.
145
146        The origin (black) is at YUV = (0, 128, 128) in the uint8 format.
147        The chroma processing is a bit more complicated than luma,
148        and needs MMX for vectorization.
149     */
150     if( p_dst->format.i_chroma == VLC_CODEC_I422  ||
151         p_dst->format.i_chroma == VLC_CODEC_J422 )
152     {
153         for( i_plane = 0 ; i_plane < p_dst->i_planes ; i_plane++ )
154         {
155             if( i_plane == Y_PLANE )
156                 continue; /* luma already handled */
157
158             int w = p_dst->p[i_plane].i_visible_pitch;
159 #ifdef CAN_COMPILE_MMXEXT
160             int wm8 = w % 8;   /* remainder */
161             int w8  = w - wm8; /* part of width that is divisible by 8 */
162 #endif
163             p_out = p_dst->p[i_plane].p_pixels;
164             p_out_end = p_out + p_dst->p[i_plane].i_pitch
165                               * p_dst->p[i_plane].i_visible_lines;
166
167             /* skip first line for bottom field */
168             if( i_field == 1 )
169                 p_out += p_dst->p[i_plane].i_pitch;
170
171             for( ; p_out < p_out_end ; p_out += 2*p_dst->p[i_plane].i_pitch )
172             {
173 #ifdef CAN_COMPILE_MMXEXT
174                 /* See also easy-to-read C version below. */
175                 if( u_cpu & CPU_CAPABILITY_MMXEXT )
176                 {
177                     static const mmx_t b128 = { .uq = 0x8080808080808080ULL };
178                     movq_m2r( b128, mm5 );
179                     movq_m2r( i_strength_u64,  mm6 );
180                     movq_m2r( remove_high_u64, mm7 );
181
182                     uint64_t *po = (uint64_t *)p_out;
183                     for( int x = 0 ; x < w8; x += 8 )
184                     {
185                         movq_m2r( (*po), mm0 );
186
187                         movq_r2r( mm5, mm2 ); /* 128 */
188                         movq_r2r( mm0, mm1 ); /* copy of data */
189                         psubusb_r2r( mm2, mm1 ); /* mm1 = max(data - 128, 0) */
190                         psubusb_r2r( mm0, mm2 ); /* mm2 = max(128 - data, 0) */
191
192                         /* >> i_strength */
193                         psrlq_r2r( mm6, mm1 );
194                         psrlq_r2r( mm6, mm2 );
195                         pand_r2r(  mm7, mm1 );
196                         pand_r2r(  mm7, mm2 );
197
198                         /* collect results from pos./neg. parts */
199                         psubb_r2r( mm2, mm1 );
200                         paddb_r2r( mm5, mm1 );
201
202                         movq_r2m( mm1, (*po++) );
203                     }
204
205                     /* handle the width remainder */
206                     if( wm8 )
207                     {
208                         /* The output is closer to 128 than the input;
209                            the result always fits in uint8. */
210                         uint8_t *po8 = (uint8_t *)po;
211                         for( int x = 0 ; x < wm8; ++x, ++po8 )
212                             (*po8) = 128 + ( ((*po8) - 128) /
213                                                   (1 << i_strength) );
214                     }
215                 }
216                 else
217                 {
218 #endif
219                     /* 4:2:2 chroma handler, C version */
220                     uint8_t *po = p_out;
221                     for( int x = 0 ; x < w; ++x, ++po )
222                         (*po) = 128 + ( ((*po) - 128) / (1 << i_strength) );
223 #ifdef CAN_COMPILE_MMXEXT
224                 }
225 #endif
226             } /* for p_out... */
227         } /* for i_plane... */
228     } /* if b_i422 */
229
230 #ifdef CAN_COMPILE_MMXEXT
231     if( u_cpu & CPU_CAPABILITY_MMXEXT )
232         emms();
233 #endif
234 }
235
236 /*****************************************************************************
237  * Public functions
238  *****************************************************************************/
239
240 /* See header for function doc. */
241 int RenderPhosphor( filter_t *p_filter,
242                     picture_t *p_dst,
243                     int i_order, int i_field )
244 {
245     assert( p_filter != NULL );
246     assert( p_dst != NULL );
247     assert( i_order >= 0 && i_order <= 2 ); /* 2 = soft field repeat */
248     assert( i_field == 0 || i_field == 1 );
249
250     filter_sys_t *p_sys = p_filter->p_sys;
251
252     /* Last two input frames */
253     picture_t *p_in  = p_sys->pp_history[HISTORY_SIZE-1];
254     picture_t *p_old = p_sys->pp_history[HISTORY_SIZE-2];
255
256     /* Use the same input picture as "old" at the first frame after startup */
257     if( !p_old )
258         p_old = p_in;
259
260     /* If the history mechanism has failed, we can't do anything. */
261     if( !p_in )
262         return VLC_EGENERIC;
263
264     assert( p_old != NULL );
265     assert( p_in != NULL );
266
267     /* Decide sources for top & bottom fields of output. */
268     picture_t *p_in_top    = p_in;
269     picture_t *p_in_bottom = p_in;
270     /* For the first output field this frame,
271        grab "old" field from previous frame. */
272     if( i_order == 0 )
273     {
274         if( i_field == 0 ) /* rendering top field */
275             p_in_bottom = p_old;
276         else /* i_field == 1, rendering bottom field */
277             p_in_top = p_old;
278     }
279
280     compose_chroma_t cc = CC_ALTLINE; /* initialize to prevent compiler warning */
281     switch( p_sys->phosphor.i_chroma_for_420 )
282     {
283         case PC_BLEND:
284             cc = CC_MERGE;
285             break;
286         case PC_LATEST:
287             if( i_field == 0 )
288                 cc = CC_SOURCE_TOP;
289             else /* i_field == 1 */
290                 cc = CC_SOURCE_BOTTOM;
291             break;
292         case PC_ALTLINE:
293             cc = CC_ALTLINE;
294             break;
295         case PC_UPCONVERT:
296             cc = CC_UPCONVERT;
297             break;
298         default:
299             /* The above are the only possibilities, if there are no bugs. */
300             assert(0);
301             break;
302     }
303
304     ComposeFrame( p_filter, p_dst, p_in_top, p_in_bottom, cc );
305
306     /* Simulate phosphor light output decay for the old field.
307
308        The dimmer can also be switched off in the configuration, but that is
309        more of a technical curiosity or an educational toy for advanced users
310        than a useful deinterlacer mode (although it does make telecined
311        material look slightly better than without any filtering).
312
313        In most use cases the dimmer is used.
314     */
315     if( p_sys->phosphor.i_dimmer_strength > 0 )
316         DarkenField( p_dst, !i_field, p_sys->phosphor.i_dimmer_strength );
317
318     return VLC_SUCCESS;
319 }