1 /*****************************************************************************
2 * transform.c : transform image module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 char *type_list[] = { "90", "180", "270", "hflip", "vflip" };
61 static 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") };
66 set_description( _("Video transformation filter") );
67 set_shortname( N_("Transformation"));
68 set_capability( "video filter", 0 );
69 set_category( CAT_VIDEO );
70 set_subcategory( SUBCAT_VIDEO_VFILTER );
72 add_string( "transform-type", "90", NULL,
73 TYPE_TEXT, TYPE_LONGTEXT, VLC_FALSE);
74 change_string_list( type_list, type_list_text, 0);
76 add_shortcut( "transform" );
77 set_callbacks( Create, Destroy );
80 /*****************************************************************************
81 * vout_sys_t: Transform video output method descriptor
82 *****************************************************************************
83 * This structure is part of the video output thread descriptor.
84 * It describes the Transform specific properties of an output thread.
85 *****************************************************************************/
89 vlc_bool_t b_rotation;
90 vout_thread_t *p_vout;
93 /*****************************************************************************
94 * Control: control facility for the vout (forwards to child vout)
95 *****************************************************************************/
96 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
98 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
101 /*****************************************************************************
102 * Create: allocates Transform video thread output method
103 *****************************************************************************
104 * This function allocates and initializes a Transform vout method.
105 *****************************************************************************/
106 static int Create( vlc_object_t *p_this )
108 vout_thread_t *p_vout = (vout_thread_t *)p_this;
111 /* Allocate structure */
112 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
113 if( p_vout->p_sys == NULL )
115 msg_Err( p_vout, "out of memory" );
119 p_vout->pf_init = Init;
120 p_vout->pf_end = End;
121 p_vout->pf_manage = NULL;
122 p_vout->pf_render = Render;
123 p_vout->pf_display = NULL;
124 p_vout->pf_control = Control;
126 /* Look what method was requested */
127 psz_method = config_GetPsz( p_vout, "transform-type" );
129 if( psz_method == NULL )
131 msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
132 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
133 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
134 p_vout->p_sys->b_rotation = 1;
138 if( !strcmp( psz_method, "hflip" ) )
140 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
141 p_vout->p_sys->b_rotation = 0;
143 else if( !strcmp( psz_method, "vflip" ) )
145 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
146 p_vout->p_sys->b_rotation = 0;
148 else if( !strcmp( psz_method, "90" ) )
150 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
151 p_vout->p_sys->b_rotation = 1;
153 else if( !strcmp( psz_method, "180" ) )
155 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
156 p_vout->p_sys->b_rotation = 0;
158 else if( !strcmp( psz_method, "270" ) )
160 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
161 p_vout->p_sys->b_rotation = 1;
165 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
166 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
167 p_vout->p_sys->b_rotation = 1;
176 /*****************************************************************************
177 * Init: initialize Transform video thread output method
178 *****************************************************************************/
179 static int Init( vout_thread_t *p_vout )
183 video_format_t fmt = {0};
185 I_OUTPUTPICTURES = 0;
187 /* Initialize the output structure */
188 p_vout->output.i_chroma = p_vout->render.i_chroma;
189 p_vout->output.i_width = p_vout->render.i_width;
190 p_vout->output.i_height = p_vout->render.i_height;
191 p_vout->output.i_aspect = p_vout->render.i_aspect;
192 p_vout->fmt_out = p_vout->fmt_in;
193 fmt = p_vout->fmt_out;
195 /* Try to open the real video output */
196 msg_Dbg( p_vout, "spawning the real video output" );
198 if( p_vout->p_sys->b_rotation )
200 fmt.i_width = p_vout->fmt_out.i_height;
201 fmt.i_visible_width = p_vout->fmt_out.i_visible_height;
202 fmt.i_x_offset = p_vout->fmt_out.i_y_offset;
204 fmt.i_height = p_vout->fmt_out.i_width;
205 fmt.i_visible_height = p_vout->fmt_out.i_visible_width;
206 fmt.i_y_offset = p_vout->fmt_out.i_x_offset;
208 fmt.i_aspect = VOUT_ASPECT_FACTOR *
209 (uint64_t)VOUT_ASPECT_FACTOR / fmt.i_aspect;
211 fmt.i_sar_num = p_vout->fmt_out.i_sar_den;
212 fmt.i_sar_den = p_vout->fmt_out.i_sar_num;
214 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
218 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
221 /* Everything failed */
222 if( p_vout->p_sys->p_vout == NULL )
224 msg_Err( p_vout, "cannot open vout, aborting" );
228 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
230 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
232 ADD_PARENT_CALLBACKS( SendEventsToChild );
237 /*****************************************************************************
238 * End: terminate Transform video thread output method
239 *****************************************************************************/
240 static void End( vout_thread_t *p_vout )
244 /* Free the fake output buffers we allocated */
245 for( i_index = I_OUTPUTPICTURES ; i_index ; )
248 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
252 /*****************************************************************************
253 * Destroy: destroy Transform video thread output method
254 *****************************************************************************
255 * Terminate an output method created by TransformCreateOutputMethod
256 *****************************************************************************/
257 static void Destroy( vlc_object_t *p_this )
259 vout_thread_t *p_vout = (vout_thread_t *)p_this;
261 if( p_vout->p_sys->p_vout )
263 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
264 vlc_object_detach( p_vout->p_sys->p_vout );
265 vout_Destroy( p_vout->p_sys->p_vout );
268 DEL_PARENT_CALLBACKS( SendEventsToChild );
270 free( p_vout->p_sys );
273 /*****************************************************************************
274 * Render: displays previously rendered output
275 *****************************************************************************
276 * This function send the currently rendered image to Transform image, waits
277 * until it is displayed and switch the two rendering buffers, preparing next
279 *****************************************************************************/
280 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
285 /* This is a new frame. Get a structure from the video_output. */
286 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
289 if( p_vout->b_die || p_vout->b_error )
293 msleep( VOUT_OUTMEM_SLEEP );
296 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
297 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
299 switch( p_vout->p_sys->i_mode )
301 case TRANSFORM_MODE_90:
302 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
304 int i_pitch = p_pic->p[i_index].i_pitch;
306 uint8_t *p_in = p_pic->p[i_index].p_pixels;
308 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
309 uint8_t *p_out_end = p_out +
310 p_outpic->p[i_index].i_visible_lines *
311 p_outpic->p[i_index].i_pitch;
313 for( ; p_out < p_out_end ; )
317 p_out_end -= p_outpic->p[i_index].i_pitch
318 - p_outpic->p[i_index].i_visible_pitch;
319 p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
322 for( ; p_in < p_line_end ; )
324 p_line_end -= i_pitch;
325 *(--p_out_end) = *p_line_end;
333 case TRANSFORM_MODE_180:
334 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
336 uint8_t *p_in = p_pic->p[i_index].p_pixels;
337 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
338 * p_pic->p[i_index].i_pitch;
340 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
342 for( ; p_in < p_in_end ; )
344 uint8_t *p_line_start = p_in_end
345 - p_pic->p[i_index].i_pitch;
346 p_in_end -= p_pic->p[i_index].i_pitch
347 - p_pic->p[i_index].i_visible_pitch;
349 for( ; p_line_start < p_in_end ; )
351 *p_out++ = *(--p_in_end);
354 p_out += p_outpic->p[i_index].i_pitch
355 - p_outpic->p[i_index].i_visible_pitch;
360 case TRANSFORM_MODE_270:
361 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
363 int i_pitch = p_pic->p[i_index].i_pitch;
365 uint8_t *p_in = p_pic->p[i_index].p_pixels;
367 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
368 uint8_t *p_out_end = p_out +
369 p_outpic->p[i_index].i_visible_lines *
370 p_outpic->p[i_index].i_pitch;
372 for( ; p_out < p_out_end ; )
376 p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
379 for( ; p_in < p_in_end ; )
382 *p_out++ = *p_in_end;
385 p_out += p_outpic->p[i_index].i_pitch
386 - p_outpic->p[i_index].i_visible_pitch;
392 case TRANSFORM_MODE_HFLIP:
393 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
395 uint8_t *p_in = p_pic->p[i_index].p_pixels;
396 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
397 * p_pic->p[i_index].i_pitch;
399 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
401 for( ; p_in < p_in_end ; )
403 p_in_end -= p_pic->p[i_index].i_pitch;
404 p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
405 p_pic->p[i_index].i_visible_pitch );
406 p_out += p_pic->p[i_index].i_pitch;
411 case TRANSFORM_MODE_VFLIP:
412 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
414 uint8_t *p_in = p_pic->p[i_index].p_pixels;
415 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
416 * p_pic->p[i_index].i_pitch;
418 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
420 for( ; p_in < p_in_end ; )
422 uint8_t *p_line_end = p_in
423 + p_pic->p[i_index].i_visible_pitch;
425 for( ; p_in < p_line_end ; )
427 *p_out++ = *(--p_line_end);
430 p_in += p_pic->p[i_index].i_pitch;
439 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
441 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
444 /*****************************************************************************
445 * SendEvents: forward mouse and keyboard events to the parent p_vout
446 *****************************************************************************/
447 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
448 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
450 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
451 vlc_value_t sentval = newval;
453 /* Translate the mouse coordinates */
454 if( !strcmp( psz_var, "mouse-x" ) )
456 switch( p_vout->p_sys->i_mode )
458 case TRANSFORM_MODE_270:
459 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
461 case TRANSFORM_MODE_90:
462 var_Set( p_vout, "mouse-y", sentval );
465 case TRANSFORM_MODE_180:
466 case TRANSFORM_MODE_HFLIP:
467 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
471 case TRANSFORM_MODE_VFLIP:
476 else if( !strcmp( psz_var, "mouse-y" ) )
478 switch( p_vout->p_sys->i_mode )
480 case TRANSFORM_MODE_90:
481 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
483 case TRANSFORM_MODE_270:
484 var_Set( p_vout, "mouse-x", sentval );
487 case TRANSFORM_MODE_180:
488 case TRANSFORM_MODE_VFLIP:
489 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
493 case TRANSFORM_MODE_HFLIP:
499 var_Set( p_vout, psz_var, sentval );
504 /*****************************************************************************
505 * SendEventsToChild: forward events to the child/children vout
506 *****************************************************************************/
507 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
508 vlc_value_t oldval, vlc_value_t newval, void *p_data )
510 vout_thread_t *p_vout = (vout_thread_t *)p_this;
511 var_Set( p_vout->p_sys->p_vout, psz_var, newval );