]> git.sesse.net Git - vlc/blob - modules/video_filter/sepia.c
Fix sepia video filter in PackedYUV
[vlc] / modules / video_filter / sepia.c
1 /*****************************************************************************
2  * sepia.c : Sepia video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Branko Kokanovic <branko.kokanovic@gmail.com>
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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_cpu.h>
36
37 #include <assert.h>
38 #include "filter_picture.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create      ( vlc_object_t * );
44 static void Destroy     ( vlc_object_t * );
45
46 static void RVSepia( picture_t *, picture_t *, int );
47 static void PlanarI420Sepia( picture_t *, picture_t *, int);
48 static void PackedYUVSepia( picture_t *, picture_t *, int);
49 static picture_t *Filter( filter_t *, picture_t * );
50 inline void Sepia8ySSE2( uint8_t *, const uint8_t *, int );
51 static const char *const ppsz_filter_options[] = {
52     "intensity", NULL
53 };
54
55 /*****************************************************************************
56  * Module descriptor
57  *****************************************************************************/
58 #define SEPIA_INTENSITY_TEXT N_("Sepia intensity")
59 #define SEPIA_INTENSITY_LONGTEXT N_("Intensity of sepia effect" )
60
61 #define CFG_PREFIX "sepia-"
62
63 vlc_module_begin ()
64     set_description( N_("Sepia video filter") )
65     set_shortname( N_("Sepia" ) )
66     set_help( N_("Gives video a warmer tone by applying sepia effect") )
67     set_category( CAT_VIDEO )
68     set_subcategory( SUBCAT_VIDEO_VFILTER )
69     set_capability( "video filter2", 0 )
70     add_integer_with_range( CFG_PREFIX "intensity", 120, 0, 255,
71                            SEPIA_INTENSITY_TEXT, SEPIA_INTENSITY_LONGTEXT,
72                            false )
73     set_callbacks( Create, Destroy )
74 vlc_module_end ()
75
76 /*****************************************************************************
77  * callback prototypes
78  *****************************************************************************/
79 static int FilterCallback( vlc_object_t *, char const *,
80                            vlc_value_t, vlc_value_t, void * );
81
82 typedef void (*SepiaFunction)( picture_t *, picture_t *, int );
83
84 static const struct
85 {
86     vlc_fourcc_t i_chroma;
87     SepiaFunction pf_sepia;
88 } p_sepia_cfg[] = {
89     { VLC_CODEC_I420, PlanarI420Sepia },
90     { VLC_CODEC_RGB24, RVSepia },
91     { VLC_CODEC_RGB32, RVSepia },
92     { VLC_CODEC_UYVY, PackedYUVSepia },
93     { VLC_CODEC_VYUY, PackedYUVSepia },
94     { VLC_CODEC_YUYV, PackedYUVSepia },
95     { VLC_CODEC_YVYU, PackedYUVSepia },
96     { 0, NULL }
97 };
98
99 /*****************************************************************************
100  * filter_sys_t: adjust filter method descriptor
101  *****************************************************************************/
102 struct filter_sys_t
103 {
104     SepiaFunction pf_sepia;
105     int i_intensity;
106     vlc_spinlock_t lock;
107 };
108
109 /*****************************************************************************
110  * Create: allocates Sepia video thread output method
111  *****************************************************************************
112  * This function allocates and initializes a Sepia vout method.
113  *****************************************************************************/
114 static int Create( vlc_object_t *p_this )
115 {
116     filter_t *p_filter = (filter_t *)p_this;
117     filter_sys_t *p_sys;
118
119     /* Allocate structure */
120     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
121     if( p_filter->p_sys == NULL )
122         return VLC_ENOMEM;
123
124     p_sys->pf_sepia = NULL;
125
126     for( int i = 0; p_sepia_cfg[i].i_chroma != 0; i++ )
127     {
128         if( p_sepia_cfg[i].i_chroma != p_filter->fmt_in.video.i_chroma )
129             continue;
130         p_sys->pf_sepia = p_sepia_cfg[i].pf_sepia;
131     }
132
133     if( p_sys->pf_sepia == NULL )
134     {
135         msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
136                 (char*)&(p_filter->fmt_in.video.i_chroma) );
137         free( p_sys );
138         return VLC_EGENERIC;
139     }
140
141     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
142                        p_filter->p_cfg );
143     p_sys->i_intensity= var_CreateGetIntegerCommand( p_filter,
144                        CFG_PREFIX "intensity" );
145
146     vlc_spin_init( &p_sys->lock );
147
148     var_AddCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
149
150     p_filter->pf_video_filter = Filter;
151
152     return VLC_SUCCESS;
153 }
154
155 /*****************************************************************************
156  * Destroy: destroy sepia video thread output method
157  *****************************************************************************
158  * Terminate an output method
159  *****************************************************************************/
160 static void Destroy( vlc_object_t *p_this )
161 {
162     filter_t *p_filter = (filter_t *)p_this;
163
164     var_DelCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
165
166     vlc_spin_destroy( &p_filter->p_sys->lock );
167     free( p_filter->p_sys );
168 }
169
170 /*****************************************************************************
171  * Render: displays previously rendered output
172  *****************************************************************************
173  * This function send the currently rendered image to sepia image, waits
174  * until it is displayed and switch the two rendering buffers, preparing next
175  * frame.
176  *****************************************************************************/
177 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
178 {
179     picture_t *p_outpic;
180     int intensity;
181
182     if( !p_pic ) return NULL;
183
184     filter_sys_t *p_sys = p_filter->p_sys;
185     vlc_spin_lock( &p_sys->lock );
186     intensity = p_sys->i_intensity;
187     vlc_spin_unlock( &p_sys->lock );
188
189     p_outpic = filter_NewPicture( p_filter );
190     if( !p_outpic )
191     {
192         msg_Warn( p_filter, "can't get output picture" );
193         picture_Release( p_pic );
194         return NULL;
195     }
196
197     p_sys->pf_sepia( p_pic, p_outpic, intensity );
198
199     return CopyInfoAndRelease( p_outpic, p_pic );
200 }
201
202 /*****************************************************************************
203  * PlanarI420Sepia: Applies sepia to one frame of the planar I420 video
204  *****************************************************************************
205  * This function applies sepia effect to one frame of the video by iterating
206  * through video lines. We iterate for every two lines and for every two pixels
207  * in line to calculate new sepia values for four y components as well for u
208  * and v components.
209  *****************************************************************************/
210 static void PlanarI420Sepia( picture_t *p_pic, picture_t *p_outpic,
211                                int i_intensity )
212 {
213     // prepared values to copy for U and V channels
214     const uint8_t filling_const_8u = 128 - i_intensity / 6;
215     const uint8_t filling_const_8v = 128 + i_intensity / 14;
216
217 #if defined(CAN_COMPILE_SSE2)
218     if (vlc_CPU() & CPU_CAPABILITY_SSE2)
219     {
220         /* prepared value for faster broadcasting in xmm register */
221         int i_intensity_spread = 0x10001 * (uint8_t) i_intensity;
222
223         __asm__ volatile(
224             "pxor      %%xmm7, %%xmm7\n"
225         ::);
226
227         /* iterate for every two visible line in the frame */
228         for (int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
229         {
230             const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
231             const int i_dy_line2_start =
232             (y + 1) * p_outpic->p[Y_PLANE].i_pitch;
233             const int i_du_line_start =
234             (y / 2) * p_outpic->p[U_PLANE].i_pitch;
235             const int i_dv_line_start =
236             (y / 2) * p_outpic->p[V_PLANE].i_pitch;
237             int x = 0;
238             /* iterate for every visible line in the frame (eight values at once) */
239             for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 15; x += 16 )
240             {
241                 /* Compute yellow channel values with asm function */
242                 Sepia8ySSE2(
243                     &p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
244                     &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
245                     i_intensity_spread );
246                 Sepia8ySSE2(
247                     &p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
248                     &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
249                     i_intensity_spread );
250                 Sepia8ySSE2(
251                     &p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
252                     &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
253                     i_intensity_spread );
254                 Sepia8ySSE2(
255                     &p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
256                     &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
257                     i_intensity_spread );
258                 /* Copy precomputed values to destination memory location */
259                 vlc_memset(
260                     &p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)],
261                     filling_const_8u, 8 );
262                 vlc_memset(
263                     &p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)],
264                     filling_const_8v, 8 );
265             }
266             /* Completing the job, the cycle above takes really big chunks, so
267               this makes sure the job will be done completely */
268             for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 1; x += 2 )
269             {
270                 // y = y - y/4 {to prevent overflow} + intensity / 4
271                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
272                     p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
273                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
274                     (i_intensity >> 2);
275                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
276                     p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
277                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
278                     (i_intensity >> 2);
279                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
280                     p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
281                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
282                     (i_intensity >> 2);
283                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
284                     p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
285                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
286                     (i_intensity >> 2);
287                 // u = 128 {half => B&W} - intensity / 6
288                 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
289                     filling_const_8u;
290                 // v = 128 {half => B&W} + intensity / 14
291                 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
292                     filling_const_8v;
293             }
294         }
295     }
296     else
297 #endif
298     {
299         /* iterate for every two visible line in the frame */
300         for( int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
301         {
302             const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
303             const int i_dy_line2_start = ( y + 1 ) * p_outpic->p[Y_PLANE].i_pitch;
304             const int i_du_line_start = (y/2) * p_outpic->p[U_PLANE].i_pitch;
305             const int i_dv_line_start = (y/2) * p_outpic->p[V_PLANE].i_pitch;
306             // to prevent sigsegv if one pic is smaller (theoretically)
307             int i_picture_size_limit = p_pic->p[Y_PLANE].i_visible_pitch
308                       < p_outpic->p[Y_PLANE].i_visible_pitch
309                       ? (p_pic->p[Y_PLANE].i_visible_pitch - 1) :
310                       (p_outpic->p[Y_PLANE].i_visible_pitch - 1);
311             /* iterate for every two visible line in the frame */
312             for( int x = 0; x < i_picture_size_limit; x += 2)
313             {
314                 // y = y - y/4 {to prevent overflow} + intensity / 4
315                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
316                     p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
317                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
318                     (i_intensity >> 2);
319                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
320                     p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
321                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
322                     (i_intensity >> 2);
323                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
324                     p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
325                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
326                     (i_intensity >> 2);
327                 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
328                     p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
329                     (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
330                     (i_intensity >> 2);
331                 // u = 128 {half => B&W} - intensity / 6
332                 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
333                     filling_const_8u;
334                 // v = 128 {half => B&W} + intensity / 14
335                 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
336                     filling_const_8v;
337             }
338         }
339     }
340 }
341
342 /*****************************************************************************
343  * PackedYUVSepia: Applies sepia to one frame of the packed YUV video
344  *****************************************************************************
345  * This function applies sepia effext to one frame of the video by iterating
346  * through video lines. In every pass, we calculate new values for pixels
347  * (UYVY, VYUY, YUYV and YVYU formats are supported)
348  *****************************************************************************/
349 static void PackedYUVSepia( picture_t *p_pic, picture_t *p_outpic,
350                            int i_intensity )
351 {
352     uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
353     int i_yindex = 1, i_uindex = 2, i_vindex = 0;
354
355     GetPackedYuvOffsets( p_outpic->format.i_chroma,
356                         &i_yindex, &i_uindex, &i_vindex );
357
358     // prepared values to copy for U and V channels
359     const uint8_t filling_const_8u = 128 - i_intensity / 6;
360     const uint8_t filling_const_8v = 128 + i_intensity / 14;
361
362     p_in = p_pic->p[0].p_pixels;
363     p_in_end = p_in + p_pic->p[0].i_visible_lines
364         * p_pic->p[0].i_pitch;
365     p_out = p_outpic->p[0].p_pixels;
366
367     {
368         while( p_in < p_in_end )
369         {
370             p_line_end = p_in + p_pic->p[0].i_visible_pitch;
371             while( p_in < p_line_end )
372             {
373                 /* calculate new, sepia values */
374                 p_out[i_yindex] =
375                     p_in[i_yindex] - (p_in[i_yindex] >> 2) + (i_intensity >> 2);
376                 p_out[i_yindex + 2] =
377                     p_in[i_yindex + 2] - (p_in[i_yindex + 2] >> 2)
378                     + (i_intensity >> 2);
379                 p_out[i_uindex] = filling_const_8u;
380                 p_out[i_vindex] = filling_const_8v;
381                 p_in += 4;
382                 p_out += 4;
383             }
384             p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
385             p_out += p_outpic->p[0].i_pitch
386                 - p_outpic->p[0].i_visible_pitch;
387         }
388     }
389 }
390
391 /*****************************************************************************
392  * RVSepia: Applies sepia to one frame of the RV24/RV32 video
393  *****************************************************************************
394  * This function applies sepia effect to one frame of the video by iterating
395  * through video lines and calculating new values for every byte in chunks of
396  * 3 (RV24) or 4 (RV32) bytes.
397  *****************************************************************************/
398 static void RVSepia( picture_t *p_pic, picture_t *p_outpic, int i_intensity )
399 {
400 #define SCALEBITS 10
401 #define ONE_HALF  (1 << (SCALEBITS - 1))
402 #define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
403     uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
404     bool b_isRV32 = p_pic->format.i_chroma == VLC_CODEC_RGB32;
405     int i_rindex = 0, i_gindex = 1, i_bindex = 2;
406
407     GetPackedRgbIndexes( &p_outpic->format, &i_rindex, &i_gindex, &i_bindex );
408
409     p_in = p_pic->p[0].p_pixels;
410     p_in_end = p_in + p_pic->p[0].i_visible_lines
411         * p_pic->p[0].i_pitch;
412     p_out = p_outpic->p[0].p_pixels;
413
414     /* Precompute values constant for this certain i_intensity, using the same
415      * formula as YUV functions above */
416     uint8_t r_intensity = (( FIX( 1.40200 * 255.0 / 224.0 ) * (i_intensity * 14)
417                         + ONE_HALF )) >> SCALEBITS;
418     uint8_t g_intensity = (( - FIX(0.34414*255.0/224.0) * ( - i_intensity / 6 )
419                         - FIX( 0.71414 * 255.0 / 224.0) * ( i_intensity * 14 )
420                         + ONE_HALF )) >> SCALEBITS;
421     uint8_t b_intensity = (( FIX( 1.77200 * 255.0 / 224.0) * ( - i_intensity / 6 )
422                         + ONE_HALF )) >> SCALEBITS;
423
424     while (p_in < p_in_end)
425     {
426         p_line_end = p_in + p_pic->p[0].i_visible_pitch;
427         while (p_in < p_line_end)
428         {
429             /* do sepia: this calculation is based on the formula to calculate
430              * YUV->RGB and RGB->YUV (in filter_picture.h) mode and that
431              * y = y - y/4 + intensity/4 . As Y is the only channel that changes
432              * through the whole image. After that, precomputed values are added
433              * for each RGB channel and saved in the output image.
434              * FIXME: needs cleanup */
435             uint8_t i_y = ((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] +  25
436                       * p_in[i_bindex] + 128 ) >> 8 ) * FIX(255.0/219.0))
437                       - (((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
438                       * p_in[i_bindex] + 128 ) >> 8 )
439                       * FIX( 255.0 / 219.0 )) >> 2 ) + ( i_intensity >> 2 );
440             p_out[i_rindex] = vlc_uint8(i_y + r_intensity);
441             p_out[i_gindex] = vlc_uint8(i_y + g_intensity);
442             p_out[i_bindex] = vlc_uint8(i_y + b_intensity);
443             p_in += 3;
444             p_out += 3;
445             /* for rv32 we take 4 chunks at the time */
446             if (b_isRV32) {
447             /* alpha channel stays the same */
448             *p_out++ = *p_in++;
449             }
450         }
451
452         p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
453         p_out += p_outpic->p[0].i_pitch
454             - p_outpic->p[0].i_visible_pitch;
455     }
456 #undef SCALEBITS
457 #undef ONE_HALF
458 #undef FIX
459 }
460
461 /*****************************************************************************
462  * Sepia8ySSE2
463  *****************************************************************************
464  * This function applies sepia effect to eight bytes of yellow using SSE4.1
465  * instructions. It copies those 8 bytes to 128b register and fills the gaps
466  * with zeroes and following operations are made with word-operating instructs.
467  *****************************************************************************/
468 inline void Sepia8ySSE2(uint8_t * dst, const uint8_t * src,
469                          int i_intensity_spread)
470 {
471 #if defined(CAN_COMPILE_SSE2)
472     __asm__ volatile (
473         // y = y - y / 4 + i_intensity / 4
474         "movq            (%1), %%xmm1\n"
475         "punpcklbw     %%xmm7, %%xmm1\n"
476         "movq            (%1), %%xmm2\n" // store bytes as words with 0s in between
477         "punpcklbw     %%xmm7, %%xmm2\n"
478         "movd              %2, %%xmm3\n"
479         "pshufd    $0, %%xmm3, %%xmm3\n"
480         "psrlw             $2, %%xmm2\n"    // rotate right 2
481         "psubusb       %%xmm1, %%xmm2\n"    // subtract
482         "psrlw             $2, %%xmm3\n"
483         "paddsb        %%xmm1, %%xmm3\n"    // add
484         "packuswb      %%xmm2, %%xmm1\n"    // pack back to bytes
485         "movq          %%xmm1, (%0)  \n"    // load to dest
486         :
487         :"r" (dst), "r"(src), "r"(i_intensity_spread)
488         :"memory");
489 #endif
490 }
491
492 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
493                             vlc_value_t oldval, vlc_value_t newval,
494                             void *p_data )
495 {
496     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
497     filter_t *p_filter = (filter_t*)p_this;
498     filter_sys_t *p_sys = p_filter->p_sys;
499
500     vlc_spin_lock( &p_sys->lock );
501     p_sys->i_intensity = newval.i_int;
502     vlc_spin_unlock( &p_sys->lock );
503
504     return VLC_SUCCESS;
505 }