lib: media_player: fix libvlc_MediaPlayerMediaChanged event
[vlc.git] / modules / video_filter / sepia.c
blob9ca567c614e00b4cf35a88fff5191285de3cac0d
1 /*****************************************************************************
2 * sepia.c : Sepia video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2010 VLC authors and VideoLAN
6 * Authors: Branko Kokanovic <branko.kokanovic@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34 #include <vlc_picture.h>
35 #include <vlc_cpu.h>
37 #include <stdatomic.h>
38 #include <assert.h>
39 #include "filter_picture.h"
41 /*****************************************************************************
42 * Local prototypes
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static void RVSepia( picture_t *, picture_t *, int );
48 static void PlanarI420Sepia( picture_t *, picture_t *, int);
49 static void PackedYUVSepia( picture_t *, picture_t *, int);
50 static picture_t *Filter( filter_t *, picture_t * );
51 static const char *const ppsz_filter_options[] = {
52 "intensity", NULL
55 /*****************************************************************************
56 * Module descriptor
57 *****************************************************************************/
58 #define SEPIA_INTENSITY_TEXT N_("Sepia intensity")
59 #define SEPIA_INTENSITY_LONGTEXT N_("Intensity of sepia effect" )
61 #define CFG_PREFIX "sepia-"
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 filter", 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 ()
76 /*****************************************************************************
77 * callback prototypes
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 );
84 static const struct
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 }
99 /*****************************************************************************
100 * filter_sys_t: adjust filter method descriptor
101 *****************************************************************************/
102 typedef struct
104 SepiaFunction pf_sepia;
105 atomic_int i_intensity;
106 } filter_sys_t;
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;
116 filter_sys_t *p_sys;
118 /* Allocate structure */
119 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
120 if( p_filter->p_sys == NULL )
121 return VLC_ENOMEM;
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 )
128 continue;
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) );
136 free( p_sys );
137 return VLC_EGENERIC;
140 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
141 p_filter->p_cfg );
142 atomic_init( &p_sys->i_intensity,
143 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "intensity" ) );
144 var_AddCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
146 p_filter->pf_video_filter = Filter;
148 return VLC_SUCCESS;
151 /*****************************************************************************
152 * Destroy: destroy sepia video thread output method
153 *****************************************************************************
154 * Terminate an output method
155 *****************************************************************************/
156 static void Destroy( vlc_object_t *p_this )
158 filter_t *p_filter = (filter_t *)p_this;
160 var_DelCallback( p_filter, CFG_PREFIX "intensity", FilterCallback, NULL );
162 free( p_filter->p_sys );
165 /*****************************************************************************
166 * Render: displays previously rendered output
167 *****************************************************************************
168 * This function send the currently rendered image to sepia image, waits
169 * until it is displayed and switch the two rendering buffers, preparing next
170 * frame.
171 *****************************************************************************/
172 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
174 picture_t *p_outpic;
176 if( !p_pic ) return NULL;
178 filter_sys_t *p_sys = p_filter->p_sys;
179 int intensity = atomic_load( &p_sys->i_intensity );
181 p_outpic = filter_NewPicture( p_filter );
182 if( !p_outpic )
184 msg_Warn( p_filter, "can't get output picture" );
185 picture_Release( p_pic );
186 return NULL;
189 p_sys->pf_sepia( p_pic, p_outpic, intensity );
191 return CopyInfoAndRelease( p_outpic, p_pic );
194 #if defined(CAN_COMPILE_SSE2)
195 /*****************************************************************************
196 * Sepia8ySSE2
197 *****************************************************************************
198 * This function applies sepia effect to eight bytes of yellow using SSE4.1
199 * instructions. It copies those 8 bytes to 128b register and fills the gaps
200 * with zeroes and following operations are made with word-operating instructs.
201 *****************************************************************************/
202 VLC_SSE
203 static inline void Sepia8ySSE2(uint8_t * dst, const uint8_t * src,
204 int i_intensity_shifted_pair)
206 __asm__ volatile (
207 // y = y - y / 4 + i_intensity / 4
208 "movq (%1), %%xmm1\n"
209 "punpcklbw %%xmm7, %%xmm1\n" // zero-extend bytes to words
210 "movdqa %%xmm1, %%xmm2\n" // copy it
211 "movd %2, %%xmm3\n"
212 "pshufd $0, %%xmm3, %%xmm3\n"
213 "psrlw $2, %%xmm2\n" // get 1/4 of it
214 "psubusb %%xmm2, %%xmm1\n"
215 "paddusb %%xmm3, %%xmm1\n"
216 "packuswb %%xmm1, %%xmm1\n" // pack back to bytes
217 "movq %%xmm1, (%0) \n"
219 :"r" (dst), "r"(src), "r"(i_intensity_shifted_pair)
220 :"memory", "xmm1", "xmm2", "xmm3");
223 VLC_SSE
224 static void PlanarI420SepiaSSE( picture_t *p_pic, picture_t *p_outpic,
225 int i_intensity )
227 /* prepared values to copy for U and V channels */
228 const uint8_t filling_const_8u = 128 - i_intensity / 6;
229 const uint8_t filling_const_8v = 128 + i_intensity / 14;
230 /* prepared value for faster broadcasting in xmm register */
231 int i_intensity_shifted_pair = 0x10001 * (((uint8_t) i_intensity) >> 2);
233 __asm__ volatile("pxor %%xmm7, %%xmm7\n" ::: "xmm7");
235 /* iterate for every two visible line in the frame */
236 for (int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
238 const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
239 const int i_dy_line2_start = (y + 1) * p_outpic->p[Y_PLANE].i_pitch;
240 const int i_du_line_start = (y / 2) * p_outpic->p[U_PLANE].i_pitch;
241 const int i_dv_line_start = (y / 2) * p_outpic->p[V_PLANE].i_pitch;
242 int x = 0;
243 /* iterate for every visible line in the frame (eight values at once) */
244 for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 15; x += 16 )
246 /* Compute yellow channel values with asm function */
247 Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
248 &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x],
249 i_intensity_shifted_pair );
250 Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
251 &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x],
252 i_intensity_shifted_pair );
253 Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
254 &p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 8],
255 i_intensity_shifted_pair );
256 Sepia8ySSE2(&p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
257 &p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 8],
258 i_intensity_shifted_pair );
259 /* Copy precomputed values to destination memory location */
260 memset(&p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)],
261 filling_const_8u, 8 );
262 memset(&p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)],
263 filling_const_8v, 8 );
265 /* Completing the job, the cycle above takes really big chunks, so
266 this makes sure the job will be done completely */
267 for ( ; x < p_pic->p[Y_PLANE].i_visible_pitch - 1; x += 2 )
269 // y = y - y/4 {to prevent overflow} + intensity / 4
270 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
271 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
272 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
273 (i_intensity >> 2);
274 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
275 p_pic->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] >> 2) +
277 (i_intensity >> 2);
278 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
279 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
280 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
281 (i_intensity >> 2);
282 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
283 p_pic->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] >> 2) +
285 (i_intensity >> 2);
286 // u = 128 {half => B&W} - intensity / 6
287 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
288 filling_const_8u;
289 // v = 128 {half => B&W} + intensity / 14
290 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
291 filling_const_8v;
295 #endif
297 /*****************************************************************************
298 * PlanarI420Sepia: Applies sepia to one frame of the planar I420 video
299 *****************************************************************************
300 * This function applies sepia effect to one frame of the video by iterating
301 * through video lines. We iterate for every two lines and for every two pixels
302 * in line to calculate new sepia values for four y components as well for u
303 * and v components.
304 *****************************************************************************/
305 static void PlanarI420Sepia( picture_t *p_pic, picture_t *p_outpic,
306 int i_intensity )
308 #if defined(CAN_COMPILE_SSE2)
309 if (vlc_CPU_SSE2())
310 return PlanarI420SepiaSSE( p_pic, p_outpic, i_intensity );
311 #endif
313 // prepared values to copy for U and V channels
314 const uint8_t filling_const_8u = 128 - i_intensity / 6;
315 const uint8_t filling_const_8v = 128 + i_intensity / 14;
317 /* iterate for every two visible line in the frame */
318 for( int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2 )
320 const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
321 const int i_dy_line2_start = ( y + 1 ) * p_outpic->p[Y_PLANE].i_pitch;
322 const int i_du_line_start = (y/2) * p_outpic->p[U_PLANE].i_pitch;
323 const int i_dv_line_start = (y/2) * p_outpic->p[V_PLANE].i_pitch;
324 // to prevent sigsegv if one pic is smaller (theoretically)
325 int i_picture_size_limit = p_pic->p[Y_PLANE].i_visible_pitch
326 < p_outpic->p[Y_PLANE].i_visible_pitch
327 ? (p_pic->p[Y_PLANE].i_visible_pitch - 1) :
328 (p_outpic->p[Y_PLANE].i_visible_pitch - 1);
329 /* iterate for every two visible line in the frame */
330 for( int x = 0; x < i_picture_size_limit; x += 2 )
332 // y = y - y/4 {to prevent overflow} + intensity / 4
333 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
334 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
335 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
336 (i_intensity >> 2);
337 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
338 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
339 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
340 (i_intensity >> 2);
341 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
342 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
343 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
344 (i_intensity >> 2);
345 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
346 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
347 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
348 (i_intensity >> 2);
349 // u = 128 {half => B&W} - intensity / 6
350 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
351 filling_const_8u;
352 // v = 128 {half => B&W} + intensity / 14
353 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
354 filling_const_8v;
359 /*****************************************************************************
360 * PackedYUVSepia: Applies sepia to one frame of the packed YUV video
361 *****************************************************************************
362 * This function applies sepia effect to one frame of the video by iterating
363 * through video lines. In every pass, we calculate new values for pixels
364 * (UYVY, VYUY, YUYV and YVYU formats are supported)
365 *****************************************************************************/
366 static void PackedYUVSepia( picture_t *p_pic, picture_t *p_outpic,
367 int i_intensity )
369 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
370 int i_yindex = 1, i_uindex = 2, i_vindex = 0;
372 GetPackedYuvOffsets( p_outpic->format.i_chroma,
373 &i_yindex, &i_uindex, &i_vindex );
375 // prepared values to copy for U and V channels
376 const uint8_t filling_const_8u = 128 - i_intensity / 6;
377 const uint8_t filling_const_8v = 128 + i_intensity / 14;
379 p_in = p_pic->p[0].p_pixels;
380 p_in_end = p_in + p_pic->p[0].i_visible_lines
381 * p_pic->p[0].i_pitch;
382 p_out = p_outpic->p[0].p_pixels;
385 while( p_in < p_in_end )
387 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
388 while( p_in < p_line_end )
390 /* calculate new, sepia values */
391 p_out[i_yindex] =
392 p_in[i_yindex] - (p_in[i_yindex] >> 2) + (i_intensity >> 2);
393 p_out[i_yindex + 2] =
394 p_in[i_yindex + 2] - (p_in[i_yindex + 2] >> 2)
395 + (i_intensity >> 2);
396 p_out[i_uindex] = filling_const_8u;
397 p_out[i_vindex] = filling_const_8v;
398 p_in += 4;
399 p_out += 4;
401 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
402 p_out += p_outpic->p[0].i_pitch
403 - p_outpic->p[0].i_visible_pitch;
408 /*****************************************************************************
409 * RVSepia: Applies sepia to one frame of the RV24/RV32 video
410 *****************************************************************************
411 * This function applies sepia effect to one frame of the video by iterating
412 * through video lines and calculating new values for every byte in chunks of
413 * 3 (RV24) or 4 (RV32) bytes.
414 *****************************************************************************/
415 static void RVSepia( picture_t *p_pic, picture_t *p_outpic, int i_intensity )
417 #define SCALEBITS 10
418 #define ONE_HALF (1 << (SCALEBITS - 1))
419 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
420 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
421 bool b_isRV32 = p_pic->format.i_chroma == VLC_CODEC_RGB32;
422 int i_rindex = 0, i_gindex = 1, i_bindex = 2;
424 GetPackedRgbIndexes( &p_outpic->format, &i_rindex, &i_gindex, &i_bindex );
426 p_in = p_pic->p[0].p_pixels;
427 p_in_end = p_in + p_pic->p[0].i_visible_lines
428 * p_pic->p[0].i_pitch;
429 p_out = p_outpic->p[0].p_pixels;
431 /* Precompute values constant for this certain i_intensity, using the same
432 * formula as YUV functions above */
433 uint8_t r_intensity = (( FIX( 1.40200 * 255.0 / 224.0 ) * (i_intensity * 14)
434 + ONE_HALF )) >> SCALEBITS;
435 uint8_t g_intensity = (( - FIX(0.34414*255.0/224.0) * ( - i_intensity / 6 )
436 - FIX( 0.71414 * 255.0 / 224.0) * ( i_intensity * 14 )
437 + ONE_HALF )) >> SCALEBITS;
438 uint8_t b_intensity = (( FIX( 1.77200 * 255.0 / 224.0) * ( - i_intensity / 6 )
439 + ONE_HALF )) >> SCALEBITS;
441 while (p_in < p_in_end)
443 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
444 while (p_in < p_line_end)
446 /* do sepia: this calculation is based on the formula to calculate
447 * YUV->RGB and RGB->YUV (in filter_picture.h) mode and that
448 * y = y - y/4 + intensity/4 . As Y is the only channel that changes
449 * through the whole image. After that, precomputed values are added
450 * for each RGB channel and saved in the output image.
451 * FIXME: needs cleanup */
452 uint8_t i_y = ((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
453 * p_in[i_bindex] + 128 ) >> 8 ) * FIX(255.0/219.0))
454 - (((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
455 * p_in[i_bindex] + 128 ) >> 8 )
456 * FIX( 255.0 / 219.0 )) >> 2 ) + ( i_intensity >> 2 );
457 p_out[i_rindex] = vlc_uint8(i_y + r_intensity);
458 p_out[i_gindex] = vlc_uint8(i_y + g_intensity);
459 p_out[i_bindex] = vlc_uint8(i_y + b_intensity);
460 p_in += 3;
461 p_out += 3;
462 /* for rv32 we take 4 chunks at the time */
463 if (b_isRV32) {
464 /* alpha channel stays the same */
465 *p_out++ = *p_in++;
469 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
470 p_out += p_outpic->p[0].i_pitch
471 - p_outpic->p[0].i_visible_pitch;
473 #undef SCALEBITS
474 #undef ONE_HALF
475 #undef FIX
478 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
479 vlc_value_t oldval, vlc_value_t newval,
480 void *p_data )
482 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
483 filter_t *p_filter = (filter_t*)p_this;
484 filter_sys_t *p_sys = p_filter->p_sys;
486 atomic_store( &p_sys->i_intensity, newval.i_int );
487 return VLC_SUCCESS;