1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: transform.c,v 1.4 2002/01/04 14:01:34 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_Probe ( probedata_t *p_data );
88 static int vout_Create ( struct vout_thread_s * );
89 static int vout_Init ( struct vout_thread_s * );
90 static void vout_End ( struct vout_thread_s * );
91 static void vout_Destroy ( struct vout_thread_s * );
92 static int vout_Manage ( struct vout_thread_s * );
93 static void vout_Render ( struct vout_thread_s *, struct picture_s * );
94 static void vout_Display ( struct vout_thread_s *, struct picture_s * );
96 /*****************************************************************************
97 * Functions exported as capabilities. They are declared as static so that
98 * we don't pollute the namespace too much.
99 *****************************************************************************/
100 static void vout_getfunctions( function_list_t * p_function_list )
102 p_function_list->pf_probe = vout_Probe;
103 p_function_list->functions.vout.pf_create = vout_Create;
104 p_function_list->functions.vout.pf_init = vout_Init;
105 p_function_list->functions.vout.pf_end = vout_End;
106 p_function_list->functions.vout.pf_destroy = vout_Destroy;
107 p_function_list->functions.vout.pf_manage = vout_Manage;
108 p_function_list->functions.vout.pf_render = vout_Render;
109 p_function_list->functions.vout.pf_display = vout_Display;
110 p_function_list->functions.vout.pf_setpalette = NULL;
113 /*****************************************************************************
114 * intf_Probe: return a score
115 *****************************************************************************/
116 static int vout_Probe( probedata_t *p_data )
121 /*****************************************************************************
122 * vout_Create: allocates Transform video thread output method
123 *****************************************************************************
124 * This function allocates and initializes a Transform vout method.
125 *****************************************************************************/
126 static int vout_Create( vout_thread_t *p_vout )
130 /* Allocate structure */
131 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
132 if( p_vout->p_sys == NULL )
134 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
138 /* Look what method was requested */
139 psz_method = main_GetPszVariable( VOUT_FILTER_VAR, "" );
141 while( *psz_method && *psz_method != ':' )
146 if( !strcmp( psz_method, ":hflip" ) )
148 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
149 p_vout->p_sys->b_rotation = 0;
151 else if( !strcmp( psz_method, ":vflip" ) )
153 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
154 p_vout->p_sys->b_rotation = 0;
156 else if( !strcmp( psz_method, ":90" ) )
158 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
159 p_vout->p_sys->b_rotation = 1;
161 else if( !strcmp( psz_method, ":180" ) )
163 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
164 p_vout->p_sys->b_rotation = 0;
166 else if( !strcmp( psz_method, ":270" ) )
168 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
169 p_vout->p_sys->b_rotation = 1;
173 intf_ErrMsg( "filter error: no valid transform mode provided, "
174 "using transform:90" );
175 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
176 p_vout->p_sys->b_rotation = 1;
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 = main_GetPszVariable( VOUT_FILTER_VAR, "" );
201 main_PutPszVariable( VOUT_FILTER_VAR, "" );
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 /* Everything failed */
223 if( p_vout->p_sys->p_vout == NULL )
225 intf_ErrMsg( "filter error: can't open vout, aborting" );
229 main_PutPszVariable( VOUT_FILTER_VAR, psz_filter );
231 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
236 /*****************************************************************************
237 * vout_End: terminate Transform video thread output method
238 *****************************************************************************/
239 static void vout_End( vout_thread_t *p_vout )
243 /* Free the fake output buffers we allocated */
244 for( i_index = I_OUTPUTPICTURES ; i_index ; )
247 free( PP_OUTPUTPICTURE[ i_index ]->p_data );
251 /*****************************************************************************
252 * vout_Destroy: destroy Transform video thread output method
253 *****************************************************************************
254 * Terminate an output method created by TransformCreateOutputMethod
255 *****************************************************************************/
256 static void vout_Destroy( vout_thread_t *p_vout )
258 vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
260 free( p_vout->p_sys );
263 /*****************************************************************************
264 * vout_Manage: handle Transform events
265 *****************************************************************************
266 * This function should be called regularly by video output thread. It manages
267 * console events. It returns a non null value on error.
268 *****************************************************************************/
269 static int vout_Manage( vout_thread_t *p_vout )
274 /*****************************************************************************
275 * vout_Render: displays previously rendered output
276 *****************************************************************************
277 * This function send the currently rendered image to Transform image, waits
278 * until it is displayed and switch the two rendering buffers, preparing next
280 *****************************************************************************/
281 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
286 /* This is a new frame. Get a structure from the video_output. */
287 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
290 if( p_vout->b_die || p_vout->b_error )
294 msleep( VOUT_OUTMEM_SLEEP );
297 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
298 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
300 switch( p_vout->p_sys->i_mode )
302 case TRANSFORM_MODE_90:
303 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
305 int i_pitch = p_pic->p[i_index].i_pitch;
307 u8 *p_in = p_pic->p[i_index].p_pixels;
309 u8 *p_out = p_outpic->p[i_index].p_pixels;
310 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
311 * p_outpic->p[i_index].i_pitch;
313 for( ; p_out < p_out_end ; )
317 p_line_end = p_in + p_pic->p[i_index].i_lines
318 * p_pic->p[i_index].i_pitch;
320 for( ; p_in < p_line_end ; )
322 p_line_end -= i_pitch;
323 *(--p_out_end) = *p_line_end;
331 case TRANSFORM_MODE_180:
332 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
334 u8 *p_in = p_pic->p[i_index].p_pixels;
335 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
336 * p_pic->p[i_index].i_pitch;
338 u8 *p_out = p_outpic->p[i_index].p_pixels;
340 for( ; p_in < p_in_end ; )
342 *p_out++ = *(--p_in_end);
347 case TRANSFORM_MODE_270:
348 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
350 int i_pitch = p_pic->p[i_index].i_pitch;
352 u8 *p_in = p_pic->p[i_index].p_pixels;
354 u8 *p_out = p_outpic->p[i_index].p_pixels;
355 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
356 * p_outpic->p[i_index].i_pitch;
358 for( ; p_out < p_out_end ; )
362 p_in_end = p_in + p_pic->p[i_index].i_lines
363 * p_pic->p[i_index].i_pitch;
365 for( ; p_in < p_in_end ; )
368 *p_out++ = *p_in_end;
376 case TRANSFORM_MODE_VFLIP:
377 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
379 u8 *p_in = p_pic->p[i_index].p_pixels;
380 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
381 * p_pic->p[i_index].i_pitch;
383 u8 *p_out = p_outpic->p[i_index].p_pixels;
385 for( ; p_in < p_in_end ; )
387 p_in_end -= p_pic->p[i_index].i_pitch;
388 FAST_MEMCPY( p_out, p_in_end, p_pic->p[i_index].i_pitch );
389 p_out += p_pic->p[i_index].i_pitch;
394 case TRANSFORM_MODE_HFLIP:
395 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
397 u8 *p_in = p_pic->p[i_index].p_pixels;
398 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
399 * p_pic->p[i_index].i_pitch;
401 u8 *p_out = p_outpic->p[i_index].p_pixels;
403 for( ; p_in < p_in_end ; )
405 u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
407 for( ; p_in < p_line_end ; )
409 *p_out++ = *(--p_line_end);
412 p_in += p_pic->p[i_index].i_pitch;
421 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
423 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
426 /*****************************************************************************
427 * vout_Display: displays previously rendered output
428 *****************************************************************************
429 * This function send the currently rendered image to Invert image, waits
430 * until it is displayed and switch the two rendering buffers, preparing next
432 *****************************************************************************/
433 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )