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>
37 #include "filter_picture.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
45 static void RVSepia( picture_t *, picture_t *, int );
46 static void PlanarI420Sepia( picture_t *, picture_t *, int);
47 static void PackedYUVSepia( picture_t *, picture_t *, int);
48 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", 100, 0, 255, NULL,
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 /*****************************************************************************
202 * PlanarI420Sepia: Applies sepia to one frame of the planar I420 video
203 *****************************************************************************
204 * This function applies sepia effect to one frame of the video by iterating
205 * through video lines. We iterate for every two lines and for every two pixels
206 * in line to calculate new sepia values for four y components as well for u
208 *****************************************************************************/
209 static void PlanarI420Sepia( picture_t *p_pic, picture_t *p_outpic,
212 // prepared values to copy for U and V channels
213 const uint8_t filling_const_8u = 128 - i_intensity / 6;
214 const uint8_t filling_const_8v = 128 + i_intensity / 14;
215 /* iterate for every two visible line in the frame */
216 for( int y = 0; y < p_pic->p[Y_PLANE].i_visible_lines - 1; y += 2)
218 const int i_dy_line1_start = y * p_outpic->p[Y_PLANE].i_pitch;
219 const int i_dy_line2_start = ( y + 1 ) * p_outpic->p[Y_PLANE].i_pitch;
220 const int i_du_line_start = (y/2) * p_outpic->p[U_PLANE].i_pitch;
221 const int i_dv_line_start = (y/2) * p_outpic->p[V_PLANE].i_pitch;
222 // to prevent sigsegv if one pic is smaller (theoretically)
223 int i_picture_size_limit = p_pic->p[Y_PLANE].i_visible_pitch
224 < p_outpic->p[Y_PLANE].i_visible_pitch
225 ? (p_pic->p[Y_PLANE].i_visible_pitch - 1) :
226 (p_outpic->p[Y_PLANE].i_visible_pitch - 1);
227 /* iterate for every two visible line in the frame */
228 for( int x = 0; x < i_picture_size_limit; x += 2)
230 // y = y - y/4 {to prevent overflow} + intensity / 4
231 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] =
232 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] -
233 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x] >> 2) +
235 p_outpic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] =
236 p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] -
237 (p_pic->p[Y_PLANE].p_pixels[i_dy_line1_start + x + 1] >> 2) +
239 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] =
240 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] -
241 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x] >> 2) +
243 p_outpic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] =
244 p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] -
245 (p_pic->p[Y_PLANE].p_pixels[i_dy_line2_start + x + 1] >> 2) +
247 // u = 128 {half => B&W} - intensity / 6
248 p_outpic->p[U_PLANE].p_pixels[i_du_line_start + (x / 2)] =
250 // v = 128 {half => B&W} + intensity / 14
251 p_outpic->p[V_PLANE].p_pixels[i_dv_line_start + (x / 2)] =
257 /*****************************************************************************
258 * PackedYUVSepia: Applies sepia to one frame of the packed YUV video
259 *****************************************************************************
260 * This function applies sepia effext to one frame of the video by iterating
261 * through video lines. In every pass, we calculate new values for pixels
262 * (UYVY, VYUY, YUYV and YVYU formats are supported)
263 *****************************************************************************/
264 static void PackedYUVSepia( picture_t *p_pic, picture_t *p_outpic,
267 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
268 int i_yindex = 1, i_uindex = 2, i_vindex = 0;
270 GetPackedYuvOffsets( p_outpic->format.i_chroma,
271 &i_yindex, &i_uindex, &i_vindex );
273 // prepared values to copy for U and V channels
274 const uint8_t filling_const_8u = 128 - i_intensity / 6;
275 const uint8_t filling_const_8v = 128 + i_intensity / 14;
277 p_in = p_pic->p[0].p_pixels;
278 p_in_end = p_in + p_pic->p[0].i_visible_lines
279 * p_pic->p[0].i_pitch;
280 p_out = p_outpic->p[0].p_pixels;
282 while( p_in < p_in_end )
284 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
285 while( p_in < p_line_end )
287 /* calculate new, sepia values */
289 p_in[i_yindex] - (p_in[i_yindex] >> 2) + (i_intensity >> 2);
290 p_out[i_yindex + 2] =
291 p_in[i_yindex + 2] - (p_in[i_yindex + 2] >> 2)
292 + (i_intensity >> 2);
293 p_out[i_uindex] = filling_const_8u;
294 p_out[i_vindex] = filling_const_8v;
298 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
299 p_out += p_outpic->p[0].i_pitch
300 - p_outpic->p[0].i_visible_pitch;
304 /*****************************************************************************
305 * RVSepia: Applies sepia to one frame of the RV24/RV32 video
306 *****************************************************************************
307 * This function applies sepia effect to one frame of the video by iterating
308 * through video lines and calculating new values for every byte in chunks of
309 * 3 (RV24) or 4 (RV32) bytes.
310 *****************************************************************************/
311 static void RVSepia( picture_t *p_pic, picture_t *p_outpic, int i_intensity )
314 #define ONE_HALF (1 << (SCALEBITS - 1))
315 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
316 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
318 bool b_isRV32 = p_pic->format.i_chroma == VLC_CODEC_RGB32;
319 int i_rindex = 0, i_gindex = 1, i_bindex = 2;
321 GetPackedRgbIndexes( &p_outpic->format, &i_rindex, &i_gindex, &i_bindex );
323 p_in = p_pic->p[0].p_pixels;
324 p_in_end = p_in + p_pic->p[0].i_visible_lines
325 * p_pic->p[0].i_pitch;
326 p_out = p_outpic->p[0].p_pixels;
328 /* Precompute values constant for this certain i_intensity, using the same
329 * formula as YUV functions above */
330 uint8_t r_intensity = (( FIX( 1.40200 * 255.0 / 224.0 ) * (i_intensity * 14)
331 + ONE_HALF )) >> SCALEBITS;
332 uint8_t g_intensity = (( - FIX(0.34414*255.0/224.0) * ( - i_intensity / 6 )
333 - FIX( 0.71414 * 255.0 / 224.0) * ( i_intensity * 14 )
334 + ONE_HALF )) >> SCALEBITS;
335 uint8_t b_intensity = (( FIX( 1.77200 * 255.0 / 224.0) * ( - i_intensity / 6 )
336 + ONE_HALF )) >> SCALEBITS;
338 while (p_in < p_in_end)
340 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
341 while (p_in < p_line_end)
343 /* do sepia: this calculation is based on the formula to calculate
344 * YUV->RGB and RGB->YUV (in filter_picture.h) mode and that
345 * y = y - y/4 + intensity/4 . As Y is the only channel that changes
346 * through the whole image. After that, precomputed values are added
347 * for each RGB channel and saved in the output image.
348 * FIXME: needs cleanup */
349 uint8_t i_y = ((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
350 * p_in[i_bindex] + 128 ) >> 8 ) * FIX(255.0/219.0))
351 - (((( 66 * p_in[i_rindex] + 129 * p_in[i_gindex] + 25
352 * p_in[i_bindex] + 128 ) >> 8 )
353 * FIX( 255.0 / 219.0 )) >> 2 ) + ( i_intensity >> 2 );
354 p_out[i_rindex] = vlc_uint8(i_y + r_intensity);
355 p_out[i_gindex] = vlc_uint8(i_y + g_intensity);
356 p_out[i_bindex] = vlc_uint8(i_y + b_intensity);
359 /* for rv32 we take 4 chunks at the time */
361 /* alpha channel stays the same */
366 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
367 p_out += p_outpic->p[0].i_pitch
368 - p_outpic->p[0].i_visible_pitch;
375 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
376 vlc_value_t oldval, vlc_value_t newval,
379 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
380 filter_t *p_filter = (filter_t*)p_this;
381 filter_sys_t *p_sys = p_filter->p_sys;
383 vlc_spin_lock( &p_sys->lock );
384 p_sys->i_intensity = newval.i_int;
385 vlc_spin_unlock( &p_sys->lock );