1 /*****************************************************************************
2 * posterize.c : Posterize 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>
36 #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 picture_t *Filter( filter_t *, picture_t * );
46 static void PlanarYUVPosterize( picture_t *, picture_t *, int);
47 static void PackedYUVPosterize( picture_t *, picture_t *, int);
48 static void RVPosterize( picture_t *, picture_t *, bool, int );
49 static void YuvPosterization( uint8_t *, uint8_t *, uint8_t *, uint8_t *,
50 uint8_t, uint8_t, uint8_t, uint8_t, int );
52 static const char *const ppsz_filter_options[] = {
56 /*****************************************************************************
58 *****************************************************************************/
59 #define POSTERIZE_LEVEL_TEXT N_("Posterize level")
60 #define POSTERIZE_LEVEL_LONGTEXT N_("Posterize level "\
61 "(number of colors is cube of this value)" )
63 #define CFG_PREFIX "posterize-"
66 set_description( N_("Posterize video filter") )
67 set_shortname( N_("Posterize" ) )
68 set_help( N_("Posterize video by lowering the number of colors") )
69 set_category( CAT_VIDEO )
70 set_subcategory( SUBCAT_VIDEO_VFILTER )
71 set_capability( "video filter2", 0 )
72 add_integer_with_range( CFG_PREFIX "level", 6, 2, 256,
73 POSTERIZE_LEVEL_TEXT, POSTERIZE_LEVEL_LONGTEXT,
75 set_callbacks( Create, Destroy )
78 /*****************************************************************************
80 *****************************************************************************/
81 static int FilterCallback( vlc_object_t *, char const *,
82 vlc_value_t, vlc_value_t, void * );
84 /*****************************************************************************
85 * filter_sys_t: adjust filter method descriptor
86 *****************************************************************************/
93 /*****************************************************************************
94 * Create: allocates Posterize video thread output method
95 *****************************************************************************
96 * This function allocates and initializes a Posterize vout method.
97 *****************************************************************************/
98 static int Create( vlc_object_t *p_this )
100 filter_t *p_filter = (filter_t *)p_this;
103 switch( p_filter->fmt_in.video.i_chroma )
105 CASE_PLANAR_YUV_SQUARE
109 case VLC_CODEC_RGB24:
111 case VLC_CODEC_RGB32:
114 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
115 (char*)&(p_filter->fmt_in.video.i_chroma) );
119 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
121 msg_Err( p_filter, "Input and output chromas don't match" );
125 /* Allocate structure */
126 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
127 if( p_filter->p_sys == NULL )
130 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
132 p_sys->i_level = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "level" );
134 vlc_mutex_init( &p_sys->lock );
136 var_AddCallback( p_filter, CFG_PREFIX "level", FilterCallback, NULL );
138 p_filter->pf_video_filter = Filter;
143 /*****************************************************************************
144 * Destroy: destroy Posterize video thread output method
145 *****************************************************************************
146 * Terminate an output method created by PosterizeCreateOutputMethod
147 *****************************************************************************/
148 static void Destroy( vlc_object_t *p_this )
150 filter_t *p_filter = (filter_t *)p_this;
152 var_DelCallback( p_filter, CFG_PREFIX "level", FilterCallback, NULL );
154 vlc_mutex_destroy( &p_filter->p_sys->lock );
155 free( p_filter->p_sys );
158 /*****************************************************************************
159 * Render: displays previously rendered output
160 *****************************************************************************
161 * This function send the currently rendered image to Posterize image, waits
162 * until it is displayed and switch the two rendering buffers, preparing next
164 *****************************************************************************/
165 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
170 if( !p_pic ) return NULL;
172 filter_sys_t *p_sys = p_filter->p_sys;
173 vlc_mutex_lock( &p_sys->lock );
174 level = p_sys->i_level;
175 vlc_mutex_unlock( &p_sys->lock );
177 p_outpic = filter_NewPicture( p_filter );
180 msg_Warn( p_filter, "can't get output picture" );
181 picture_Release( p_pic );
185 switch( p_pic->format.i_chroma )
187 case VLC_CODEC_RGB24:
188 RVPosterize( p_pic, p_outpic, false, level );
190 case VLC_CODEC_RGB32:
191 RVPosterize( p_pic, p_outpic, true, level );
193 CASE_PLANAR_YUV_SQUARE
194 PlanarYUVPosterize( p_pic, p_outpic, level );
197 PackedYUVPosterize( p_pic, p_outpic, level );
203 return CopyInfoAndRelease( p_outpic, p_pic );
206 /*****************************************************************************
207 * For a given level, calculates new posterized value for pixel whose value x
208 * is in range of 0-255
209 *****************************************************************************/
210 #define POSTERIZE_PIXEL(x, level) \
211 (((( x * level ) >> 8 ) * 255 ) / ( level - 1 ))
213 /*****************************************************************************
214 * PlanarYUVPosterize: Posterize one frame of the planar YUV video
215 *****************************************************************************
216 * This function posterizes one frame of the video by iterating through video
217 * lines. In every pass, start of Y, U and V planes is calculated and for
218 * every pixel we calculate new values of YUV values.
219 *****************************************************************************/
220 static void PlanarYUVPosterize( picture_t *p_pic, picture_t *p_outpic,
223 uint8_t *p_in_y, *p_in_u, *p_in_v, *p_in_end_y, *p_line_end_y, *p_out_y,
225 int i_current_line = 0;
227 p_in_y = p_pic->p[Y_PLANE].p_pixels;
228 p_in_end_y = p_in_y + p_pic->p[Y_PLANE].i_visible_lines
229 * p_pic->p[Y_PLANE].i_pitch;
230 p_out_y = p_outpic->p[Y_PLANE].p_pixels;
232 /* iterate for every visible line in the frame */
233 while( p_in_y < p_in_end_y )
235 p_line_end_y = p_in_y + p_pic->p[Y_PLANE].i_visible_pitch;
236 /* calculate start of U plane line */
237 p_in_u = p_pic->p[U_PLANE].p_pixels
238 + p_pic->p[U_PLANE].i_pitch * ( i_current_line / 2 );
239 p_out_u = p_outpic->p[U_PLANE].p_pixels
240 + p_outpic->p[U_PLANE].i_pitch * ( i_current_line / 2 );
241 /* calculate start of V plane line */
242 p_in_v = p_pic->p[V_PLANE].p_pixels
243 + p_pic->p[V_PLANE].i_pitch * ( i_current_line / 2 );
244 p_out_v = p_outpic->p[V_PLANE].p_pixels
245 + p_outpic->p[V_PLANE].i_pitch * ( i_current_line / 2 );
246 /* iterate for every two pixels in line */
247 while( p_in_y < p_line_end_y )
249 uint8_t y1, y2, u, v;
250 uint8_t posterized_y1, posterized_y2, posterized_u, posterized_v;
251 /* retrieve original YUV values */
256 /* do posterization */
257 YuvPosterization( &posterized_y1, &posterized_y2, &posterized_u,
258 &posterized_v, y1, y2, u, v, i_level );
259 /* put posterized valued */
260 *p_out_y++ = posterized_y1;
261 *p_out_y++ = posterized_y2;
262 *p_out_u++ = posterized_u;
263 *p_out_v++ = posterized_v;
265 p_in_y += p_pic->p[Y_PLANE].i_pitch
266 - p_pic->p[Y_PLANE].i_visible_pitch;
267 p_out_y += p_outpic->p[Y_PLANE].i_pitch
268 - p_outpic->p[Y_PLANE].i_visible_pitch;
273 /*****************************************************************************
274 * PackedYUVPosterize: Posterize one frame of the packed YUV video
275 *****************************************************************************
276 * This function posterizes one frame of the video by iterating through video
277 * lines. In every pass, we calculate new values for pixels (UYVY, VYUY, YUYV
278 * and YVYU formats are supported)
279 *****************************************************************************/
280 static void PackedYUVPosterize( picture_t *p_pic, picture_t *p_outpic, int i_level )
282 uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
283 uint8_t y1, y2, u, v;
285 p_in = p_pic->p[0].p_pixels;
286 p_in_end = p_in + p_pic->p[0].i_visible_lines
287 * p_pic->p[0].i_pitch;
288 p_out = p_outpic->p[0].p_pixels;
290 while( p_in < p_in_end )
292 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
293 while( p_in < p_line_end )
295 uint8_t posterized_y1, posterized_y2, posterized_u, posterized_v;
296 /* extract proper pixel values */
297 switch( p_pic->format.i_chroma )
326 /* do posterization */
327 YuvPosterization( &posterized_y1, &posterized_y2, &posterized_u,
328 &posterized_v, y1, y2, u, v, i_level );
329 /* put posterized values in proper place */
330 switch( p_pic->format.i_chroma )
333 *p_out++ = posterized_u;
334 *p_out++ = posterized_y1;
335 *p_out++ = posterized_v;
336 *p_out++ = posterized_y2;
339 *p_out++ = posterized_v;
340 *p_out++ = posterized_y1;
341 *p_out++ = posterized_u;
342 *p_out++ = posterized_y2;
345 *p_out++ = posterized_y1;
346 *p_out++ = posterized_u;
347 *p_out++ = posterized_y2;
348 *p_out++ = posterized_v;
351 *p_out++ = posterized_y1;
352 *p_out++ = posterized_v;
353 *p_out++ = posterized_y2;
354 *p_out++ = posterized_u;
360 p_in += p_pic->p[0].i_pitch - p_pic->p[0].i_visible_pitch;
361 p_out += p_outpic->p[0].i_pitch
362 - p_outpic->p[0].i_visible_pitch;
366 /*****************************************************************************
367 * RVPosterize: Posterize one frame of the RV24/RV32 video
368 *****************************************************************************
369 * This function posterizes one frame of the video by iterating through video
370 * lines and calculating new values for every byte in chunks of 3 (RV24) or
372 *****************************************************************************/
373 static void RVPosterize( picture_t *p_pic, picture_t *p_outpic,
374 bool rv32, int level )
376 uint8_t *p_in, *p_in_end, *p_line_end, *p_out, pixel;
378 p_in = p_pic->p[0].p_pixels;
379 p_in_end = p_in + p_pic->p[0].i_visible_lines
380 * p_pic->p[0].i_pitch;
381 p_out = p_outpic->p[0].p_pixels;
383 while( p_in < p_in_end )
385 p_line_end = p_in + p_pic->p[0].i_visible_pitch;
386 while( p_in < p_line_end )
389 *p_out++ = POSTERIZE_PIXEL( pixel, level );
391 *p_out++ = POSTERIZE_PIXEL( pixel, level );
393 *p_out++ = POSTERIZE_PIXEL( pixel, level );
394 /* for rv32 we take 4 chunks at the time */
398 *p_out++ = POSTERIZE_PIXEL( pixel, level );
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;
407 /*****************************************************************************
408 * YuvPosterization: Lowers the color depth of YUV color space
409 *****************************************************************************
410 * This function lowers the color level of YUV color space to specified level
411 * by converting YUV color values to theirs RGB equivalents, calculates new
412 * values and then converts RGB values to YUV values again.
413 *****************************************************************************/
414 static void YuvPosterization( uint8_t* posterized_y1, uint8_t* posterized_y2,
415 uint8_t* posterized_u, uint8_t* posterized_v,
416 uint8_t y1, uint8_t y2, uint8_t u, uint8_t v,
418 int r1, g1, b1; /* for y1 new value */
419 int r2, b2, g2; /* for y2 new value */
420 int r3, g3, b3; /* for new values of u and v */
421 /* fist convert YUV -> RGB */
422 yuv_to_rgb( &r1, &g1, &b1, y1, u, v );
423 yuv_to_rgb( &r2, &g2, &b2, y1, u, v );
424 yuv_to_rgb( &r3, &g3, &b3, ( y1 + y2 ) / 2, u, v );
425 /* round RGB values to specified posterize level */
426 r1 = POSTERIZE_PIXEL( r1, i_level );
427 g1 = POSTERIZE_PIXEL( g1, i_level );
428 b1 = POSTERIZE_PIXEL( b1, i_level );
429 r2 = POSTERIZE_PIXEL( r2, i_level );
430 g2 = POSTERIZE_PIXEL( g2, i_level );
431 b2 = POSTERIZE_PIXEL( b2, i_level );
432 r3 = POSTERIZE_PIXEL( r3, i_level );
433 g3 = POSTERIZE_PIXEL( g3, i_level );
434 b3 = POSTERIZE_PIXEL( b3, i_level );
435 /* convert from calculated RGB -> YUV */
436 *posterized_y1 = ( ( 66 * r1 + 129 * g1 + 25 * b1 + 128 ) >> 8 ) + 16;
437 *posterized_y2 = ( ( 66 * r2 + 129 * g2 + 25 * b2 + 128 ) >> 8 ) + 16;
438 *posterized_u = ( ( -38 * r3 - 74 * g3 + 112 * b3 + 128 ) >> 8 ) + 128;
439 *posterized_v = ( ( 112 * r3 - 94 * g3 - 18 * b3 + 128 ) >> 8 ) + 128;
442 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
443 vlc_value_t oldval, vlc_value_t newval, void *p_data )
445 (void)oldval; (void)p_data;
446 filter_t *p_filter = (filter_t*)p_this;
447 filter_sys_t *p_sys = p_filter->p_sys;
449 if( !strcmp( psz_var, CFG_PREFIX "level" ) )
451 vlc_mutex_lock( &p_sys->lock );
452 p_sys->i_level = newval.i_int;
453 vlc_mutex_unlock( &p_sys->lock );