1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: transform.c,v 1.11 2002/05/27 19:35:41 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() */
31 #include <videolan/vlc.h>
34 #include "video_output.h"
36 #include "filter_common.h"
38 #define TRANSFORM_MODE_HFLIP 1
39 #define TRANSFORM_MODE_VFLIP 2
40 #define TRANSFORM_MODE_90 3
41 #define TRANSFORM_MODE_180 4
42 #define TRANSFORM_MODE_270 5
44 /*****************************************************************************
45 * Capabilities defined in the other files.
46 *****************************************************************************/
47 static void vout_getfunctions( function_list_t * p_function_list );
49 /*****************************************************************************
50 * Build configuration tree.
51 *****************************************************************************/
53 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
54 ADD_STRING("transform_type", "90", NULL, N_("Transform type"),
55 N_("One of '90', '180', '270', 'hflip' and 'vflip'"))
59 SET_DESCRIPTION( _("image transformation module") )
60 /* Capability score set to 0 because we don't want to be spawned
61 * as a video output unless explicitly requested to */
62 ADD_CAPABILITY( VOUT, 0 )
63 ADD_SHORTCUT( "transform" )
67 vout_getfunctions( &p_module->p_functions->vout );
70 MODULE_DEACTIVATE_START
71 MODULE_DEACTIVATE_STOP
73 /*****************************************************************************
74 * vout_sys_t: Transform video output method descriptor
75 *****************************************************************************
76 * This structure is part of the video output thread descriptor.
77 * It describes the Transform specific properties of an output thread.
78 *****************************************************************************/
79 typedef struct vout_sys_s
83 struct vout_thread_s *p_vout;
87 /*****************************************************************************
89 *****************************************************************************/
90 static int vout_Create ( struct vout_thread_s * );
91 static int vout_Init ( struct vout_thread_s * );
92 static void vout_End ( struct vout_thread_s * );
93 static void vout_Destroy ( struct vout_thread_s * );
94 static int vout_Manage ( struct vout_thread_s * );
95 static void vout_Render ( struct vout_thread_s *, struct picture_s * );
96 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
98 /*****************************************************************************
99 * Functions exported as capabilities. They are declared as static so that
100 * we don't pollute the namespace too much.
101 *****************************************************************************/
102 static void vout_getfunctions( function_list_t * p_function_list )
104 p_function_list->functions.vout.pf_create = vout_Create;
105 p_function_list->functions.vout.pf_init = vout_Init;
106 p_function_list->functions.vout.pf_end = vout_End;
107 p_function_list->functions.vout.pf_destroy = vout_Destroy;
108 p_function_list->functions.vout.pf_manage = vout_Manage;
109 p_function_list->functions.vout.pf_render = vout_Render;
110 p_function_list->functions.vout.pf_display = vout_Display;
113 /*****************************************************************************
114 * vout_Create: allocates Transform video thread output method
115 *****************************************************************************
116 * This function allocates and initializes a Transform vout method.
117 *****************************************************************************/
118 static int vout_Create( vout_thread_t *p_vout )
120 char *psz_method, *psz_method_tmp;
122 /* Allocate structure */
123 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
124 if( p_vout->p_sys == NULL )
126 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
130 /* Look what method was requested */
131 if( !(psz_method = psz_method_tmp
132 = config_GetPszVariable( "transform_type" )) )
134 intf_ErrMsg( "vout error: configuration variable %s empty",
136 intf_ErrMsg( "filter error: no valid transform mode provided, "
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 intf_ErrMsg( "filter error: no valid transform mode provided, "
172 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
173 p_vout->p_sys->b_rotation = 1;
177 free( psz_method_tmp );
182 /*****************************************************************************
183 * vout_Init: initialize Transform video thread output method
184 *****************************************************************************/
185 static int vout_Init( vout_thread_t *p_vout )
191 I_OUTPUTPICTURES = 0;
193 /* Initialize the output structure */
194 p_vout->output.i_chroma = p_vout->render.i_chroma;
195 p_vout->output.i_width = p_vout->render.i_width;
196 p_vout->output.i_height = p_vout->render.i_height;
197 p_vout->output.i_aspect = p_vout->render.i_aspect;
199 /* Try to open the real video output */
200 psz_filter = config_GetPszVariable( "filter" );
201 config_PutPszVariable( "filter", NULL );
203 intf_WarnMsg( 1, "filter: spawning the real video output" );
205 if( p_vout->p_sys->b_rotation )
207 p_vout->p_sys->p_vout =
208 vout_CreateThread( NULL,
209 p_vout->render.i_height, p_vout->render.i_width,
210 p_vout->render.i_chroma,
211 (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
212 / (u64)p_vout->render.i_aspect );
216 p_vout->p_sys->p_vout =
217 vout_CreateThread( NULL,
218 p_vout->render.i_width, p_vout->render.i_height,
219 p_vout->render.i_chroma, p_vout->render.i_aspect );
222 config_PutPszVariable( "filter", psz_filter );
223 if( psz_filter ) free( psz_filter );
225 /* Everything failed */
226 if( p_vout->p_sys->p_vout == NULL )
228 intf_ErrMsg( "filter error: can't open vout, aborting" );
232 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
237 /*****************************************************************************
238 * vout_End: terminate Transform video thread output method
239 *****************************************************************************/
240 static void vout_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 * vout_Destroy: destroy Transform video thread output method
254 *****************************************************************************
255 * Terminate an output method created by TransformCreateOutputMethod
256 *****************************************************************************/
257 static void vout_Destroy( vout_thread_t *p_vout )
259 vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
261 free( p_vout->p_sys );
264 /*****************************************************************************
265 * vout_Manage: handle Transform events
266 *****************************************************************************
267 * This function should be called regularly by video output thread. It manages
268 * console events. It returns a non null value on error.
269 *****************************************************************************/
270 static int vout_Manage( vout_thread_t *p_vout )
275 /*****************************************************************************
276 * vout_Render: displays previously rendered output
277 *****************************************************************************
278 * This function send the currently rendered image to Transform image, waits
279 * until it is displayed and switch the two rendering buffers, preparing next
281 *****************************************************************************/
282 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
287 /* This is a new frame. Get a structure from the video_output. */
288 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
291 if( p_vout->b_die || p_vout->b_error )
295 msleep( VOUT_OUTMEM_SLEEP );
298 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
299 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
301 switch( p_vout->p_sys->i_mode )
303 case TRANSFORM_MODE_90:
304 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
306 int i_pitch = p_pic->p[i_index].i_pitch;
308 u8 *p_in = p_pic->p[i_index].p_pixels;
310 u8 *p_out = p_outpic->p[i_index].p_pixels;
311 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
312 * p_outpic->p[i_index].i_pitch;
314 for( ; p_out < p_out_end ; )
318 p_line_end = p_in + p_pic->p[i_index].i_lines
319 * p_pic->p[i_index].i_pitch;
321 for( ; p_in < p_line_end ; )
323 p_line_end -= i_pitch;
324 *(--p_out_end) = *p_line_end;
332 case TRANSFORM_MODE_180:
333 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
335 u8 *p_in = p_pic->p[i_index].p_pixels;
336 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
337 * p_pic->p[i_index].i_pitch;
339 u8 *p_out = p_outpic->p[i_index].p_pixels;
341 for( ; p_in < p_in_end ; )
343 *p_out++ = *(--p_in_end);
348 case TRANSFORM_MODE_270:
349 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
351 int i_pitch = p_pic->p[i_index].i_pitch;
353 u8 *p_in = p_pic->p[i_index].p_pixels;
355 u8 *p_out = p_outpic->p[i_index].p_pixels;
356 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
357 * p_outpic->p[i_index].i_pitch;
359 for( ; p_out < p_out_end ; )
363 p_in_end = p_in + p_pic->p[i_index].i_lines
364 * p_pic->p[i_index].i_pitch;
366 for( ; p_in < p_in_end ; )
369 *p_out++ = *p_in_end;
377 case TRANSFORM_MODE_VFLIP:
378 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
380 u8 *p_in = p_pic->p[i_index].p_pixels;
381 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
382 * p_pic->p[i_index].i_pitch;
384 u8 *p_out = p_outpic->p[i_index].p_pixels;
386 for( ; p_in < p_in_end ; )
388 p_in_end -= p_pic->p[i_index].i_pitch;
389 FAST_MEMCPY( p_out, p_in_end, p_pic->p[i_index].i_pitch );
390 p_out += p_pic->p[i_index].i_pitch;
395 case TRANSFORM_MODE_HFLIP:
396 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
398 u8 *p_in = p_pic->p[i_index].p_pixels;
399 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
400 * p_pic->p[i_index].i_pitch;
402 u8 *p_out = p_outpic->p[i_index].p_pixels;
404 for( ; p_in < p_in_end ; )
406 u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
408 for( ; p_in < p_line_end ; )
410 *p_out++ = *(--p_line_end);
413 p_in += p_pic->p[i_index].i_pitch;
422 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
424 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
427 /*****************************************************************************
428 * vout_Display: displays previously rendered output
429 *****************************************************************************
430 * This function send the currently rendered image to Invert image, waits
431 * until it is displayed and switch the two rendering buffers, preparing next
433 *****************************************************************************/
434 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )