1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: transform.c,v 1.9 2002/04/19 13:56:11 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 *****************************************************************************/
56 SET_DESCRIPTION( _("image transformation module") )
57 /* Capability score set to 0 because we don't want to be spawned
58 * as a video output unless explicitly requested to */
59 ADD_CAPABILITY( VOUT, 0 )
60 ADD_SHORTCUT( "transform" )
64 vout_getfunctions( &p_module->p_functions->vout );
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
70 /*****************************************************************************
71 * vout_sys_t: Transform video output method descriptor
72 *****************************************************************************
73 * This structure is part of the video output thread descriptor.
74 * It describes the Transform specific properties of an output thread.
75 *****************************************************************************/
76 typedef struct vout_sys_s
80 struct vout_thread_s *p_vout;
84 /*****************************************************************************
86 *****************************************************************************/
87 static int vout_Create ( struct vout_thread_s * );
88 static int vout_Init ( struct vout_thread_s * );
89 static void vout_End ( struct vout_thread_s * );
90 static void vout_Destroy ( struct vout_thread_s * );
91 static int vout_Manage ( struct vout_thread_s * );
92 static void vout_Render ( struct vout_thread_s *, struct picture_s * );
93 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
95 /*****************************************************************************
96 * Functions exported as capabilities. They are declared as static so that
97 * we don't pollute the namespace too much.
98 *****************************************************************************/
99 static void vout_getfunctions( function_list_t * p_function_list )
101 p_function_list->functions.vout.pf_create = vout_Create;
102 p_function_list->functions.vout.pf_init = vout_Init;
103 p_function_list->functions.vout.pf_end = vout_End;
104 p_function_list->functions.vout.pf_destroy = vout_Destroy;
105 p_function_list->functions.vout.pf_manage = vout_Manage;
106 p_function_list->functions.vout.pf_render = vout_Render;
107 p_function_list->functions.vout.pf_display = vout_Display;
110 /*****************************************************************************
111 * vout_Create: allocates Transform video thread output method
112 *****************************************************************************
113 * This function allocates and initializes a Transform vout method.
114 *****************************************************************************/
115 static int vout_Create( vout_thread_t *p_vout )
117 char *psz_method, *psz_method_tmp;
119 /* Allocate structure */
120 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
121 if( p_vout->p_sys == NULL )
123 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
127 /* Look what method was requested */
128 if( !(psz_method = psz_method_tmp
129 = config_GetPszVariable( "filter" )) )
131 intf_ErrMsg( "vout error: configuration variable %s empty",
136 while( *psz_method && *psz_method != ':' )
141 if( !strcmp( psz_method, ":hflip" ) )
143 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
144 p_vout->p_sys->b_rotation = 0;
146 else if( !strcmp( psz_method, ":vflip" ) )
148 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
149 p_vout->p_sys->b_rotation = 0;
151 else if( !strcmp( psz_method, ":90" ) )
153 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
154 p_vout->p_sys->b_rotation = 1;
156 else if( !strcmp( psz_method, ":180" ) )
158 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
159 p_vout->p_sys->b_rotation = 0;
161 else if( !strcmp( psz_method, ":270" ) )
163 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
164 p_vout->p_sys->b_rotation = 1;
168 intf_ErrMsg( "filter error: no valid transform mode provided, "
169 "using transform:90" );
170 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
171 p_vout->p_sys->b_rotation = 1;
174 free( psz_method_tmp );
179 /*****************************************************************************
180 * vout_Init: initialize Transform video thread output method
181 *****************************************************************************/
182 static int vout_Init( vout_thread_t *p_vout )
188 I_OUTPUTPICTURES = 0;
190 /* Initialize the output structure */
191 p_vout->output.i_chroma = p_vout->render.i_chroma;
192 p_vout->output.i_width = p_vout->render.i_width;
193 p_vout->output.i_height = p_vout->render.i_height;
194 p_vout->output.i_aspect = p_vout->render.i_aspect;
196 /* Try to open the real video output */
197 psz_filter = config_GetPszVariable( "filter" );
198 config_PutPszVariable( "filter", NULL );
200 intf_WarnMsg( 1, "filter: spawning the real video output" );
202 if( p_vout->p_sys->b_rotation )
204 p_vout->p_sys->p_vout =
205 vout_CreateThread( NULL,
206 p_vout->render.i_height, p_vout->render.i_width,
207 p_vout->render.i_chroma,
208 (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
209 / (u64)p_vout->render.i_aspect );
213 p_vout->p_sys->p_vout =
214 vout_CreateThread( NULL,
215 p_vout->render.i_width, p_vout->render.i_height,
216 p_vout->render.i_chroma, p_vout->render.i_aspect );
219 config_PutPszVariable( "filter", psz_filter );
220 if( psz_filter ) free( psz_filter );
222 /* Everything failed */
223 if( p_vout->p_sys->p_vout == NULL )
225 intf_ErrMsg( "filter error: can't open vout, aborting" );
229 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
234 /*****************************************************************************
235 * vout_End: terminate Transform video thread output method
236 *****************************************************************************/
237 static void vout_End( vout_thread_t *p_vout )
241 /* Free the fake output buffers we allocated */
242 for( i_index = I_OUTPUTPICTURES ; i_index ; )
245 free( PP_OUTPUTPICTURE[ i_index ]->p_data );
249 /*****************************************************************************
250 * vout_Destroy: destroy Transform video thread output method
251 *****************************************************************************
252 * Terminate an output method created by TransformCreateOutputMethod
253 *****************************************************************************/
254 static void vout_Destroy( vout_thread_t *p_vout )
256 vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
258 free( p_vout->p_sys );
261 /*****************************************************************************
262 * vout_Manage: handle Transform events
263 *****************************************************************************
264 * This function should be called regularly by video output thread. It manages
265 * console events. It returns a non null value on error.
266 *****************************************************************************/
267 static int vout_Manage( vout_thread_t *p_vout )
272 /*****************************************************************************
273 * vout_Render: displays previously rendered output
274 *****************************************************************************
275 * This function send the currently rendered image to Transform image, waits
276 * until it is displayed and switch the two rendering buffers, preparing next
278 *****************************************************************************/
279 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
284 /* This is a new frame. Get a structure from the video_output. */
285 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
288 if( p_vout->b_die || p_vout->b_error )
292 msleep( VOUT_OUTMEM_SLEEP );
295 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
296 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
298 switch( p_vout->p_sys->i_mode )
300 case TRANSFORM_MODE_90:
301 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
303 int i_pitch = p_pic->p[i_index].i_pitch;
305 u8 *p_in = p_pic->p[i_index].p_pixels;
307 u8 *p_out = p_outpic->p[i_index].p_pixels;
308 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
309 * p_outpic->p[i_index].i_pitch;
311 for( ; p_out < p_out_end ; )
315 p_line_end = p_in + p_pic->p[i_index].i_lines
316 * p_pic->p[i_index].i_pitch;
318 for( ; p_in < p_line_end ; )
320 p_line_end -= i_pitch;
321 *(--p_out_end) = *p_line_end;
329 case TRANSFORM_MODE_180:
330 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
332 u8 *p_in = p_pic->p[i_index].p_pixels;
333 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
334 * p_pic->p[i_index].i_pitch;
336 u8 *p_out = p_outpic->p[i_index].p_pixels;
338 for( ; p_in < p_in_end ; )
340 *p_out++ = *(--p_in_end);
345 case TRANSFORM_MODE_270:
346 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
348 int i_pitch = p_pic->p[i_index].i_pitch;
350 u8 *p_in = p_pic->p[i_index].p_pixels;
352 u8 *p_out = p_outpic->p[i_index].p_pixels;
353 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
354 * p_outpic->p[i_index].i_pitch;
356 for( ; p_out < p_out_end ; )
360 p_in_end = p_in + p_pic->p[i_index].i_lines
361 * p_pic->p[i_index].i_pitch;
363 for( ; p_in < p_in_end ; )
366 *p_out++ = *p_in_end;
374 case TRANSFORM_MODE_VFLIP:
375 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
377 u8 *p_in = p_pic->p[i_index].p_pixels;
378 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
379 * p_pic->p[i_index].i_pitch;
381 u8 *p_out = p_outpic->p[i_index].p_pixels;
383 for( ; p_in < p_in_end ; )
385 p_in_end -= p_pic->p[i_index].i_pitch;
386 FAST_MEMCPY( p_out, p_in_end, p_pic->p[i_index].i_pitch );
387 p_out += p_pic->p[i_index].i_pitch;
392 case TRANSFORM_MODE_HFLIP:
393 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
395 u8 *p_in = p_pic->p[i_index].p_pixels;
396 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
397 * p_pic->p[i_index].i_pitch;
399 u8 *p_out = p_outpic->p[i_index].p_pixels;
401 for( ; p_in < p_in_end ; )
403 u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
405 for( ; p_in < p_line_end ; )
407 *p_out++ = *(--p_line_end);
410 p_in += p_pic->p[i_index].i_pitch;
419 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
421 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
424 /*****************************************************************************
425 * vout_Display: displays previously rendered output
426 *****************************************************************************
427 * This function send the currently rendered image to Invert image, waits
428 * until it is displayed and switch the two rendering buffers, preparing next
430 *****************************************************************************/
431 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )