1 /*****************************************************************************
2 * transform.c : transform image module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.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 "filter_common.h"
35 #define TRANSFORM_MODE_HFLIP 1
36 #define TRANSFORM_MODE_VFLIP 2
37 #define TRANSFORM_MODE_90 3
38 #define TRANSFORM_MODE_180 4
39 #define TRANSFORM_MODE_270 5
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static int Init ( vout_thread_t * );
48 static void End ( vout_thread_t * );
49 static void Render ( vout_thread_t *, picture_t * );
51 static int SendEvents( vlc_object_t *, char const *,
52 vlc_value_t, vlc_value_t, void * );
54 /*****************************************************************************
56 *****************************************************************************/
57 #define TYPE_TEXT N_("Transform type")
58 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
60 static const char *type_list[] = { "90", "180", "270", "hflip", "vflip" };
61 static const char *type_list_text[] = { N_("Rotate by 90 degrees"),
62 N_("Rotate by 180 degrees"), N_("Rotate by 270 degrees"),
63 N_("Flip horizontally"), N_("Flip vertically") };
65 #define CFG_PREFIX "transform-"
68 set_description( _("Video transformation filter") );
69 set_shortname( _("Transformation"));
70 set_capability( "video filter", 0 );
71 set_category( CAT_VIDEO );
72 set_subcategory( SUBCAT_VIDEO_VFILTER );
74 add_string( CFG_PREFIX "type", "90", NULL,
75 TYPE_TEXT, TYPE_LONGTEXT, VLC_FALSE);
76 change_string_list( type_list, type_list_text, 0);
78 add_shortcut( "transform" );
79 set_callbacks( Create, Destroy );
82 static const char *ppsz_filter_options[] = {
86 /*****************************************************************************
87 * vout_sys_t: Transform video output method descriptor
88 *****************************************************************************
89 * This structure is part of the video output thread descriptor.
90 * It describes the Transform specific properties of an output thread.
91 *****************************************************************************/
95 vlc_bool_t b_rotation;
96 vout_thread_t *p_vout;
99 /*****************************************************************************
100 * Control: control facility for the vout (forwards to child vout)
101 *****************************************************************************/
102 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
104 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
107 /*****************************************************************************
108 * Create: allocates Transform video thread output method
109 *****************************************************************************
110 * This function allocates and initializes a Transform vout method.
111 *****************************************************************************/
112 static int Create( vlc_object_t *p_this )
114 vout_thread_t *p_vout = (vout_thread_t *)p_this;
117 /* Allocate structure */
118 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
119 if( p_vout->p_sys == NULL )
121 msg_Err( p_vout, "out of memory" );
125 p_vout->pf_init = Init;
126 p_vout->pf_end = End;
127 p_vout->pf_manage = NULL;
128 p_vout->pf_render = Render;
129 p_vout->pf_display = NULL;
130 p_vout->pf_control = Control;
132 config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
135 /* Look what method was requested */
136 psz_method = var_CreateGetNonEmptyString( p_vout, "transform-type" );
138 if( psz_method == NULL )
140 msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
141 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
142 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
143 p_vout->p_sys->b_rotation = 1;
147 if( !strcmp( psz_method, "hflip" ) )
149 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
150 p_vout->p_sys->b_rotation = 0;
152 else if( !strcmp( psz_method, "vflip" ) )
154 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
155 p_vout->p_sys->b_rotation = 0;
157 else if( !strcmp( psz_method, "90" ) )
159 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
160 p_vout->p_sys->b_rotation = 1;
162 else if( !strcmp( psz_method, "180" ) )
164 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
165 p_vout->p_sys->b_rotation = 0;
167 else if( !strcmp( psz_method, "270" ) )
169 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
170 p_vout->p_sys->b_rotation = 1;
174 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
175 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
176 p_vout->p_sys->b_rotation = 1;
185 /*****************************************************************************
186 * Init: initialize Transform video thread output method
187 *****************************************************************************/
188 static int Init( vout_thread_t *p_vout )
192 video_format_t fmt = {0};
194 I_OUTPUTPICTURES = 0;
196 /* Initialize the output structure */
197 p_vout->output.i_chroma = p_vout->render.i_chroma;
198 p_vout->output.i_width = p_vout->render.i_width;
199 p_vout->output.i_height = p_vout->render.i_height;
200 p_vout->output.i_aspect = p_vout->render.i_aspect;
201 p_vout->fmt_out = p_vout->fmt_in;
202 fmt = p_vout->fmt_out;
204 /* Try to open the real video output */
205 msg_Dbg( p_vout, "spawning the real video output" );
207 if( p_vout->p_sys->b_rotation )
209 fmt.i_width = p_vout->fmt_out.i_height;
210 fmt.i_visible_width = p_vout->fmt_out.i_visible_height;
211 fmt.i_x_offset = p_vout->fmt_out.i_y_offset;
213 fmt.i_height = p_vout->fmt_out.i_width;
214 fmt.i_visible_height = p_vout->fmt_out.i_visible_width;
215 fmt.i_y_offset = p_vout->fmt_out.i_x_offset;
217 fmt.i_aspect = VOUT_ASPECT_FACTOR *
218 (uint64_t)VOUT_ASPECT_FACTOR / fmt.i_aspect;
220 fmt.i_sar_num = p_vout->fmt_out.i_sar_den;
221 fmt.i_sar_den = p_vout->fmt_out.i_sar_num;
224 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
226 /* Everything failed */
227 if( p_vout->p_sys->p_vout == NULL )
229 msg_Err( p_vout, "cannot open vout, aborting" );
233 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
235 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
237 ADD_PARENT_CALLBACKS( SendEventsToChild );
242 /*****************************************************************************
243 * End: terminate Transform video thread output method
244 *****************************************************************************/
245 static void End( vout_thread_t *p_vout )
249 /* Free the fake output buffers we allocated */
250 for( i_index = I_OUTPUTPICTURES ; i_index ; )
253 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
257 /*****************************************************************************
258 * Destroy: destroy Transform video thread output method
259 *****************************************************************************
260 * Terminate an output method created by TransformCreateOutputMethod
261 *****************************************************************************/
262 static void Destroy( vlc_object_t *p_this )
264 vout_thread_t *p_vout = (vout_thread_t *)p_this;
266 if( p_vout->p_sys->p_vout )
268 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
269 vlc_object_detach( p_vout->p_sys->p_vout );
270 vout_Destroy( p_vout->p_sys->p_vout );
273 DEL_PARENT_CALLBACKS( SendEventsToChild );
275 free( p_vout->p_sys );
278 /*****************************************************************************
279 * Render: displays previously rendered output
280 *****************************************************************************
281 * This function send the currently rendered image to Transform image, waits
282 * until it is displayed and switch the two rendering buffers, preparing next
284 *****************************************************************************/
285 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
290 /* This is a new frame. Get a structure from the video_output. */
291 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
294 if( p_vout->b_die || p_vout->b_error )
298 msleep( VOUT_OUTMEM_SLEEP );
301 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
302 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
304 switch( p_vout->p_sys->i_mode )
306 case TRANSFORM_MODE_90:
307 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
309 int i_pitch = p_pic->p[i_index].i_pitch;
311 uint8_t *p_in = p_pic->p[i_index].p_pixels;
313 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
314 uint8_t *p_out_end = p_out +
315 p_outpic->p[i_index].i_visible_lines *
316 p_outpic->p[i_index].i_pitch;
318 for( ; p_out < p_out_end ; )
322 p_out_end -= p_outpic->p[i_index].i_pitch
323 - p_outpic->p[i_index].i_visible_pitch;
324 p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
327 for( ; p_in < p_line_end ; )
329 p_line_end -= i_pitch;
330 *(--p_out_end) = *p_line_end;
338 case TRANSFORM_MODE_180:
339 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
341 uint8_t *p_in = p_pic->p[i_index].p_pixels;
342 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
343 * p_pic->p[i_index].i_pitch;
345 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
347 for( ; p_in < p_in_end ; )
349 uint8_t *p_line_start = p_in_end
350 - p_pic->p[i_index].i_pitch;
351 p_in_end -= p_pic->p[i_index].i_pitch
352 - p_pic->p[i_index].i_visible_pitch;
354 for( ; p_line_start < p_in_end ; )
356 *p_out++ = *(--p_in_end);
359 p_out += p_outpic->p[i_index].i_pitch
360 - p_outpic->p[i_index].i_visible_pitch;
365 case TRANSFORM_MODE_270:
366 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
368 int i_pitch = p_pic->p[i_index].i_pitch;
370 uint8_t *p_in = p_pic->p[i_index].p_pixels;
372 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
373 uint8_t *p_out_end = p_out +
374 p_outpic->p[i_index].i_visible_lines *
375 p_outpic->p[i_index].i_pitch;
377 for( ; p_out < p_out_end ; )
381 p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
384 for( ; p_in < p_in_end ; )
387 *p_out++ = *p_in_end;
390 p_out += p_outpic->p[i_index].i_pitch
391 - p_outpic->p[i_index].i_visible_pitch;
397 case TRANSFORM_MODE_HFLIP:
398 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
400 uint8_t *p_in = p_pic->p[i_index].p_pixels;
401 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
402 * p_pic->p[i_index].i_pitch;
404 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
406 for( ; p_in < p_in_end ; )
408 p_in_end -= p_pic->p[i_index].i_pitch;
409 p_vout->p_libvlc->pf_memcpy( p_out, p_in_end,
410 p_pic->p[i_index].i_visible_pitch );
411 p_out += p_pic->p[i_index].i_pitch;
416 case TRANSFORM_MODE_VFLIP:
417 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
419 uint8_t *p_in = p_pic->p[i_index].p_pixels;
420 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
421 * p_pic->p[i_index].i_pitch;
423 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
425 for( ; p_in < p_in_end ; )
427 uint8_t *p_line_end = p_in
428 + p_pic->p[i_index].i_visible_pitch;
430 for( ; p_in < p_line_end ; )
432 *p_out++ = *(--p_line_end);
435 p_in += p_pic->p[i_index].i_pitch;
444 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
446 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
449 /*****************************************************************************
450 * SendEvents: forward mouse and keyboard events to the parent p_vout
451 *****************************************************************************/
452 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
453 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
455 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
456 vlc_value_t sentval = newval;
458 /* Translate the mouse coordinates */
459 if( !strcmp( psz_var, "mouse-x" ) )
461 switch( p_vout->p_sys->i_mode )
463 case TRANSFORM_MODE_270:
464 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
466 case TRANSFORM_MODE_90:
467 var_Set( p_vout, "mouse-y", sentval );
470 case TRANSFORM_MODE_180:
471 case TRANSFORM_MODE_HFLIP:
472 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
476 case TRANSFORM_MODE_VFLIP:
481 else if( !strcmp( psz_var, "mouse-y" ) )
483 switch( p_vout->p_sys->i_mode )
485 case TRANSFORM_MODE_90:
486 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
488 case TRANSFORM_MODE_270:
489 var_Set( p_vout, "mouse-x", sentval );
492 case TRANSFORM_MODE_180:
493 case TRANSFORM_MODE_VFLIP:
494 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
498 case TRANSFORM_MODE_HFLIP:
504 var_Set( p_vout, psz_var, sentval );
509 /*****************************************************************************
510 * SendEventsToChild: forward events to the child/children vout
511 *****************************************************************************/
512 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
513 vlc_value_t oldval, vlc_value_t newval, void *p_data )
515 vout_thread_t *p_vout = (vout_thread_t *)p_this;
516 var_Set( p_vout->p_sys->p_vout, psz_var, newval );