1 /*****************************************************************************
2 * transform.c : transform image module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
5 * $Id: transform.c,v 1.17 2004/01/25 03:28:41 hartman Exp $
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 add_category_hint( N_("Miscellaneous"), NULL, VLC_FALSE );
67 add_string( "transform-type", "90", NULL,
68 TYPE_TEXT, TYPE_LONGTEXT, VLC_FALSE);
69 change_string_list( type_list, type_list_text, 0);
70 set_description( _("Video transformation filter") );
71 set_capability( "video filter", 0 );
72 add_shortcut( "transform" );
73 set_callbacks( Create, Destroy );
76 /*****************************************************************************
77 * vout_sys_t: Transform video output method descriptor
78 *****************************************************************************
79 * This structure is part of the video output thread descriptor.
80 * It describes the Transform specific properties of an output thread.
81 *****************************************************************************/
85 vlc_bool_t b_rotation;
86 vout_thread_t *p_vout;
89 /*****************************************************************************
90 * Create: allocates Transform video thread output method
91 *****************************************************************************
92 * This function allocates and initializes a Transform vout method.
93 *****************************************************************************/
94 static int Create( vlc_object_t *p_this )
96 vout_thread_t *p_vout = (vout_thread_t *)p_this;
99 /* Allocate structure */
100 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
101 if( p_vout->p_sys == NULL )
103 msg_Err( p_vout, "out of memory" );
107 p_vout->pf_init = Init;
108 p_vout->pf_end = End;
109 p_vout->pf_manage = NULL;
110 p_vout->pf_render = Render;
111 p_vout->pf_display = NULL;
113 /* Look what method was requested */
114 psz_method = config_GetPsz( p_vout, "transform-type" );
116 if( psz_method == NULL )
118 msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
119 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
120 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
121 p_vout->p_sys->b_rotation = 1;
125 if( !strcmp( psz_method, "hflip" ) )
127 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
128 p_vout->p_sys->b_rotation = 0;
130 else if( !strcmp( psz_method, "vflip" ) )
132 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
133 p_vout->p_sys->b_rotation = 0;
135 else if( !strcmp( psz_method, "90" ) )
137 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
138 p_vout->p_sys->b_rotation = 1;
140 else if( !strcmp( psz_method, "180" ) )
142 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
143 p_vout->p_sys->b_rotation = 0;
145 else if( !strcmp( psz_method, "270" ) )
147 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
148 p_vout->p_sys->b_rotation = 1;
152 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
153 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
154 p_vout->p_sys->b_rotation = 1;
163 /*****************************************************************************
164 * Init: initialize Transform video thread output method
165 *****************************************************************************/
166 static int Init( vout_thread_t *p_vout )
171 I_OUTPUTPICTURES = 0;
173 /* Initialize the output structure */
174 p_vout->output.i_chroma = p_vout->render.i_chroma;
175 p_vout->output.i_width = p_vout->render.i_width;
176 p_vout->output.i_height = p_vout->render.i_height;
177 p_vout->output.i_aspect = p_vout->render.i_aspect;
179 /* Try to open the real video output */
180 msg_Dbg( p_vout, "spawning the real video output" );
182 if( p_vout->p_sys->b_rotation )
184 p_vout->p_sys->p_vout = vout_Create( p_vout,
185 p_vout->render.i_height, p_vout->render.i_width,
186 p_vout->render.i_chroma,
187 (uint64_t)VOUT_ASPECT_FACTOR
188 * (uint64_t)VOUT_ASPECT_FACTOR
189 / (uint64_t)p_vout->render.i_aspect );
193 p_vout->p_sys->p_vout = vout_Create( p_vout,
194 p_vout->render.i_width, p_vout->render.i_height,
195 p_vout->render.i_chroma, p_vout->render.i_aspect );
198 /* Everything failed */
199 if( p_vout->p_sys->p_vout == NULL )
201 msg_Err( p_vout, "cannot open vout, aborting" );
205 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
207 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
209 ADD_PARENT_CALLBACKS( SendEventsToChild );
214 /*****************************************************************************
215 * End: terminate Transform video thread output method
216 *****************************************************************************/
217 static void End( vout_thread_t *p_vout )
221 /* Free the fake output buffers we allocated */
222 for( i_index = I_OUTPUTPICTURES ; i_index ; )
225 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
229 /*****************************************************************************
230 * Destroy: destroy Transform video thread output method
231 *****************************************************************************
232 * Terminate an output method created by TransformCreateOutputMethod
233 *****************************************************************************/
234 static void Destroy( vlc_object_t *p_this )
236 vout_thread_t *p_vout = (vout_thread_t *)p_this;
238 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
239 vlc_object_detach( p_vout->p_sys->p_vout );
240 vout_Destroy( p_vout->p_sys->p_vout );
242 DEL_PARENT_CALLBACKS( SendEventsToChild );
244 free( p_vout->p_sys );
247 /*****************************************************************************
248 * Render: displays previously rendered output
249 *****************************************************************************
250 * This function send the currently rendered image to Transform image, waits
251 * until it is displayed and switch the two rendering buffers, preparing next
253 *****************************************************************************/
254 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
259 /* This is a new frame. Get a structure from the video_output. */
260 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
263 if( p_vout->b_die || p_vout->b_error )
267 msleep( VOUT_OUTMEM_SLEEP );
270 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
271 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
273 switch( p_vout->p_sys->i_mode )
275 case TRANSFORM_MODE_90:
276 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
278 int i_pitch = p_pic->p[i_index].i_pitch;
280 uint8_t *p_in = p_pic->p[i_index].p_pixels;
282 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
283 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
284 * p_outpic->p[i_index].i_pitch;
286 for( ; p_out < p_out_end ; )
290 p_out_end -= p_outpic->p[i_index].i_pitch
291 - p_outpic->p[i_index].i_visible_pitch;
292 p_line_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
294 for( ; p_in < p_line_end ; )
296 p_line_end -= i_pitch;
297 *(--p_out_end) = *p_line_end;
305 case TRANSFORM_MODE_180:
306 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
308 uint8_t *p_in = p_pic->p[i_index].p_pixels;
309 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
310 * p_pic->p[i_index].i_pitch;
312 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
314 for( ; p_in < p_in_end ; )
316 uint8_t *p_line_start = p_in_end
317 - p_pic->p[i_index].i_pitch;
318 p_in_end -= p_pic->p[i_index].i_pitch
319 - p_pic->p[i_index].i_visible_pitch;
321 for( ; p_line_start < p_in_end ; )
323 *p_out++ = *(--p_in_end);
326 p_out += p_outpic->p[i_index].i_pitch
327 - p_outpic->p[i_index].i_visible_pitch;
332 case TRANSFORM_MODE_270:
333 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
335 int i_pitch = p_pic->p[i_index].i_pitch;
337 uint8_t *p_in = p_pic->p[i_index].p_pixels;
339 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
340 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
341 * p_outpic->p[i_index].i_pitch;
343 for( ; p_out < p_out_end ; )
347 p_in_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
349 for( ; p_in < p_in_end ; )
352 *p_out++ = *p_in_end;
355 p_out += p_outpic->p[i_index].i_pitch
356 - p_outpic->p[i_index].i_visible_pitch;
362 case TRANSFORM_MODE_HFLIP:
363 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
365 uint8_t *p_in = p_pic->p[i_index].p_pixels;
366 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
367 * p_pic->p[i_index].i_pitch;
369 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
371 for( ; p_in < p_in_end ; )
373 p_in_end -= p_pic->p[i_index].i_pitch;
374 p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
375 p_pic->p[i_index].i_visible_pitch );
376 p_out += p_pic->p[i_index].i_pitch;
381 case TRANSFORM_MODE_VFLIP:
382 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
384 uint8_t *p_in = p_pic->p[i_index].p_pixels;
385 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
386 * p_pic->p[i_index].i_pitch;
388 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
390 for( ; p_in < p_in_end ; )
392 uint8_t *p_line_end = p_in
393 + p_pic->p[i_index].i_visible_pitch;
395 for( ; p_in < p_line_end ; )
397 *p_out++ = *(--p_line_end);
400 p_in += p_pic->p[i_index].i_pitch;
409 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
411 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
414 /*****************************************************************************
415 * SendEvents: forward mouse and keyboard events to the parent p_vout
416 *****************************************************************************/
417 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
418 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
420 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
421 vlc_value_t sentval = newval;
423 /* Translate the mouse coordinates */
424 if( !strcmp( psz_var, "mouse-x" ) )
426 switch( p_vout->p_sys->i_mode )
428 case TRANSFORM_MODE_270:
429 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
431 case TRANSFORM_MODE_90:
432 var_Set( p_vout, "mouse-y", sentval );
435 case TRANSFORM_MODE_180:
436 case TRANSFORM_MODE_HFLIP:
437 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
441 case TRANSFORM_MODE_VFLIP:
446 else if( !strcmp( psz_var, "mouse-y" ) )
448 switch( p_vout->p_sys->i_mode )
450 case TRANSFORM_MODE_90:
451 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
453 case TRANSFORM_MODE_270:
454 var_Set( p_vout, "mouse-x", sentval );
457 case TRANSFORM_MODE_180:
458 case TRANSFORM_MODE_VFLIP:
459 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
463 case TRANSFORM_MODE_HFLIP:
469 var_Set( p_vout, psz_var, sentval );
474 /*****************************************************************************
475 * SendEventsToChild: forward events to the child/children vout
476 *****************************************************************************/
477 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
478 vlc_value_t oldval, vlc_value_t newval, void *p_data )
480 vout_thread_t *p_vout = (vout_thread_t *)p_this;
481 var_Set( p_vout->p_sys->p_vout, psz_var, newval );