1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: transform.c,v 1.16 2002/07/20 18:01:42 sam 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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
34 #include "filter_common.h"
36 #define TRANSFORM_MODE_HFLIP 1
37 #define TRANSFORM_MODE_VFLIP 2
38 #define TRANSFORM_MODE_90 3
39 #define TRANSFORM_MODE_180 4
40 #define TRANSFORM_MODE_270 5
42 /*****************************************************************************
43 * Capabilities defined in the other files.
44 *****************************************************************************/
45 static void vout_getfunctions( function_list_t * p_function_list );
47 /*****************************************************************************
48 * Build configuration tree.
49 *****************************************************************************/
50 #define TYPE_TEXT N_("Transform type")
51 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
53 static char *type_list[] = { "90", "180", "270", "hflip", "vflip", NULL };
56 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
57 ADD_STRING_FROM_LIST("transform-type", "90", type_list, NULL, TYPE_TEXT, TYPE_LONGTEXT)
61 SET_DESCRIPTION( _("image transformation module") )
62 /* Capability score set to 0 because we don't want to be spawned
63 * as a video output unless explicitly requested to */
64 ADD_CAPABILITY( VOUT_FILTER, 0 )
65 ADD_SHORTCUT( "transform" )
69 vout_getfunctions( &p_module->p_functions->vout );
72 MODULE_DEACTIVATE_START
73 MODULE_DEACTIVATE_STOP
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 /*****************************************************************************
90 *****************************************************************************/
91 static int vout_Create ( vout_thread_t * );
92 static int vout_Init ( vout_thread_t * );
93 static void vout_End ( vout_thread_t * );
94 static void vout_Destroy ( vout_thread_t * );
95 static int vout_Manage ( vout_thread_t * );
96 static void vout_Render ( vout_thread_t *, picture_t * );
97 static void vout_Display ( vout_thread_t *, picture_t * );
99 /*****************************************************************************
100 * Functions exported as capabilities. They are declared as static so that
101 * we don't pollute the namespace too much.
102 *****************************************************************************/
103 static void vout_getfunctions( function_list_t * p_function_list )
105 p_function_list->functions.vout.pf_create = vout_Create;
106 p_function_list->functions.vout.pf_init = vout_Init;
107 p_function_list->functions.vout.pf_end = vout_End;
108 p_function_list->functions.vout.pf_destroy = vout_Destroy;
109 p_function_list->functions.vout.pf_manage = vout_Manage;
110 p_function_list->functions.vout.pf_render = vout_Render;
111 p_function_list->functions.vout.pf_display = vout_Display;
114 /*****************************************************************************
115 * vout_Create: allocates Transform video thread output method
116 *****************************************************************************
117 * This function allocates and initializes a Transform vout method.
118 *****************************************************************************/
119 static int vout_Create( vout_thread_t *p_vout )
123 /* Allocate structure */
124 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
125 if( p_vout->p_sys == NULL )
127 msg_Err( p_vout, "out of memory" );
131 /* Look what method was requested */
132 psz_method = config_GetPsz( p_vout, "transform-type" );
134 if( psz_method == NULL )
136 msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
137 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
138 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
139 p_vout->p_sys->b_rotation = 1;
143 if( !strcmp( psz_method, "hflip" ) )
145 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
146 p_vout->p_sys->b_rotation = 0;
148 else if( !strcmp( psz_method, "vflip" ) )
150 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
151 p_vout->p_sys->b_rotation = 0;
153 else if( !strcmp( psz_method, "90" ) )
155 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
156 p_vout->p_sys->b_rotation = 1;
158 else if( !strcmp( psz_method, "180" ) )
160 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
161 p_vout->p_sys->b_rotation = 0;
163 else if( !strcmp( psz_method, "270" ) )
165 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
166 p_vout->p_sys->b_rotation = 1;
170 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
171 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
172 p_vout->p_sys->b_rotation = 1;
181 /*****************************************************************************
182 * vout_Init: initialize Transform video thread output method
183 *****************************************************************************/
184 static int vout_Init( vout_thread_t *p_vout )
189 I_OUTPUTPICTURES = 0;
191 /* Initialize the output structure */
192 p_vout->output.i_chroma = p_vout->render.i_chroma;
193 p_vout->output.i_width = p_vout->render.i_width;
194 p_vout->output.i_height = p_vout->render.i_height;
195 p_vout->output.i_aspect = p_vout->render.i_aspect;
197 /* Try to open the real video output */
198 msg_Dbg( p_vout, "spawning the real video output" );
200 if( p_vout->p_sys->b_rotation )
202 p_vout->p_sys->p_vout =
203 vout_CreateThread( p_vout,
204 p_vout->render.i_height, p_vout->render.i_width,
205 p_vout->render.i_chroma,
206 (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
207 / (u64)p_vout->render.i_aspect );
211 p_vout->p_sys->p_vout =
212 vout_CreateThread( p_vout,
213 p_vout->render.i_width, p_vout->render.i_height,
214 p_vout->render.i_chroma, p_vout->render.i_aspect );
217 /* Everything failed */
218 if( p_vout->p_sys->p_vout == NULL )
220 msg_Err( p_vout, "cannot open vout, aborting" );
224 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
229 /*****************************************************************************
230 * vout_End: terminate Transform video thread output method
231 *****************************************************************************/
232 static void vout_End( vout_thread_t *p_vout )
236 /* Free the fake output buffers we allocated */
237 for( i_index = I_OUTPUTPICTURES ; i_index ; )
240 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
244 /*****************************************************************************
245 * vout_Destroy: destroy Transform video thread output method
246 *****************************************************************************
247 * Terminate an output method created by TransformCreateOutputMethod
248 *****************************************************************************/
249 static void vout_Destroy( vout_thread_t *p_vout )
251 vout_DestroyThread( p_vout->p_sys->p_vout );
253 free( p_vout->p_sys );
256 /*****************************************************************************
257 * vout_Manage: handle Transform events
258 *****************************************************************************
259 * This function should be called regularly by video output thread. It manages
260 * console events. It returns a non null value on error.
261 *****************************************************************************/
262 static int vout_Manage( vout_thread_t *p_vout )
267 /*****************************************************************************
268 * vout_Render: displays previously rendered output
269 *****************************************************************************
270 * This function send the currently rendered image to Transform image, waits
271 * until it is displayed and switch the two rendering buffers, preparing next
273 *****************************************************************************/
274 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
279 /* This is a new frame. Get a structure from the video_output. */
280 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
283 if( p_vout->b_die || p_vout->b_error )
287 msleep( VOUT_OUTMEM_SLEEP );
290 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
291 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
293 switch( p_vout->p_sys->i_mode )
295 case TRANSFORM_MODE_90:
296 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
298 int i_pitch = p_pic->p[i_index].i_pitch;
300 u8 *p_in = p_pic->p[i_index].p_pixels;
302 u8 *p_out = p_outpic->p[i_index].p_pixels;
303 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
304 * p_outpic->p[i_index].i_pitch;
306 for( ; p_out < p_out_end ; )
310 p_line_end = p_in + p_pic->p[i_index].i_lines
311 * p_pic->p[i_index].i_pitch;
313 for( ; p_in < p_line_end ; )
315 p_line_end -= i_pitch;
316 *(--p_out_end) = *p_line_end;
324 case TRANSFORM_MODE_180:
325 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
327 u8 *p_in = p_pic->p[i_index].p_pixels;
328 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
329 * p_pic->p[i_index].i_pitch;
331 u8 *p_out = p_outpic->p[i_index].p_pixels;
333 for( ; p_in < p_in_end ; )
335 *p_out++ = *(--p_in_end);
340 case TRANSFORM_MODE_270:
341 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
343 int i_pitch = p_pic->p[i_index].i_pitch;
345 u8 *p_in = p_pic->p[i_index].p_pixels;
347 u8 *p_out = p_outpic->p[i_index].p_pixels;
348 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
349 * p_outpic->p[i_index].i_pitch;
351 for( ; p_out < p_out_end ; )
355 p_in_end = p_in + p_pic->p[i_index].i_lines
356 * p_pic->p[i_index].i_pitch;
358 for( ; p_in < p_in_end ; )
361 *p_out++ = *p_in_end;
369 case TRANSFORM_MODE_VFLIP:
370 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
372 u8 *p_in = p_pic->p[i_index].p_pixels;
373 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
374 * p_pic->p[i_index].i_pitch;
376 u8 *p_out = p_outpic->p[i_index].p_pixels;
378 for( ; p_in < p_in_end ; )
380 p_in_end -= p_pic->p[i_index].i_pitch;
381 p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
382 p_pic->p[i_index].i_pitch );
383 p_out += p_pic->p[i_index].i_pitch;
388 case TRANSFORM_MODE_HFLIP:
389 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
391 u8 *p_in = p_pic->p[i_index].p_pixels;
392 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
393 * p_pic->p[i_index].i_pitch;
395 u8 *p_out = p_outpic->p[i_index].p_pixels;
397 for( ; p_in < p_in_end ; )
399 u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
401 for( ; p_in < p_line_end ; )
403 *p_out++ = *(--p_line_end);
406 p_in += p_pic->p[i_index].i_pitch;
415 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
417 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
420 /*****************************************************************************
421 * vout_Display: displays previously rendered output
422 *****************************************************************************
423 * This function send the currently rendered image to Invert image, waits
424 * until it is displayed and switch the two rendering buffers, preparing next
426 *****************************************************************************/
427 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )