]> git.sesse.net Git - vlc/blob - modules/video_filter/deinterlace/algo_phosphor.c
Qt: change tools accessor
[vlc] / modules / video_filter / deinterlace / algo_phosphor.c
1 /*****************************************************************************
2  * algo_phosphor.c : Phosphor algorithm for the VLC deinterlacer
3  *****************************************************************************
4  * Copyright (C) 2011 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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,
72                          const int i_field, const int i_strength,
73                          bool process_chroma )
74 {
75     assert( p_dst != NULL );
76     assert( i_field == 0 || i_field == 1 );
77     assert( i_strength >= 1 && i_strength <= 3 );
78
79     /* Bitwise ANDing with this clears the i_strength highest bits
80        of each byte */
81     const uint8_t  remove_high_u8 = 0xFF >> i_strength;
82     const uint64_t remove_high_u64 = remove_high_u8 *
83                                             INT64_C(0x0101010101010101);
84
85     /* Process luma.
86
87        For luma, the operation is just a shift + bitwise AND, so we vectorize
88        even in the C version.
89
90        There is an MMX version too, because it performs about twice faster.
91     */
92     int i_plane = Y_PLANE;
93     uint8_t *p_out, *p_out_end;
94     int w = p_dst->p[i_plane].i_visible_pitch;
95     p_out = p_dst->p[i_plane].p_pixels;
96     p_out_end = p_out + p_dst->p[i_plane].i_pitch
97                       * p_dst->p[i_plane].i_visible_lines;
98
99     /* skip first line for bottom field */
100     if( i_field == 1 )
101         p_out += p_dst->p[i_plane].i_pitch;
102
103     int wm8 = w % 8;   /* remainder */
104     int w8  = w - wm8; /* part of width that is divisible by 8 */
105     for( ; p_out < p_out_end ; p_out += 2*p_dst->p[i_plane].i_pitch )
106     {
107         uint64_t *po = (uint64_t *)p_out;
108         int x = 0;
109
110         for( ; x < w8; x += 8, ++po )
111             (*po) = ( ((*po) >> i_strength) & remove_high_u64 );
112
113         /* handle the width remainder */
114         uint8_t *po_temp = (uint8_t *)po;
115         for( ; x < w; ++x, ++po_temp )
116             (*po_temp) = ( ((*po_temp) >> i_strength) & remove_high_u8 );
117     }
118
119     /* Process chroma if the field chromas are independent.
120
121        The origin (black) is at YUV = (0, 128, 128) in the uint8 format.
122        The chroma processing is a bit more complicated than luma,
123        and needs MMX for vectorization.
124     */
125     if( process_chroma )
126     {
127         for( i_plane++ /* luma already handled*/;
128              i_plane < p_dst->i_planes;
129              i_plane++ )
130         {
131             int w = p_dst->p[i_plane].i_visible_pitch;
132             p_out = p_dst->p[i_plane].p_pixels;
133             p_out_end = p_out + p_dst->p[i_plane].i_pitch
134                               * p_dst->p[i_plane].i_visible_lines;
135
136             /* skip first line for bottom field */
137             if( i_field == 1 )
138                 p_out += p_dst->p[i_plane].i_pitch;
139
140             for( ; p_out < p_out_end ; p_out += 2*p_dst->p[i_plane].i_pitch )
141             {
142                 /* Handle the width remainder */
143                 uint8_t *po = p_out;
144                 for( int x = 0; x < w; ++x, ++po )
145                     (*po) = 128 + ( ((*po) - 128) / (1 << i_strength) );
146             } /* for p_out... */
147         } /* for i_plane... */
148     } /* if process_chroma */
149 }
150
151 #ifdef CAN_COMPILE_MMXEXT
152 VLC_MMX
153 static void DarkenFieldMMX( picture_t *p_dst,
154                             const int i_field, const int i_strength,
155                             bool process_chroma )
156 {
157     assert( p_dst != NULL );
158     assert( i_field == 0 || i_field == 1 );
159     assert( i_strength >= 1 && i_strength <= 3 );
160
161     uint64_t i_strength_u64 = i_strength; /* needs to know number of bits */
162     const uint8_t  remove_high_u8 = 0xFF >> i_strength;
163     const uint64_t remove_high_u64 = remove_high_u8 *
164                                             INT64_C(0x0101010101010101);
165
166     int i_plane = Y_PLANE;
167     uint8_t *p_out, *p_out_end;
168     int w = p_dst->p[i_plane].i_visible_pitch;
169     p_out = p_dst->p[i_plane].p_pixels;
170     p_out_end = p_out + p_dst->p[i_plane].i_pitch
171                       * p_dst->p[i_plane].i_visible_lines;
172
173     /* skip first line for bottom field */
174     if( i_field == 1 )
175         p_out += p_dst->p[i_plane].i_pitch;
176
177     int wm8 = w % 8;   /* remainder */
178     int w8  = w - wm8; /* part of width that is divisible by 8 */
179     for( ; p_out < p_out_end ; p_out += 2*p_dst->p[i_plane].i_pitch )
180     {
181         uint64_t *po = (uint64_t *)p_out;
182         int x = 0;
183
184         movq_m2r( i_strength_u64,  mm1 );
185         movq_m2r( remove_high_u64, mm2 );
186         for( ; x < w8; x += 8 )
187         {
188             movq_m2r( (*po), mm0 );
189
190             psrlq_r2r( mm1, mm0 );
191             pand_r2r(  mm2, mm0 );
192
193             movq_r2m( mm0, (*po++) );
194         }
195
196         /* handle the width remainder */
197         uint8_t *po_temp = (uint8_t *)po;
198         for( ; x < w; ++x, ++po_temp )
199             (*po_temp) = ( ((*po_temp) >> i_strength) & remove_high_u8 );
200     }
201
202     /* Process chroma if the field chromas are independent.
203
204        The origin (black) is at YUV = (0, 128, 128) in the uint8 format.
205        The chroma processing is a bit more complicated than luma,
206        and needs MMX for vectorization.
207     */
208     if( process_chroma )
209     {
210         for( i_plane++ /* luma already handled */;
211              i_plane < p_dst->i_planes;
212              i_plane++ )
213         {
214             int w = p_dst->p[i_plane].i_visible_pitch;
215             int wm8 = w % 8;   /* remainder */
216             int w8  = w - wm8; /* part of width that is divisible by 8 */
217
218             p_out = p_dst->p[i_plane].p_pixels;
219             p_out_end = p_out + p_dst->p[i_plane].i_pitch
220                               * p_dst->p[i_plane].i_visible_lines;
221
222             /* skip first line for bottom field */
223             if( i_field == 1 )
224                 p_out += p_dst->p[i_plane].i_pitch;
225
226             for( ; p_out < p_out_end ; p_out += 2*p_dst->p[i_plane].i_pitch )
227             {
228                 int x = 0;
229
230                 /* See also easy-to-read C version below. */
231                 static const mmx_t b128 = { .uq = 0x8080808080808080ULL };
232                 movq_m2r( b128, mm5 );
233                 movq_m2r( i_strength_u64,  mm6 );
234                 movq_m2r( remove_high_u64, mm7 );
235
236                 uint64_t *po8 = (uint64_t *)p_out;
237                 for( ; x < w8; x += 8 )
238                 {
239                     movq_m2r( (*po8), mm0 );
240
241                     movq_r2r( mm5, mm2 ); /* 128 */
242                     movq_r2r( mm0, mm1 ); /* copy of data */
243                     psubusb_r2r( mm2, mm1 ); /* mm1 = max(data - 128, 0) */
244                     psubusb_r2r( mm0, mm2 ); /* mm2 = max(128 - data, 0) */
245
246                     /* >> i_strength */
247                     psrlq_r2r( mm6, mm1 );
248                     psrlq_r2r( mm6, mm2 );
249                     pand_r2r(  mm7, mm1 );
250                     pand_r2r(  mm7, mm2 );
251
252                     /* collect results from pos./neg. parts */
253                     psubb_r2r( mm2, mm1 );
254                     paddb_r2r( mm5, mm1 );
255
256                     movq_r2m( mm1, (*po8++) );
257                 }
258
259                 /* C version - handle the width remainder */
260                 uint8_t *po = p_out;
261                 for( ; x < w; ++x, ++po )
262                     (*po) = 128 + ( ((*po) - 128) / (1 << i_strength) );
263             } /* for p_out... */
264         } /* for i_plane... */
265     } /* if process_chroma */
266
267     emms();
268 }
269 #endif
270
271 /*****************************************************************************
272  * Public functions
273  *****************************************************************************/
274
275 /* See header for function doc. */
276 int RenderPhosphor( filter_t *p_filter,
277                     picture_t *p_dst,
278                     int i_order, int i_field )
279 {
280     assert( p_filter != NULL );
281     assert( p_dst != NULL );
282     assert( i_order >= 0 && i_order <= 2 ); /* 2 = soft field repeat */
283     assert( i_field == 0 || i_field == 1 );
284
285     filter_sys_t *p_sys = p_filter->p_sys;
286
287     /* Last two input frames */
288     picture_t *p_in  = p_sys->pp_history[HISTORY_SIZE-1];
289     picture_t *p_old = p_sys->pp_history[HISTORY_SIZE-2];
290
291     /* Use the same input picture as "old" at the first frame after startup */
292     if( !p_old )
293         p_old = p_in;
294
295     /* If the history mechanism has failed, we can't do anything. */
296     if( !p_in )
297         return VLC_EGENERIC;
298
299     assert( p_old != NULL );
300     assert( p_in != NULL );
301
302     /* Decide sources for top & bottom fields of output. */
303     picture_t *p_in_top    = p_in;
304     picture_t *p_in_bottom = p_in;
305     /* For the first output field this frame,
306        grab "old" field from previous frame. */
307     if( i_order == 0 )
308     {
309         if( i_field == 0 ) /* rendering top field */
310             p_in_bottom = p_old;
311         else /* i_field == 1, rendering bottom field */
312             p_in_top = p_old;
313     }
314
315     compose_chroma_t cc = CC_ALTLINE;
316     if( 2 * p_sys->chroma->p[1].h.num == p_sys->chroma->p[1].h.den &&
317         2 * p_sys->chroma->p[2].h.num == p_sys->chroma->p[2].h.den )
318     {
319         /* Only 420 like chroma */
320         switch( p_sys->phosphor.i_chroma_for_420 )
321         {
322         case PC_BLEND:
323             cc = CC_MERGE;
324             break;
325         case PC_LATEST:
326             if( i_field == 0 )
327                 cc = CC_SOURCE_TOP;
328             else /* i_field == 1 */
329                 cc = CC_SOURCE_BOTTOM;
330             break;
331         case PC_ALTLINE:
332             cc = CC_ALTLINE;
333             break;
334         case PC_UPCONVERT:
335             cc = CC_UPCONVERT;
336             break;
337         default:
338             /* The above are the only possibilities, if there are no bugs. */
339             assert(0);
340             break;
341         }
342     }
343     ComposeFrame( p_filter, p_dst, p_in_top, p_in_bottom, cc, p_filter->fmt_in.video.i_chroma == VLC_CODEC_YV12 );
344
345     /* Simulate phosphor light output decay for the old field.
346
347        The dimmer can also be switched off in the configuration, but that is
348        more of a technical curiosity or an educational toy for advanced users
349        than a useful deinterlacer mode (although it does make telecined
350        material look slightly better than without any filtering).
351
352        In most use cases the dimmer is used.
353     */
354     if( p_sys->phosphor.i_dimmer_strength > 0 )
355     {
356 #ifdef CAN_COMPILE_MMXEXT
357         if( vlc_CPU_MMXEXT() )
358             DarkenFieldMMX( p_dst, !i_field, p_sys->phosphor.i_dimmer_strength,
359                 p_sys->chroma->p[1].h.num == p_sys->chroma->p[1].h.den &&
360                 p_sys->chroma->p[2].h.num == p_sys->chroma->p[2].h.den );
361         else
362 #endif
363             DarkenField( p_dst, !i_field, p_sys->phosphor.i_dimmer_strength,
364                 p_sys->chroma->p[1].h.num == p_sys->chroma->p[1].h.den &&
365                 p_sys->chroma->p[2].h.num == p_sys->chroma->p[2].h.den );
366     }
367     return VLC_SUCCESS;
368 }