1 /*****************************************************************************
2 * extract.c : Extract RGB components
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea .t videolan d@t org>
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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
33 #include "vlc_filter.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Create ( vlc_object_t * );
39 static void Destroy ( vlc_object_t * );
41 static picture_t *Filter( filter_t *, picture_t * );
43 #define COMPONENT_TEXT N_("RGB component to extract")
44 #define COMPONENT_LONGTEXT N_("RGB component to extract. 0 for Red, 1 for Green and 2 for Blue.")
45 #define FILTER_PREFIX "extract-"
47 static int pi_component_values[] = { 0, 1, 2 };
48 static const char *ppsz_component_descriptions[] = { "Red", "Green", "Blue" };
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( _("Extract RGB component video filter") );
55 set_shortname( _("Extract" ));
56 set_category( CAT_VIDEO );
57 set_subcategory( SUBCAT_VIDEO_VFILTER );
58 set_capability( "video filter2", 0 );
59 add_shortcut( "extract" );
61 add_integer( FILTER_PREFIX "component", 0, NULL,
62 COMPONENT_TEXT, COMPONENT_LONGTEXT, VLC_FALSE );
63 change_integer_list( pi_component_values, ppsz_component_descriptions, 0 );
65 set_callbacks( Create, Destroy );
68 static const char *ppsz_filter_options[] = {
72 enum { RED, GREEN, BLUE };
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Create( vlc_object_t *p_this )
83 filter_t *p_filter = (filter_t *)p_this;
85 /* Allocate structure */
86 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
87 if( p_filter->p_sys == NULL )
89 msg_Err( p_filter, "out of memory" );
93 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
96 var_Create( p_filter, FILTER_PREFIX "component",
97 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
98 p_filter->p_sys->i_color = var_GetInteger( p_filter,
99 FILTER_PREFIX "component" );
101 p_filter->pf_video_filter = Filter;
106 /*****************************************************************************
108 *****************************************************************************/
109 static void Destroy( vlc_object_t *p_this )
111 filter_t *p_filter = (filter_t *)p_this;
113 free( p_filter->p_sys );
116 /*****************************************************************************
118 *****************************************************************************/
119 static void get_red_from_yuv420( picture_t *, picture_t *, int, int, int );
120 static void get_green_from_yuv420( picture_t *, picture_t *, int, int, int );
121 static void get_blue_from_yuv420( picture_t *, picture_t *, int, int, int );
122 static void get_red_from_yuv422( picture_t *, picture_t *, int, int, int );
123 static void get_green_from_yuv422( picture_t *, picture_t *, int, int, int );
124 static void get_blue_from_yuv422( picture_t *, picture_t *, int, int, int );
126 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
130 if( !p_pic ) return NULL;
132 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
135 msg_Warn( p_filter, "can't get output picture" );
136 if( p_pic->pf_release )
137 p_pic->pf_release( p_pic );
141 switch( p_pic->format.i_chroma )
143 case VLC_FOURCC('I','4','2','0'):
144 case VLC_FOURCC('I','Y','U','V'):
145 case VLC_FOURCC('J','4','2','0'):
146 case VLC_FOURCC('Y','V','1','2'):
147 switch( p_filter->p_sys->i_color )
150 get_red_from_yuv420( p_pic, p_outpic,
151 Y_PLANE, U_PLANE, V_PLANE );
154 get_green_from_yuv420( p_pic, p_outpic,
155 Y_PLANE, U_PLANE, V_PLANE );
158 get_blue_from_yuv420( p_pic, p_outpic,
159 Y_PLANE, U_PLANE, V_PLANE );
164 case VLC_FOURCC('I','4','2','2'):
165 case VLC_FOURCC('J','4','2','2'):
166 switch( p_filter->p_sys->i_color )
169 get_red_from_yuv422( p_pic, p_outpic,
170 Y_PLANE, U_PLANE, V_PLANE );
173 get_green_from_yuv422( p_pic, p_outpic,
174 Y_PLANE, U_PLANE, V_PLANE );
177 get_blue_from_yuv422( p_pic, p_outpic,
178 Y_PLANE, U_PLANE, V_PLANE );
184 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
185 (char*)&(p_pic->format.i_chroma) );
186 if( p_pic->pf_release )
187 p_pic->pf_release( p_pic );
191 p_outpic->date = p_pic->date;
192 p_outpic->b_force = p_pic->b_force;
193 p_outpic->i_nb_fields = p_pic->i_nb_fields;
194 p_outpic->b_progressive = p_pic->b_progressive;
195 p_outpic->b_top_field_first = p_pic->b_top_field_first;
197 if( p_pic->pf_release )
198 p_pic->pf_release( p_pic );
203 inline uint8_t crop( int a );
204 inline uint8_t crop( int a )
206 if( a < 0 ) return 0;
207 if( a > 255 ) return 255;
208 else return (uint8_t)a;
214 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
215 int yp, int up, int vp )
217 uint8_t *y1in = p_inpic->p[yp].p_pixels;
219 uint8_t *vin = p_inpic->p[vp].p_pixels;
221 uint8_t *y1out = p_outpic->p[yp].p_pixels;
223 uint8_t *uout = p_outpic->p[up].p_pixels;
224 uint8_t *vout = p_outpic->p[vp].p_pixels;
226 const int i_pitch = p_inpic->p[yp].i_pitch;
227 const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
228 const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
230 const int i_uv_pitch = p_inpic->p[up].i_pitch;
231 const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
233 const uint8_t *yend = y1in + i_visible_lines * i_pitch;
236 const uint8_t *y1end = y1in + i_visible_pitch;
237 y2in = y1in + i_pitch;
238 y2out = y1out + i_pitch;
239 while( y1in < y1end )
246 *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
248 *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
250 *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
252 *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
254 *y2out++ = crop( (*y2in++ * 19594 + (*vin - V) * 27473)
256 *y2out++ = crop( (*y2in++ * 19594 + (*vin++ - V) * 27473)
259 y1in += 2*i_pitch - i_visible_pitch;
260 y1out += 2*i_pitch - i_visible_pitch;
261 uout += i_uv_pitch - i_uv_visible_pitch;
262 vin += i_uv_pitch - i_uv_visible_pitch;
263 vout += i_uv_pitch - i_uv_visible_pitch;
267 static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
268 int yp, int up, int vp )
270 uint8_t *y1in = p_inpic->p[yp].p_pixels;
272 uint8_t *uin = p_inpic->p[up].p_pixels;
273 uint8_t *vin = p_inpic->p[vp].p_pixels;
275 uint8_t *y1out = p_outpic->p[yp].p_pixels;
277 uint8_t *uout = p_outpic->p[up].p_pixels;
278 uint8_t *vout = p_outpic->p[vp].p_pixels;
280 const int i_pitch = p_inpic->p[yp].i_pitch;
281 const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
282 const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
284 const int i_uv_pitch = p_inpic->p[up].i_pitch;
285 const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
287 const uint8_t *yend = y1in + i_visible_lines * i_pitch;
290 const uint8_t *y1end = y1in + i_visible_pitch;
291 y2in = y1in + i_pitch;
292 y2out = y1out + i_pitch;
293 while( y1in < y1end )
300 *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
302 *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
304 *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
306 *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
308 *y2out++ = crop( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
310 *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -128) * -27473)
313 y1in += 2*i_pitch - i_visible_pitch;
314 y1out += 2*i_pitch - i_visible_pitch;
315 uin += i_uv_pitch - i_uv_visible_pitch;
316 uout += i_uv_pitch - i_uv_visible_pitch;
317 vin += i_uv_pitch - i_uv_visible_pitch;
318 vout += i_uv_pitch - i_uv_visible_pitch;
322 static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
323 int yp, int up, int vp )
325 uint8_t *y1in = p_inpic->p[yp].p_pixels;
327 uint8_t *uin = p_inpic->p[up].p_pixels;
329 uint8_t *y1out = p_outpic->p[yp].p_pixels;
331 uint8_t *uout = p_outpic->p[up].p_pixels;
332 uint8_t *vout = p_outpic->p[vp].p_pixels;
334 const int i_pitch = p_inpic->p[yp].i_pitch;
335 const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
336 const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
338 const int i_uv_pitch = p_inpic->p[up].i_pitch;
339 const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
341 const uint8_t *yend = y1in + i_visible_lines * i_pitch;
344 const uint8_t *y1end = y1in + i_visible_pitch;
345 y2in = y1in + i_pitch;
346 y2out = y1out + i_pitch;
347 while( y1in < y1end )
354 *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
356 *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
358 *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
360 *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
362 *y2out++ = crop( (*y2in++ * 7471 + (*uin - U) * 13239 )
364 *y2out++ = crop( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
367 y1in += 2*i_pitch - i_visible_pitch;
368 y1out += 2*i_pitch - i_visible_pitch;
369 uin += i_uv_pitch - i_uv_visible_pitch;
370 uout += i_uv_pitch - i_uv_visible_pitch;
371 vout += i_uv_pitch - i_uv_visible_pitch;
375 static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
376 int yp, int up, int vp )
378 uint8_t *y1in = p_inpic->p[yp].p_pixels;
379 uint8_t *vin = p_inpic->p[vp].p_pixels;
381 uint8_t *y1out = p_outpic->p[yp].p_pixels;
382 uint8_t *uout = p_outpic->p[up].p_pixels;
383 uint8_t *vout = p_outpic->p[vp].p_pixels;
385 const int i_pitch = p_inpic->p[yp].i_pitch;
386 const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
387 const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
389 const int i_uv_pitch = p_inpic->p[up].i_pitch;
390 const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
392 const uint8_t *yend = y1in + i_visible_lines * i_pitch;
395 const uint8_t *y1end = y1in + i_visible_pitch;
396 while( y1in < y1end )
403 *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
405 *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
407 *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
409 *y1out++ = crop( (*y1in++ * 19595 + (*vin++ - V) * 27473)
412 y1in += i_pitch - i_visible_pitch;
413 y1out += i_pitch - i_visible_pitch;
414 uout += i_uv_pitch - i_uv_visible_pitch;
415 vin += i_uv_pitch - i_uv_visible_pitch;
416 vout += i_uv_pitch - i_uv_visible_pitch;
420 static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
421 int yp, int up, int vp )
423 uint8_t *y1in = p_inpic->p[yp].p_pixels;
424 uint8_t *uin = p_inpic->p[up].p_pixels;
425 uint8_t *vin = p_inpic->p[vp].p_pixels;
427 uint8_t *y1out = p_outpic->p[yp].p_pixels;
428 uint8_t *uout = p_outpic->p[up].p_pixels;
429 uint8_t *vout = p_outpic->p[vp].p_pixels;
431 const int i_pitch = p_inpic->p[yp].i_pitch;
432 const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
433 const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
435 const int i_uv_pitch = p_inpic->p[up].i_pitch;
436 const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
438 const uint8_t *yend = y1in + i_visible_lines * i_pitch;
441 const uint8_t *y1end = y1in + i_visible_pitch;
442 while( y1in < y1end )
449 *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
451 *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
453 *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
455 *y1out++ = crop( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
458 y1in += i_pitch - i_visible_pitch;
459 y1out += i_pitch - i_visible_pitch;
460 uin += i_uv_pitch - i_uv_visible_pitch;
461 uout += i_uv_pitch - i_uv_visible_pitch;
462 vin += i_uv_pitch - i_uv_visible_pitch;
463 vout += i_uv_pitch - i_uv_visible_pitch;
467 static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
468 int yp, int up, int vp )
470 uint8_t *y1in = p_inpic->p[yp].p_pixels;
471 uint8_t *uin = p_inpic->p[up].p_pixels;
473 uint8_t *y1out = p_outpic->p[yp].p_pixels;
474 uint8_t *uout = p_outpic->p[up].p_pixels;
475 uint8_t *vout = p_outpic->p[vp].p_pixels;
477 const int i_pitch = p_inpic->p[yp].i_pitch;
478 const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
479 const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
481 const int i_uv_pitch = p_inpic->p[up].i_pitch;
482 const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
484 const uint8_t *yend = y1in + i_visible_lines * i_pitch;
487 const uint8_t *y1end = y1in + i_visible_pitch;
488 while( y1in < y1end )
495 *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
497 *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
499 *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
501 *y1out++ = crop( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
504 y1in += i_pitch - i_visible_pitch;
505 y1out += i_pitch - i_visible_pitch;
506 uin += i_uv_pitch - i_uv_visible_pitch;
507 uout += i_uv_pitch - i_uv_visible_pitch;
508 vout += i_uv_pitch - i_uv_visible_pitch;