1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5 * $Id: transform.c,v 1.15 2003/11/05 00:39:17 gbazin 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°"), N_("Rotate by 1800"),
62 N_("Rotate by 270°"), N_("Flip horizontally"), N_("Flip vertically") };
65 add_category_hint( N_("Miscellaneous"), NULL, VLC_FALSE );
66 add_string( "transform-type", "90", NULL,
67 TYPE_TEXT, TYPE_LONGTEXT, VLC_FALSE);
68 change_string_list( type_list, type_list_text, 0);
69 set_description( _("video transformation filter") );
70 set_capability( "video filter", 0 );
71 add_shortcut( "transform" );
72 set_callbacks( Create, Destroy );
75 /*****************************************************************************
76 * vout_sys_t: Transform video output method descriptor
77 *****************************************************************************
78 * This structure is part of the video output thread descriptor.
79 * It describes the Transform specific properties of an output thread.
80 *****************************************************************************/
84 vlc_bool_t b_rotation;
85 vout_thread_t *p_vout;
88 /*****************************************************************************
89 * Create: allocates Transform video thread output method
90 *****************************************************************************
91 * This function allocates and initializes a Transform vout method.
92 *****************************************************************************/
93 static int Create( vlc_object_t *p_this )
95 vout_thread_t *p_vout = (vout_thread_t *)p_this;
98 /* Allocate structure */
99 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
100 if( p_vout->p_sys == NULL )
102 msg_Err( p_vout, "out of memory" );
106 p_vout->pf_init = Init;
107 p_vout->pf_end = End;
108 p_vout->pf_manage = NULL;
109 p_vout->pf_render = Render;
110 p_vout->pf_display = NULL;
112 /* Look what method was requested */
113 psz_method = config_GetPsz( p_vout, "transform-type" );
115 if( psz_method == NULL )
117 msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
118 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
119 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
120 p_vout->p_sys->b_rotation = 1;
124 if( !strcmp( psz_method, "hflip" ) )
126 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
127 p_vout->p_sys->b_rotation = 0;
129 else if( !strcmp( psz_method, "vflip" ) )
131 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
132 p_vout->p_sys->b_rotation = 0;
134 else if( !strcmp( psz_method, "90" ) )
136 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
137 p_vout->p_sys->b_rotation = 1;
139 else if( !strcmp( psz_method, "180" ) )
141 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
142 p_vout->p_sys->b_rotation = 0;
144 else if( !strcmp( psz_method, "270" ) )
146 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
147 p_vout->p_sys->b_rotation = 1;
151 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
152 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
153 p_vout->p_sys->b_rotation = 1;
162 /*****************************************************************************
163 * Init: initialize Transform video thread output method
164 *****************************************************************************/
165 static int Init( vout_thread_t *p_vout )
170 I_OUTPUTPICTURES = 0;
172 /* Initialize the output structure */
173 p_vout->output.i_chroma = p_vout->render.i_chroma;
174 p_vout->output.i_width = p_vout->render.i_width;
175 p_vout->output.i_height = p_vout->render.i_height;
176 p_vout->output.i_aspect = p_vout->render.i_aspect;
178 /* Try to open the real video output */
179 msg_Dbg( p_vout, "spawning the real video output" );
181 if( p_vout->p_sys->b_rotation )
183 p_vout->p_sys->p_vout = vout_Create( p_vout,
184 p_vout->render.i_height, p_vout->render.i_width,
185 p_vout->render.i_chroma,
186 (uint64_t)VOUT_ASPECT_FACTOR
187 * (uint64_t)VOUT_ASPECT_FACTOR
188 / (uint64_t)p_vout->render.i_aspect );
192 p_vout->p_sys->p_vout = vout_Create( p_vout,
193 p_vout->render.i_width, p_vout->render.i_height,
194 p_vout->render.i_chroma, p_vout->render.i_aspect );
197 /* Everything failed */
198 if( p_vout->p_sys->p_vout == NULL )
200 msg_Err( p_vout, "cannot open vout, aborting" );
204 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
206 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
208 ADD_PARENT_CALLBACKS( SendEventsToChild );
213 /*****************************************************************************
214 * End: terminate Transform video thread output method
215 *****************************************************************************/
216 static void End( vout_thread_t *p_vout )
220 /* Free the fake output buffers we allocated */
221 for( i_index = I_OUTPUTPICTURES ; i_index ; )
224 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
228 /*****************************************************************************
229 * Destroy: destroy Transform video thread output method
230 *****************************************************************************
231 * Terminate an output method created by TransformCreateOutputMethod
232 *****************************************************************************/
233 static void Destroy( vlc_object_t *p_this )
235 vout_thread_t *p_vout = (vout_thread_t *)p_this;
237 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
238 vlc_object_detach( p_vout->p_sys->p_vout );
239 vout_Destroy( p_vout->p_sys->p_vout );
241 DEL_PARENT_CALLBACKS( SendEventsToChild );
243 free( p_vout->p_sys );
246 /*****************************************************************************
247 * Render: displays previously rendered output
248 *****************************************************************************
249 * This function send the currently rendered image to Transform image, waits
250 * until it is displayed and switch the two rendering buffers, preparing next
252 *****************************************************************************/
253 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
258 /* This is a new frame. Get a structure from the video_output. */
259 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
262 if( p_vout->b_die || p_vout->b_error )
266 msleep( VOUT_OUTMEM_SLEEP );
269 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
270 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
272 switch( p_vout->p_sys->i_mode )
274 case TRANSFORM_MODE_90:
275 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
277 int i_pitch = p_pic->p[i_index].i_pitch;
279 uint8_t *p_in = p_pic->p[i_index].p_pixels;
281 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
282 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
283 * p_outpic->p[i_index].i_pitch;
285 for( ; p_out < p_out_end ; )
289 p_out_end -= p_outpic->p[i_index].i_pitch
290 - p_outpic->p[i_index].i_visible_pitch;
291 p_line_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
293 for( ; p_in < p_line_end ; )
295 p_line_end -= i_pitch;
296 *(--p_out_end) = *p_line_end;
304 case TRANSFORM_MODE_180:
305 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
307 uint8_t *p_in = p_pic->p[i_index].p_pixels;
308 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
309 * p_pic->p[i_index].i_pitch;
311 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
313 for( ; p_in < p_in_end ; )
315 uint8_t *p_line_start = p_in_end
316 - p_pic->p[i_index].i_pitch;
317 p_in_end -= p_pic->p[i_index].i_pitch
318 - p_pic->p[i_index].i_visible_pitch;
320 for( ; p_line_start < p_in_end ; )
322 *p_out++ = *(--p_in_end);
325 p_out += p_outpic->p[i_index].i_pitch
326 - p_outpic->p[i_index].i_visible_pitch;
331 case TRANSFORM_MODE_270:
332 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
334 int i_pitch = p_pic->p[i_index].i_pitch;
336 uint8_t *p_in = p_pic->p[i_index].p_pixels;
338 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
339 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
340 * p_outpic->p[i_index].i_pitch;
342 for( ; p_out < p_out_end ; )
346 p_in_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
348 for( ; p_in < 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;
361 case TRANSFORM_MODE_HFLIP:
362 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
364 uint8_t *p_in = p_pic->p[i_index].p_pixels;
365 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
366 * p_pic->p[i_index].i_pitch;
368 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
370 for( ; p_in < p_in_end ; )
372 p_in_end -= p_pic->p[i_index].i_pitch;
373 p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
374 p_pic->p[i_index].i_visible_pitch );
375 p_out += p_pic->p[i_index].i_pitch;
380 case TRANSFORM_MODE_VFLIP:
381 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
383 uint8_t *p_in = p_pic->p[i_index].p_pixels;
384 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
385 * p_pic->p[i_index].i_pitch;
387 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
389 for( ; p_in < p_in_end ; )
391 uint8_t *p_line_end = p_in
392 + p_pic->p[i_index].i_visible_pitch;
394 for( ; p_in < p_line_end ; )
396 *p_out++ = *(--p_line_end);
399 p_in += p_pic->p[i_index].i_pitch;
408 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
410 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
413 /*****************************************************************************
414 * SendEvents: forward mouse and keyboard events to the parent p_vout
415 *****************************************************************************/
416 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
417 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
419 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
420 vlc_value_t sentval = newval;
422 /* Translate the mouse coordinates */
423 if( !strcmp( psz_var, "mouse-x" ) )
425 switch( p_vout->p_sys->i_mode )
427 case TRANSFORM_MODE_270:
428 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
430 case TRANSFORM_MODE_90:
431 var_Set( p_vout, "mouse-y", sentval );
434 case TRANSFORM_MODE_180:
435 case TRANSFORM_MODE_HFLIP:
436 sentval.i_int = p_vout->p_sys->p_vout->output.i_width
440 case TRANSFORM_MODE_VFLIP:
445 else if( !strcmp( psz_var, "mouse-y" ) )
447 switch( p_vout->p_sys->i_mode )
449 case TRANSFORM_MODE_90:
450 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
452 case TRANSFORM_MODE_270:
453 var_Set( p_vout, "mouse-x", sentval );
456 case TRANSFORM_MODE_180:
457 case TRANSFORM_MODE_VFLIP:
458 sentval.i_int = p_vout->p_sys->p_vout->output.i_height
462 case TRANSFORM_MODE_HFLIP:
468 var_Set( p_vout, psz_var, sentval );
473 /*****************************************************************************
474 * SendEventsToChild: forward events to the child/children vout
475 *****************************************************************************/
476 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
477 vlc_value_t oldval, vlc_value_t newval, void *p_data )
479 vout_thread_t *p_vout = (vout_thread_t *)p_this;
480 var_Set( p_vout->p_sys->p_vout, psz_var, newval );