1 /*****************************************************************************
2 * sepia.c : Sepia video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2010 VLC authors and VideoLAN
7 * Authors: Branko Kokanovic <branko.kokanovic@gmail.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
35 #include <vlc_picture.h>
37 #include <vlc_atomic.h>
40 #include "filter_picture.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t
* );
46 static void Destroy ( vlc_object_t
* );
48 static void RVSepia( picture_t
*, picture_t
*, int );
49 static void PlanarI420Sepia( picture_t
*, picture_t
*, int);
50 static void PackedYUVSepia( picture_t
*, picture_t
*, int);
51 static picture_t
*Filter( filter_t
*, picture_t
* );
52 static const char *const ppsz_filter_options
[] = {
56 /*****************************************************************************
58 *****************************************************************************/
59 #define SEPIA_INTENSITY_TEXT N_("Sepia intensity")
60 #define SEPIA_INTENSITY_LONGTEXT N_("Intensity of sepia effect" )
62 #define CFG_PREFIX "sepia-"
65 set_description( N_("Sepia video filter") )
66 set_shortname( N_("Sepia" ) )
67 set_help( N_("Gives video a warmer tone by applying sepia effect") )
68 set_category( CAT_VIDEO
)
69 set_subcategory( SUBCAT_VIDEO_VFILTER
)
70 set_capability( "video filter", 0 )
71 add_integer_with_range( CFG_PREFIX
"intensity", 120, 0, 255,
72 SEPIA_INTENSITY_TEXT
, SEPIA_INTENSITY_LONGTEXT
,
74 set_callbacks( Create
, Destroy
)
77 /*****************************************************************************
79 *****************************************************************************/
80 static int FilterCallback( vlc_object_t
*, char const *,
81 vlc_value_t
, vlc_value_t
, void * );
83 typedef void (*SepiaFunction
)( picture_t
*, picture_t
*, int );
87 vlc_fourcc_t i_chroma
;
88 SepiaFunction pf_sepia
;
90 { VLC_CODEC_I420
, PlanarI420Sepia
},
91 { VLC_CODEC_RGB24
, RVSepia
},
92 { VLC_CODEC_RGB32
, RVSepia
},
93 { VLC_CODEC_UYVY
, PackedYUVSepia
},
94 { VLC_CODEC_VYUY
, PackedYUVSepia
},
95 { VLC_CODEC_YUYV
, PackedYUVSepia
},
96 { VLC_CODEC_YVYU
, PackedYUVSepia
},
100 /*****************************************************************************
101 * filter_sys_t: adjust filter method descriptor
102 *****************************************************************************/
105 SepiaFunction pf_sepia
;
106 atomic_int i_intensity
;
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
)
116 filter_t
*p_filter
= (filter_t
*)p_this
;
119 /* Allocate structure */
120 p_sys
= p_filter
->p_sys
= malloc( sizeof( filter_sys_t
) );
121 if( p_filter
->p_sys
== NULL
)
124 p_sys
->pf_sepia
= NULL
;
126 for( int i
= 0; p_sepia_cfg
[i
].i_chroma
!= 0; i
++ )
128 if( p_sepia_cfg
[i
].i_chroma
!= p_filter
->fmt_in
.video
.i_chroma
)
130 p_sys
->pf_sepia
= p_sepia_cfg
[i
].pf_sepia
;
133 if( p_sys
->pf_sepia
== NULL
)
135 msg_Err( p_filter
, "Unsupported input chroma (%4.4s)",
136 (char*)&(p_filter
->fmt_in
.video
.i_chroma
) );
141 config_ChainParse( p_filter
, CFG_PREFIX
, ppsz_filter_options
,
143 atomic_init( &p_sys
->i_intensity
,
144 var_CreateGetIntegerCommand( p_filter
, CFG_PREFIX
"intensity" ) );
145 var_AddCallback( p_filter
, CFG_PREFIX
"intensity", FilterCallback
, NULL
);
147 p_filter
->pf_video_filter
= Filter
;
152 /*****************************************************************************
153 * Destroy: destroy sepia video thread output method
154 *****************************************************************************
155 * Terminate an output method
156 *****************************************************************************/
157 static void Destroy( vlc_object_t
*p_this
)
159 filter_t
*p_filter
= (filter_t
*)p_this
;
161 var_DelCallback( p_filter
, CFG_PREFIX
"intensity", FilterCallback
, NULL
);
163 free( p_filter
->p_sys
);
166 /*****************************************************************************
167 * Render: displays previously rendered output
168 *****************************************************************************
169 * This function send the currently rendered image to sepia image, waits
170 * until it is displayed and switch the two rendering buffers, preparing next
172 *****************************************************************************/
173 static picture_t
*Filter( filter_t
*p_filter
, picture_t
*p_pic
)
177 if( !p_pic
) return NULL
;
179 filter_sys_t
*p_sys
= p_filter
->p_sys
;
180 int intensity
= atomic_load( &p_sys
->i_intensity
);
182 p_outpic
= filter_NewPicture( p_filter
);
185 msg_Warn( p_filter
, "can't get output picture" );
186 picture_Release( p_pic
);
190 p_sys
->pf_sepia( p_pic
, p_outpic
, intensity
);
192 return CopyInfoAndRelease( p_outpic
, p_pic
);
195 #if defined(CAN_COMPILE_SSE2)
196 /*****************************************************************************
198 *****************************************************************************
199 * This function applies sepia effect to eight bytes of yellow using SSE4.1
200 * instructions. It copies those 8 bytes to 128b register and fills the gaps
201 * with zeroes and following operations are made with word-operating instructs.
202 *****************************************************************************/
204 static inline void Sepia8ySSE2(uint8_t * dst
, const uint8_t * src
,
205 int i_intensity_spread
)
208 // y = y - y / 4 + i_intensity / 4
209 "movq (%1), %%xmm1\n"
210 "punpcklbw %%xmm7, %%xmm1\n"
211 "movq (%1), %%xmm2\n" // store bytes as words with 0s in between
212 "punpcklbw %%xmm7, %%xmm2\n"
214 "pshufd $0, %%xmm3, %%xmm3\n"
215 "psrlw $2, %%xmm2\n" // rotate right 2
216 "psubusb %%xmm1, %%xmm2\n" // subtract
218 "paddsb %%xmm1, %%xmm3\n" // add
219 "packuswb %%xmm2, %%xmm1\n" // pack back to bytes
220 "movq %%xmm1, (%0) \n" // load to dest
222 :"r" (dst
), "r"(src
), "r"(i_intensity_spread
)
223 :"memory", "xmm1", "xmm2", "xmm3");
227 static void PlanarI420SepiaSSE( picture_t
*p_pic
, picture_t
*p_outpic
,
230 /* prepared values to copy for U and V channels */
231 const uint8_t filling_const_8u
= 128 - i_intensity
/ 6;
232 const uint8_t filling_const_8v
= 128 + i_intensity
/ 14;
233 /* prepared value for faster broadcasting in xmm register */
234 int i_intensity_spread
= 0x10001 * (uint8_t) i_intensity
;
237 "pxor %%xmm7, %%xmm7\n"
240 /* iterate for every two visible line in the frame */
241 for (int y
= 0; y
< p_pic
->p
[Y_PLANE
].i_visible_lines
- 1; y
+= 2)
243 const int i_dy_line1_start
= y
* p_outpic
->p
[Y_PLANE
].i_pitch
;
244 const int i_dy_line2_start
= (y
+ 1) * p_outpic
->p
[Y_PLANE
].i_pitch
;
245 const int i_du_line_start
= (y
/ 2) * p_outpic
->p
[U_PLANE
].i_pitch
;
246 const int i_dv_line_start
= (y
/ 2) * p_outpic
->p
[V_PLANE
].i_pitch
;
248 /* iterate for every visible line in the frame (eight values at once) */
249 for ( ; x
< p_pic
->p
[Y_PLANE
].i_visible_pitch
- 15; x
+= 16 )
251 /* Compute yellow channel values with asm function */
252 Sepia8ySSE2(&p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
],
253 &p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
],
254 i_intensity_spread
);
255 Sepia8ySSE2(&p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
],
256 &p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
],
257 i_intensity_spread
);
258 Sepia8ySSE2(&p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 8],
259 &p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 8],
260 i_intensity_spread
);
261 Sepia8ySSE2(&p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 8],
262 &p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 8],
263 i_intensity_spread
);
264 /* Copy precomputed values to destination memory location */
265 memset(&p_outpic
->p
[U_PLANE
].p_pixels
[i_du_line_start
+ (x
/ 2)],
266 filling_const_8u
, 8 );
267 memset(&p_outpic
->p
[V_PLANE
].p_pixels
[i_dv_line_start
+ (x
/ 2)],
268 filling_const_8v
, 8 );
270 /* Completing the job, the cycle above takes really big chunks, so
271 this makes sure the job will be done completely */
272 for ( ; x
< p_pic
->p
[Y_PLANE
].i_visible_pitch
- 1; x
+= 2 )
274 // y = y - y/4 {to prevent overflow} + intensity / 4
275 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
] =
276 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
] -
277 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
] >> 2) +
279 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 1] =
280 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 1] -
281 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 1] >> 2) +
283 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
] =
284 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
] -
285 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
] >> 2) +
287 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 1] =
288 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 1] -
289 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 1] >> 2) +
291 // u = 128 {half => B&W} - intensity / 6
292 p_outpic
->p
[U_PLANE
].p_pixels
[i_du_line_start
+ (x
/ 2)] =
294 // v = 128 {half => B&W} + intensity / 14
295 p_outpic
->p
[V_PLANE
].p_pixels
[i_dv_line_start
+ (x
/ 2)] =
302 /*****************************************************************************
303 * PlanarI420Sepia: Applies sepia to one frame of the planar I420 video
304 *****************************************************************************
305 * This function applies sepia effect to one frame of the video by iterating
306 * through video lines. We iterate for every two lines and for every two pixels
307 * in line to calculate new sepia values for four y components as well for u
309 *****************************************************************************/
310 static void PlanarI420Sepia( picture_t
*p_pic
, picture_t
*p_outpic
,
313 #if defined(CAN_COMPILE_SSE2)
315 return PlanarI420SepiaSSE( p_pic
, p_outpic
, i_intensity
);
318 // prepared values to copy for U and V channels
319 const uint8_t filling_const_8u
= 128 - i_intensity
/ 6;
320 const uint8_t filling_const_8v
= 128 + i_intensity
/ 14;
322 /* iterate for every two visible line in the frame */
323 for( int y
= 0; y
< p_pic
->p
[Y_PLANE
].i_visible_lines
- 1; y
+= 2 )
325 const int i_dy_line1_start
= y
* p_outpic
->p
[Y_PLANE
].i_pitch
;
326 const int i_dy_line2_start
= ( y
+ 1 ) * p_outpic
->p
[Y_PLANE
].i_pitch
;
327 const int i_du_line_start
= (y
/2) * p_outpic
->p
[U_PLANE
].i_pitch
;
328 const int i_dv_line_start
= (y
/2) * p_outpic
->p
[V_PLANE
].i_pitch
;
329 // to prevent sigsegv if one pic is smaller (theoretically)
330 int i_picture_size_limit
= p_pic
->p
[Y_PLANE
].i_visible_pitch
331 < p_outpic
->p
[Y_PLANE
].i_visible_pitch
332 ? (p_pic
->p
[Y_PLANE
].i_visible_pitch
- 1) :
333 (p_outpic
->p
[Y_PLANE
].i_visible_pitch
- 1);
334 /* iterate for every two visible line in the frame */
335 for( int x
= 0; x
< i_picture_size_limit
; x
+= 2 )
337 // y = y - y/4 {to prevent overflow} + intensity / 4
338 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
] =
339 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
] -
340 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
] >> 2) +
342 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 1] =
343 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 1] -
344 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line1_start
+ x
+ 1] >> 2) +
346 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
] =
347 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
] -
348 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
] >> 2) +
350 p_outpic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 1] =
351 p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 1] -
352 (p_pic
->p
[Y_PLANE
].p_pixels
[i_dy_line2_start
+ x
+ 1] >> 2) +
354 // u = 128 {half => B&W} - intensity / 6
355 p_outpic
->p
[U_PLANE
].p_pixels
[i_du_line_start
+ (x
/ 2)] =
357 // v = 128 {half => B&W} + intensity / 14
358 p_outpic
->p
[V_PLANE
].p_pixels
[i_dv_line_start
+ (x
/ 2)] =
364 /*****************************************************************************
365 * PackedYUVSepia: Applies sepia to one frame of the packed YUV video
366 *****************************************************************************
367 * This function applies sepia effext to one frame of the video by iterating
368 * through video lines. In every pass, we calculate new values for pixels
369 * (UYVY, VYUY, YUYV and YVYU formats are supported)
370 *****************************************************************************/
371 static void PackedYUVSepia( picture_t
*p_pic
, picture_t
*p_outpic
,
374 uint8_t *p_in
, *p_in_end
, *p_line_end
, *p_out
;
375 int i_yindex
= 1, i_uindex
= 2, i_vindex
= 0;
377 GetPackedYuvOffsets( p_outpic
->format
.i_chroma
,
378 &i_yindex
, &i_uindex
, &i_vindex
);
380 // prepared values to copy for U and V channels
381 const uint8_t filling_const_8u
= 128 - i_intensity
/ 6;
382 const uint8_t filling_const_8v
= 128 + i_intensity
/ 14;
384 p_in
= p_pic
->p
[0].p_pixels
;
385 p_in_end
= p_in
+ p_pic
->p
[0].i_visible_lines
386 * p_pic
->p
[0].i_pitch
;
387 p_out
= p_outpic
->p
[0].p_pixels
;
390 while( p_in
< p_in_end
)
392 p_line_end
= p_in
+ p_pic
->p
[0].i_visible_pitch
;
393 while( p_in
< p_line_end
)
395 /* calculate new, sepia values */
397 p_in
[i_yindex
] - (p_in
[i_yindex
] >> 2) + (i_intensity
>> 2);
398 p_out
[i_yindex
+ 2] =
399 p_in
[i_yindex
+ 2] - (p_in
[i_yindex
+ 2] >> 2)
400 + (i_intensity
>> 2);
401 p_out
[i_uindex
] = filling_const_8u
;
402 p_out
[i_vindex
] = filling_const_8v
;
406 p_in
+= p_pic
->p
[0].i_pitch
- p_pic
->p
[0].i_visible_pitch
;
407 p_out
+= p_outpic
->p
[0].i_pitch
408 - p_outpic
->p
[0].i_visible_pitch
;
413 /*****************************************************************************
414 * RVSepia: Applies sepia to one frame of the RV24/RV32 video
415 *****************************************************************************
416 * This function applies sepia effect to one frame of the video by iterating
417 * through video lines and calculating new values for every byte in chunks of
418 * 3 (RV24) or 4 (RV32) bytes.
419 *****************************************************************************/
420 static void RVSepia( picture_t
*p_pic
, picture_t
*p_outpic
, int i_intensity
)
423 #define ONE_HALF (1 << (SCALEBITS - 1))
424 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
425 uint8_t *p_in
, *p_in_end
, *p_line_end
, *p_out
;
426 bool b_isRV32
= p_pic
->format
.i_chroma
== VLC_CODEC_RGB32
;
427 int i_rindex
= 0, i_gindex
= 1, i_bindex
= 2;
429 GetPackedRgbIndexes( &p_outpic
->format
, &i_rindex
, &i_gindex
, &i_bindex
);
431 p_in
= p_pic
->p
[0].p_pixels
;
432 p_in_end
= p_in
+ p_pic
->p
[0].i_visible_lines
433 * p_pic
->p
[0].i_pitch
;
434 p_out
= p_outpic
->p
[0].p_pixels
;
436 /* Precompute values constant for this certain i_intensity, using the same
437 * formula as YUV functions above */
438 uint8_t r_intensity
= (( FIX( 1.40200 * 255.0 / 224.0 ) * (i_intensity
* 14)
439 + ONE_HALF
)) >> SCALEBITS
;
440 uint8_t g_intensity
= (( - FIX(0.34414*255.0/224.0) * ( - i_intensity
/ 6 )
441 - FIX( 0.71414 * 255.0 / 224.0) * ( i_intensity
* 14 )
442 + ONE_HALF
)) >> SCALEBITS
;
443 uint8_t b_intensity
= (( FIX( 1.77200 * 255.0 / 224.0) * ( - i_intensity
/ 6 )
444 + ONE_HALF
)) >> SCALEBITS
;
446 while (p_in
< p_in_end
)
448 p_line_end
= p_in
+ p_pic
->p
[0].i_visible_pitch
;
449 while (p_in
< p_line_end
)
451 /* do sepia: this calculation is based on the formula to calculate
452 * YUV->RGB and RGB->YUV (in filter_picture.h) mode and that
453 * y = y - y/4 + intensity/4 . As Y is the only channel that changes
454 * through the whole image. After that, precomputed values are added
455 * for each RGB channel and saved in the output image.
456 * FIXME: needs cleanup */
457 uint8_t i_y
= ((( 66 * p_in
[i_rindex
] + 129 * p_in
[i_gindex
] + 25
458 * p_in
[i_bindex
] + 128 ) >> 8 ) * FIX(255.0/219.0))
459 - (((( 66 * p_in
[i_rindex
] + 129 * p_in
[i_gindex
] + 25
460 * p_in
[i_bindex
] + 128 ) >> 8 )
461 * FIX( 255.0 / 219.0 )) >> 2 ) + ( i_intensity
>> 2 );
462 p_out
[i_rindex
] = vlc_uint8(i_y
+ r_intensity
);
463 p_out
[i_gindex
] = vlc_uint8(i_y
+ g_intensity
);
464 p_out
[i_bindex
] = vlc_uint8(i_y
+ b_intensity
);
467 /* for rv32 we take 4 chunks at the time */
469 /* alpha channel stays the same */
474 p_in
+= p_pic
->p
[0].i_pitch
- p_pic
->p
[0].i_visible_pitch
;
475 p_out
+= p_outpic
->p
[0].i_pitch
476 - p_outpic
->p
[0].i_visible_pitch
;
483 static int FilterCallback ( vlc_object_t
*p_this
, char const *psz_var
,
484 vlc_value_t oldval
, vlc_value_t newval
,
487 VLC_UNUSED(psz_var
); VLC_UNUSED(oldval
); VLC_UNUSED(p_data
);
488 filter_t
*p_filter
= (filter_t
*)p_this
;
489 filter_sys_t
*p_sys
= p_filter
->p_sys
;
491 atomic_store( &p_sys
->i_intensity
, newval
.i_int
);