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 static const char *const ppsz_filter_options[] = {
54 /*****************************************************************************
56 *****************************************************************************/
57 #define SEPIA_INTENSITY_TEXT N_("Sepia intensity")
58 #define SEPIA_INTENSITY_LONGTEXT N_("Intensity of sepia effect" )
60 #define CFG_PREFIX "sepia-"
63 set_description( N_("Sepia video filter") )
64 set_shortname( N_("Sepia" ) )
65 set_help( N_("Gives video a warmer tone by applying sepia effect") )
66 set_category( CAT_VIDEO )
67 set_subcategory( SUBCAT_VIDEO_VFILTER )
68 set_capability( "video filter2", 0 )
69 add_integer_with_range( CFG_PREFIX "intensity", 120, 0, 255,
70 SEPIA_INTENSITY_TEXT, SEPIA_INTENSITY_LONGTEXT,
72 set_callbacks( Create, Destroy )
75 /*****************************************************************************
77 *****************************************************************************/
78 static int FilterCallback( vlc_object_t *, char const *,
79 vlc_value_t, vlc_value_t, void * );
81 typedef void (*SepiaFunction)( picture_t *, picture_t *, int );
85 vlc_fourcc_t i_chroma;
86 SepiaFunction pf_sepia;
88 { VLC_CODEC_I420, PlanarI420Sepia },
89 { VLC_CODEC_RGB24, RVSepia },
90 { VLC_CODEC_RGB32, RVSepia },
91 { VLC_CODEC_UYVY, PackedYUVSepia },
92 { VLC_CODEC_VYUY, PackedYUVSepia },
93 { VLC_CODEC_YUYV, PackedYUVSepia },
94 { VLC_CODEC_YVYU, PackedYUVSepia },
98 /*****************************************************************************
99 * filter_sys_t: adjust filter method descriptor
100 *****************************************************************************/
103 SepiaFunction pf_sepia;
108 /*****************************************************************************
109 * Create: allocates Sepia video thread output method
110 *****************************************************************************
111 * This function allocates and initializes a Sepia vout method.
112 *****************************************************************************/
113 static int Create( vlc_object_t *p_this )
115 filter_t *p_filter = (filter_t *)p_this;
118 /* Allocate structure */
119 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
120 if( p_filter->p_sys == NULL )
123 p_sys->pf_sepia = NULL;
125 for( int i = 0; p_sepia_cfg[i].i_chroma != 0; i++ )
127 if( p_sepia_cfg[i].i_chroma != p_filter->fmt_in.video.i_chroma )
129 p_sys->pf_sepia = p_sepia_cfg[i].pf_sepia;
132 if( p_sys->pf_sepia == NULL )
134 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
135 (char*)&(p_filter->fmt_in.video.i_chroma) );
140 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
142 p_sys->i_intensity= var_CreateGetIntegerCommand( p_filter,
143 CFG_PREFIX "intensity" );
145 vlc_spin_init( &p_sys->lock );
147 var_AddCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
149 p_filter->pf_video_filter = Filter;
154 /*****************************************************************************
155 * Destroy: destroy sepia video thread output method
156 *****************************************************************************
157 * Terminate an output method
158 *****************************************************************************/
159 static void Destroy( vlc_object_t *p_this )
161 filter_t *p_filter = (filter_t *)p_this;
163 var_DelCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
165 vlc_spin_destroy( &p_filter->p_sys->lock );
166 free( p_filter->p_sys );
169 /*****************************************************************************
170 * Render: displays previously rendered output
171 *****************************************************************************
172 * This function send the currently rendered image to sepia image, waits
173 * until it is displayed and switch the two rendering buffers, preparing next
175 *****************************************************************************/
176 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
181 if( !p_pic ) return NULL;
183 filter_sys_t *p_sys = p_filter->p_sys;
184 vlc_spin_lock( &p_sys->lock );
185 intensity = p_sys->i_intensity;
186 vlc_spin_unlock( &p_sys->lock );
188 p_outpic = filter_NewPicture( p_filter );
191 msg_Warn( p_filter, "can't get output picture" );
192 picture_Release( p_pic );
196 p_sys->pf_sepia( p_pic, p_outpic, intensity );
198 return CopyInfoAndRelease( p_outpic, p_pic );
201 #if defined(CAN_COMPILE_SSE2)
202 /*****************************************************************************
204 *****************************************************************************
205 * This function applies sepia effect to eight bytes of yellow using SSE4.1
206 * instructions. It copies those 8 bytes to 128b register and fills the gaps
207 * with zeroes and following operations are made with word-operating instructs.
208 *****************************************************************************/
209 static inline void Sepia8ySSE2(uint8_t * dst, const uint8_t * src,
210 int i_intensity_spread)
213 // y = y - y / 4 + i_intensity / 4
214 "movq (%1), %%xmm1\n"
215 "punpcklbw %%xmm7, %%xmm1\n"
216 "movq (%1), %%xmm2\n" // store bytes as words with 0s in between
217 "punpcklbw %%xmm7, %%xmm2\n"
219 "pshufd $0, %%xmm3, %%xmm3\n"
220 "psrlw $2, %%xmm2\n" // rotate right 2
221 "psubusb %%xmm1, %%xmm2\n" // subtract
223 "paddsb %%xmm1, %%xmm3\n" // add
224 "packuswb %%xmm2, %%xmm1\n" // pack back to bytes
225 "movq %%xmm1, (%0) \n" // load to dest
227 :"r" (dst), "r"(src), "r"(i_intensity_spread)
232 /*****************************************************************************
233 * PlanarI420Sepia: Applies sepia to one frame of the planar I420 video
234 *****************************************************************************
235 * This function applies sepia effect to one frame of the video by iterating
236 * through video lines. We iterate for every two lines and for every two pixels
237 * in line to calculate new sepia values for four y components as well for u
239 *****************************************************************************/
240 static void PlanarI420Sepia( picture_t *p_pic, picture_t *p_outpic,
243 // prepared values to copy for U and V channels
244 const uint8_t filling_const_8u = 128 - i_intensity / 6;
245 const uint8_t filling_const_8v = 128 + i_intensity / 14;
247 #if defined(CAN_COMPILE_SSE2)
248 if (vlc_CPU() & CPU_CAPABILITY_SSE2)
250 /* prepared value for faster broadcasting in xmm register */
251 int i_intensity_spread = 0x10001 * (uint8_t) i_intensity;
254 "pxor %%xmm7, %%xmm7\n"
257 /* iterate for every two visible line in the frame */
258 for (int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
260 const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
261 const int i_dy_line2_start =
262 (y + 1) * p_outpic->p[Y_PLANE].i_pitch;
263 const int i_du_line_start =
264 (y / 2) * p_outpic->p[U_PLANE].i_pitch;
265 const int i_dv_line_start =
266 (y / 2) * p_outpic->p[V_PLANE].i_pitch;
268 /* iterate for every visible line in the frame (eight values at once) */
269 for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 15; x += 16 )
271 /* Compute yellow channel values with asm function */
273 &p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
274 &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
275 i_intensity_spread );
277 &p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
278 &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
279 i_intensity_spread );
281 &p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
282 &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
283 i_intensity_spread );
285 &p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
286 &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
287 i_intensity_spread );
288 /* Copy precomputed values to destination memory location */
290 &p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)],
291 filling_const_8u, 8 );
293 &p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)],
294 filling_const_8v, 8 );
296 /* Completing the job, the cycle above takes really big chunks, so
297 this makes sure the job will be done completely */
298 for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 1; x += 2 )
300 // y = y - y/4 {to prevent overflow} + intensity / 4
301 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
302 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
303 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
305 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
306 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
307 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
309 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
310 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
311 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
313 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
314 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
315 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
317 // u = 128 {half => B&W} - intensity / 6
318 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
320 // v = 128 {half => B&W} + intensity / 14
321 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
329 /* iterate for every two visible line in the frame */
330 for( int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
332 const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
333 const int i_dy_line2_start = ( y + 1 ) * p_outpic->p[Y_PLANE].i_pitch;
334 const int i_du_line_start = (y/2) * p_outpic->p[U_PLANE].i_pitch;
335 const int i_dv_line_start = (y/2) * p_outpic->p[V_PLANE].i_pitch;
336 // to prevent sigsegv if one pic is smaller (theoretically)
337 int i_picture_size_limit = p_pic->p[Y_PLANE].i_visible_pitch
338 < p_outpic->p[Y_PLANE].i_visible_pitch
339 ? (p_pic->p[Y_PLANE].i_visible_pitch - 1) :
340 (p_outpic->p[Y_PLANE].i_visible_pitch - 1);
341 /* iterate for every two visible line in the frame */
342 for( int x = 0; x < i_picture_size_limit; x += 2)
344 // y = y - y/4 {to prevent overflow} + intensity / 4
345 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
346 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
347 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
349 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
350 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
351 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
353 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
354 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
355 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
357 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
358 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
359 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
361 // u = 128 {half => B&W} - intensity / 6
362 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
364 // v = 128 {half => B&W} + intensity / 14
365 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
372 /*****************************************************************************
373 * PackedYUVSepia: Applies sepia to one frame of the packed YUV video
374 *****************************************************************************
375 * This function applies sepia effext to one frame of the video by iterating
376 * through video lines. In every pass, we calculate new values for pixels
377 * (UYVY, VYUY, YUYV and YVYU formats are supported)
378 *****************************************************************************/
379 static void PackedYUVSepia( picture_t *p_pic, picture_t *p_outpic,
382 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
383 int i_yindex = 1, i_uindex = 2, i_vindex = 0;
385 GetPackedYuvOffsets( p_outpic->format.i_chroma,
386 &i_yindex, &i_uindex, &i_vindex );
388 // prepared values to copy for U and V channels
389 const uint8_t filling_const_8u = 128 - i_intensity / 6;
390 const uint8_t filling_const_8v = 128 + i_intensity / 14;
392 p_in = p_pic->p[0].p_pixels;
393 p_in_end = p_in + p_pic->p[0].i_visible_lines
394 * p_pic->p[0].i_pitch;
395 p_out = p_outpic->p[0].p_pixels;
398 while( p_in < p_in_end )
400 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
401 while( p_in < p_line_end )
403 /* calculate new, sepia values */
405 p_in[i_yindex] - (p_in[i_yindex] >> 2) + (i_intensity >> 2);
406 p_out[i_yindex + 2] =
407 p_in[i_yindex + 2] - (p_in[i_yindex + 2] >> 2)
408 + (i_intensity >> 2);
409 p_out[i_uindex] = filling_const_8u;
410 p_out[i_vindex] = filling_const_8v;
414 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
415 p_out += p_outpic->p[0].i_pitch
416 - p_outpic->p[0].i_visible_pitch;
421 /*****************************************************************************
422 * RVSepia: Applies sepia to one frame of the RV24/RV32 video
423 *****************************************************************************
424 * This function applies sepia effect to one frame of the video by iterating
425 * through video lines and calculating new values for every byte in chunks of
426 * 3 (RV24) or 4 (RV32) bytes.
427 *****************************************************************************/
428 static void RVSepia( picture_t *p_pic, picture_t *p_outpic, int i_intensity )
431 #define ONE_HALF (1 << (SCALEBITS - 1))
432 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
433 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
434 bool b_isRV32 = p_pic->format.i_chroma == VLC_CODEC_RGB32;
435 int i_rindex = 0, i_gindex = 1, i_bindex = 2;
437 GetPackedRgbIndexes( &p_outpic->format, &i_rindex, &i_gindex, &i_bindex );
439 p_in = p_pic->p[0].p_pixels;
440 p_in_end = p_in + p_pic->p[0].i_visible_lines
441 * p_pic->p[0].i_pitch;
442 p_out = p_outpic->p[0].p_pixels;
444 /* Precompute values constant for this certain i_intensity, using the same
445 * formula as YUV functions above */
446 uint8_t r_intensity = (( FIX( 1.40200 * 255.0 / 224.0 ) * (i_intensity * 14)
447 + ONE_HALF )) >> SCALEBITS;
448 uint8_t g_intensity = (( - FIX(0.34414*255.0/224.0) * ( - i_intensity / 6 )
449 - FIX( 0.71414 * 255.0 / 224.0) * ( i_intensity * 14 )
450 + ONE_HALF )) >> SCALEBITS;
451 uint8_t b_intensity = (( FIX( 1.77200 * 255.0 / 224.0) * ( - i_intensity / 6 )
452 + ONE_HALF )) >> SCALEBITS;
454 while (p_in < p_in_end)
456 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
457 while (p_in < p_line_end)
459 /* do sepia: this calculation is based on the formula to calculate
460 * YUV->RGB and RGB->YUV (in filter_picture.h) mode and that
461 * y = y - y/4 + intensity/4 . As Y is the only channel that changes
462 * through the whole image. After that, precomputed values are added
463 * for each RGB channel and saved in the output image.
464 * FIXME: needs cleanup */
465 uint8_t i_y = ((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
466 * p_in[i_bindex] + 128 ) >> 8 ) * FIX(255.0/219.0))
467 - (((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
468 * p_in[i_bindex] + 128 ) >> 8 )
469 * FIX( 255.0 / 219.0 )) >> 2 ) + ( i_intensity >> 2 );
470 p_out[i_rindex] = vlc_uint8(i_y + r_intensity);
471 p_out[i_gindex] = vlc_uint8(i_y + g_intensity);
472 p_out[i_bindex] = vlc_uint8(i_y + b_intensity);
475 /* for rv32 we take 4 chunks at the time */
477 /* alpha channel stays the same */
482 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
483 p_out += p_outpic->p[0].i_pitch
484 - p_outpic->p[0].i_visible_pitch;
491 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
492 vlc_value_t oldval, vlc_value_t newval,
495 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
496 filter_t *p_filter = (filter_t*)p_this;
497 filter_sys_t *p_sys = p_filter->p_sys;
499 vlc_spin_lock( &p_sys->lock );
500 p_sys->i_intensity = newval.i_int;
501 vlc_spin_unlock( &p_sys->lock );