1 /*****************************************************************************
2 * sepia.c : Sepia video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2010 the VideoLAN team
7 * Authors: Branko Kokanovic <branko.kokanovic@gmail.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_filter.h>
38 #include "filter_picture.h"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Create ( vlc_object_t * );
44 static void Destroy ( vlc_object_t * );
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[] = {
55 /*****************************************************************************
57 *****************************************************************************/
58 #define SEPIA_INTENSITY_TEXT N_("Sepia intensity")
59 #define SEPIA_INTENSITY_LONGTEXT N_("Intensity of sepia effect" )
61 #define CFG_PREFIX "sepia-"
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,
73 set_callbacks( Create, Destroy )
76 /*****************************************************************************
78 *****************************************************************************/
79 static int FilterCallback( vlc_object_t *, char const *,
80 vlc_value_t, vlc_value_t, void * );
82 typedef void (*SepiaFunction)( picture_t *, picture_t *, int );
86 vlc_fourcc_t i_chroma;
87 SepiaFunction pf_sepia;
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 },
99 /*****************************************************************************
100 * filter_sys_t: adjust filter method descriptor
101 *****************************************************************************/
104 SepiaFunction pf_sepia;
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 p_sys->i_intensity= var_CreateGetIntegerCommand( p_filter,
144 CFG_PREFIX "intensity" );
146 vlc_spin_init( &p_sys->lock );
148 var_AddCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
150 p_filter->pf_video_filter = Filter;
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 )
162 filter_t *p_filter = (filter_t *)p_this;
164 var_DelCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
166 vlc_spin_destroy( &p_filter->p_sys->lock );
167 free( p_filter->p_sys );
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
176 *****************************************************************************/
177 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
182 if( !p_pic ) return NULL;
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 );
189 p_outpic = filter_NewPicture( p_filter );
192 msg_Warn( p_filter, "can't get output picture" );
193 picture_Release( p_pic );
197 p_sys->pf_sepia( p_pic, p_outpic, intensity );
199 return CopyInfoAndRelease( p_outpic, p_pic );
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
209 *****************************************************************************/
210 static void PlanarI420Sepia( picture_t *p_pic, picture_t *p_outpic,
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;
217 #if defined(CAN_COMPILE_SSE2)
218 if (vlc_CPU() & CPU_CAPABILITY_SSE2)
220 /* prepared value for faster broadcasting in xmm register */
221 int i_intensity_spread = 0x10001 * (uint8_t) i_intensity;
224 "pxor %%xmm7, %%xmm7\n"
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)
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;
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 )
241 /* Compute yellow channel values with asm function */
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 );
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 );
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 );
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 */
260 &p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)],
261 filling_const_8u, 8 );
263 &p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)],
264 filling_const_8v, 8 );
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 )
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) +
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) +
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) +
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) +
287 // u = 128 {half => B&W} - intensity / 6
288 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
290 // v = 128 {half => B&W} + intensity / 14
291 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
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)
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)
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) +
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) +
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) +
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) +
331 // u = 128 {half => B&W} - intensity / 6
332 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
334 // v = 128 {half => B&W} + intensity / 14
335 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
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,
352 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
353 int i_yindex = 1, i_uindex = 2, i_vindex = 0;
355 GetPackedYuvOffsets( p_outpic->format.i_chroma,
356 &i_yindex, &i_uindex, &i_vindex );
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;
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;
368 while( p_in < p_in_end )
370 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
371 while( p_in < p_line_end )
373 /* calculate new, sepia values */
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;
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;
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 )
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;
407 GetPackedRgbIndexes( &p_outpic->format, &i_rindex, &i_gindex, &i_bindex );
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;
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;
424 while (p_in < p_in_end)
426 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
427 while (p_in < p_line_end)
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);
445 /* for rv32 we take 4 chunks at the time */
447 /* alpha channel stays the same */
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;
461 /*****************************************************************************
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)
471 #if defined(CAN_COMPILE_SSE2)
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"
479 "pshufd $0, %%xmm3, %%xmm3\n"
480 "psrlw $2, %%xmm2\n" // rotate right 2
481 "psubusb %%xmm1, %%xmm2\n" // subtract
483 "paddsb %%xmm1, %%xmm3\n" // add
484 "packuswb %%xmm2, %%xmm1\n" // pack back to bytes
485 "movq %%xmm1, (%0) \n" // load to dest
487 :"r" (dst), "r"(src), "r"(i_intensity_spread)
492 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
493 vlc_value_t oldval, vlc_value_t newval,
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;
500 vlc_spin_lock( &p_sys->lock );
501 p_sys->i_intensity = newval.i_int;
502 vlc_spin_unlock( &p_sys->lock );