1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: transform.c,v 1.3 2002/01/02 14:37: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() */
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_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->pf_probe = vout_Probe;
102 p_function_list->functions.vout.pf_create = vout_Create;
103 p_function_list->functions.vout.pf_init = vout_Init;
104 p_function_list->functions.vout.pf_end = vout_End;
105 p_function_list->functions.vout.pf_destroy = vout_Destroy;
106 p_function_list->functions.vout.pf_manage = vout_Manage;
107 p_function_list->functions.vout.pf_display = vout_Display;
108 p_function_list->functions.vout.pf_setpalette = NULL;
111 /*****************************************************************************
112 * intf_Probe: return a score
113 *****************************************************************************/
114 static int vout_Probe( probedata_t *p_data )
119 /*****************************************************************************
120 * vout_Create: allocates Transform video thread output method
121 *****************************************************************************
122 * This function allocates and initializes a Transform vout method.
123 *****************************************************************************/
124 static int vout_Create( vout_thread_t *p_vout )
128 /* Allocate structure */
129 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
130 if( p_vout->p_sys == NULL )
132 intf_ErrMsg( "error: %s", strerror(ENOMEM) );
136 /* Look what method was requested */
137 psz_method = main_GetPszVariable( VOUT_FILTER_VAR, "" );
139 while( *psz_method && *psz_method != ':' )
144 if( !strcmp( psz_method, ":hflip" ) )
146 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
147 p_vout->p_sys->b_rotation = 0;
149 else if( !strcmp( psz_method, ":vflip" ) )
151 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
152 p_vout->p_sys->b_rotation = 0;
154 else if( !strcmp( psz_method, ":90" ) )
156 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
157 p_vout->p_sys->b_rotation = 1;
159 else if( !strcmp( psz_method, ":180" ) )
161 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
162 p_vout->p_sys->b_rotation = 0;
164 else if( !strcmp( psz_method, ":270" ) )
166 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
167 p_vout->p_sys->b_rotation = 1;
171 intf_ErrMsg( "filter error: no valid transform mode provided, "
172 "using transform:90" );
173 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
174 p_vout->p_sys->b_rotation = 1;
180 /*****************************************************************************
181 * vout_Init: initialize Transform video thread output method
182 *****************************************************************************/
183 static int vout_Init( vout_thread_t *p_vout )
189 I_OUTPUTPICTURES = 0;
191 /* Initialize the output structure */
192 switch( p_vout->render.i_chroma )
199 p_vout->output.i_chroma = p_vout->render.i_chroma;
200 p_vout->output.i_width = p_vout->render.i_width;
201 p_vout->output.i_height = p_vout->render.i_height;
202 p_vout->output.i_aspect = p_vout->render.i_aspect;
206 return( 0 ); /* unknown chroma */
210 /* Try to open the real video output */
211 psz_filter = main_GetPszVariable( VOUT_FILTER_VAR, "" );
212 main_PutPszVariable( VOUT_FILTER_VAR, "" );
214 intf_WarnMsg( 1, "filter: spawning the real video output" );
216 if( p_vout->p_sys->b_rotation )
218 p_vout->p_sys->p_vout =
219 vout_CreateThread( NULL,
220 p_vout->render.i_height, p_vout->render.i_width,
221 p_vout->render.i_chroma,
222 (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
223 / (u64)p_vout->render.i_aspect );
227 p_vout->p_sys->p_vout =
228 vout_CreateThread( NULL,
229 p_vout->render.i_width, p_vout->render.i_height,
230 p_vout->render.i_chroma, p_vout->render.i_aspect );
233 /* Everything failed */
234 if( p_vout->p_sys->p_vout == NULL )
236 intf_ErrMsg( "filter error: can't open vout, aborting" );
240 main_PutPszVariable( VOUT_FILTER_VAR, psz_filter );
242 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
247 /*****************************************************************************
248 * vout_End: terminate Transform video thread output method
249 *****************************************************************************/
250 static void vout_End( vout_thread_t *p_vout )
254 /* Free the fake output buffers we allocated */
255 for( i_index = I_OUTPUTPICTURES ; i_index ; )
258 free( PP_OUTPUTPICTURE[ i_index ]->planes[ 0 ].p_data );
262 /*****************************************************************************
263 * vout_Destroy: destroy Transform video thread output method
264 *****************************************************************************
265 * Terminate an output method created by TransformCreateOutputMethod
266 *****************************************************************************/
267 static void vout_Destroy( vout_thread_t *p_vout )
269 vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
271 free( p_vout->p_sys );
274 /*****************************************************************************
275 * vout_Manage: handle Transform events
276 *****************************************************************************
277 * This function should be called regularly by video output thread. It manages
278 * console events. It returns a non null value on error.
279 *****************************************************************************/
280 static int vout_Manage( vout_thread_t *p_vout )
285 /*****************************************************************************
286 * vout_Display: displays previously rendered output
287 *****************************************************************************
288 * This function send the currently rendered image to Transform image, waits
289 * until it is displayed and switch the two rendering buffers, preparing next
291 *****************************************************************************/
292 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
297 /* This is a new frame. Get a structure from the video_output. */
298 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
301 if( p_vout->b_die || p_vout->b_error )
305 msleep( VOUT_OUTMEM_SLEEP );
308 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, mdate() + 50000 );
309 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
311 switch( p_vout->p_sys->i_mode )
313 case TRANSFORM_MODE_90:
314 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
316 int i_line_bytes = p_pic->planes[ i_index ].i_line_bytes;
318 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
320 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
321 pixel_data_t *p_out_end = p_out
322 + p_outpic->planes[ i_index ].i_bytes;
324 for( ; p_out < p_out_end ; )
326 pixel_data_t *p_line_end;
328 p_line_end = p_in + p_pic->planes[ i_index ].i_bytes;
330 for( ; p_in < p_line_end ; )
332 p_line_end -= i_line_bytes;
333 *(--p_out_end) = *p_line_end;
341 case TRANSFORM_MODE_180:
342 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
344 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
345 pixel_data_t *p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
347 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
349 for( ; p_in < p_in_end ; )
351 *p_out++ = *(--p_in_end);
356 case TRANSFORM_MODE_270:
357 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
359 int i_line_bytes = p_pic->planes[ i_index ].i_line_bytes;
361 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
363 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
364 pixel_data_t *p_out_end = p_out
365 + p_outpic->planes[ i_index ].i_bytes;
367 for( ; p_out < p_out_end ; )
369 pixel_data_t *p_in_end;
371 p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
373 for( ; p_in < p_in_end ; )
375 p_in_end -= i_line_bytes;
376 *p_out++ = *p_in_end;
384 case TRANSFORM_MODE_VFLIP:
385 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
387 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
388 pixel_data_t *p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
390 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
392 for( ; p_in < p_in_end ; )
394 p_in_end -= p_pic->planes[ i_index ].i_line_bytes;
395 FAST_MEMCPY( p_out, p_in_end,
396 p_pic->planes[ i_index ].i_line_bytes );
397 p_out += p_pic->planes[ i_index ].i_line_bytes;
402 case TRANSFORM_MODE_HFLIP:
403 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
405 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
406 pixel_data_t *p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
408 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
410 for( ; p_in < p_in_end ; )
412 pixel_data_t *p_line_end = p_in
413 + p_pic->planes[ i_index ].i_line_bytes;
415 for( ; p_in < p_line_end ; )
417 *p_out++ = *(--p_line_end);
420 p_in += p_pic->planes[ i_index ].i_line_bytes;
429 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
431 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );