1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: transform.c,v 1.1 2002/08/04 17:23:43 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 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
48 static int Init ( vout_thread_t * );
49 static void End ( vout_thread_t * );
50 static void Render ( vout_thread_t *, picture_t * );
52 /*****************************************************************************
54 *****************************************************************************/
55 #define TYPE_TEXT N_("Transform type")
56 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
58 static char *type_list[] = { "90", "180", "270", "hflip", "vflip", NULL };
61 add_category_hint( N_("Miscellaneous"), NULL );
62 add_string_from_list( "transform-type", "90", type_list, NULL,
63 TYPE_TEXT, TYPE_LONGTEXT);
64 set_description( _("image transformation module") );
65 set_capability( "video filter", 0 );
66 add_shortcut( "transform" );
67 set_callbacks( Create, Destroy );
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 *****************************************************************************/
79 vlc_bool_t b_rotation;
80 vout_thread_t *p_vout;
83 /*****************************************************************************
84 * Create: allocates Transform video thread output method
85 *****************************************************************************
86 * This function allocates and initializes a Transform vout method.
87 *****************************************************************************/
88 static int Create( vlc_object_t *p_this )
90 vout_thread_t *p_vout = (vout_thread_t *)p_this;
93 /* Allocate structure */
94 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
95 if( p_vout->p_sys == NULL )
97 msg_Err( p_vout, "out of memory" );
101 p_vout->pf_init = Init;
102 p_vout->pf_end = End;
103 p_vout->pf_manage = NULL;
104 p_vout->pf_render = Render;
105 p_vout->pf_display = NULL;
107 /* Look what method was requested */
108 psz_method = config_GetPsz( p_vout, "transform-type" );
110 if( psz_method == NULL )
112 msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
113 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
114 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
115 p_vout->p_sys->b_rotation = 1;
119 if( !strcmp( psz_method, "hflip" ) )
121 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
122 p_vout->p_sys->b_rotation = 0;
124 else if( !strcmp( psz_method, "vflip" ) )
126 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
127 p_vout->p_sys->b_rotation = 0;
129 else if( !strcmp( psz_method, "90" ) )
131 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
132 p_vout->p_sys->b_rotation = 1;
134 else if( !strcmp( psz_method, "180" ) )
136 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
137 p_vout->p_sys->b_rotation = 0;
139 else if( !strcmp( psz_method, "270" ) )
141 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
142 p_vout->p_sys->b_rotation = 1;
146 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
147 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
148 p_vout->p_sys->b_rotation = 1;
157 /*****************************************************************************
158 * Init: initialize Transform video thread output method
159 *****************************************************************************/
160 static int Init( vout_thread_t *p_vout )
165 I_OUTPUTPICTURES = 0;
167 /* Initialize the output structure */
168 p_vout->output.i_chroma = p_vout->render.i_chroma;
169 p_vout->output.i_width = p_vout->render.i_width;
170 p_vout->output.i_height = p_vout->render.i_height;
171 p_vout->output.i_aspect = p_vout->render.i_aspect;
173 /* Try to open the real video output */
174 msg_Dbg( p_vout, "spawning the real video output" );
176 if( p_vout->p_sys->b_rotation )
178 p_vout->p_sys->p_vout =
179 vout_CreateThread( p_vout,
180 p_vout->render.i_height, p_vout->render.i_width,
181 p_vout->render.i_chroma,
182 (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
183 / (u64)p_vout->render.i_aspect );
187 p_vout->p_sys->p_vout =
188 vout_CreateThread( p_vout,
189 p_vout->render.i_width, p_vout->render.i_height,
190 p_vout->render.i_chroma, p_vout->render.i_aspect );
193 /* Everything failed */
194 if( p_vout->p_sys->p_vout == NULL )
196 msg_Err( p_vout, "cannot open vout, aborting" );
200 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
205 /*****************************************************************************
206 * End: terminate Transform video thread output method
207 *****************************************************************************/
208 static void End( vout_thread_t *p_vout )
212 /* Free the fake output buffers we allocated */
213 for( i_index = I_OUTPUTPICTURES ; i_index ; )
216 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
220 /*****************************************************************************
221 * Destroy: destroy Transform video thread output method
222 *****************************************************************************
223 * Terminate an output method created by TransformCreateOutputMethod
224 *****************************************************************************/
225 static void Destroy( vlc_object_t *p_this )
227 vout_thread_t *p_vout = (vout_thread_t *)p_this;
229 vout_DestroyThread( p_vout->p_sys->p_vout );
231 free( p_vout->p_sys );
234 /*****************************************************************************
235 * Render: displays previously rendered output
236 *****************************************************************************
237 * This function send the currently rendered image to Transform image, waits
238 * until it is displayed and switch the two rendering buffers, preparing next
240 *****************************************************************************/
241 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
246 /* This is a new frame. Get a structure from the video_output. */
247 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
250 if( p_vout->b_die || p_vout->b_error )
254 msleep( VOUT_OUTMEM_SLEEP );
257 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
258 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
260 switch( p_vout->p_sys->i_mode )
262 case TRANSFORM_MODE_90:
263 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
265 int i_pitch = p_pic->p[i_index].i_pitch;
267 u8 *p_in = p_pic->p[i_index].p_pixels;
269 u8 *p_out = p_outpic->p[i_index].p_pixels;
270 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
271 * p_outpic->p[i_index].i_pitch;
273 for( ; p_out < p_out_end ; )
277 p_line_end = p_in + p_pic->p[i_index].i_lines
278 * p_pic->p[i_index].i_pitch;
280 for( ; p_in < p_line_end ; )
282 p_line_end -= i_pitch;
283 *(--p_out_end) = *p_line_end;
291 case TRANSFORM_MODE_180:
292 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
294 u8 *p_in = p_pic->p[i_index].p_pixels;
295 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
296 * p_pic->p[i_index].i_pitch;
298 u8 *p_out = p_outpic->p[i_index].p_pixels;
300 for( ; p_in < p_in_end ; )
302 *p_out++ = *(--p_in_end);
307 case TRANSFORM_MODE_270:
308 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
310 int i_pitch = p_pic->p[i_index].i_pitch;
312 u8 *p_in = p_pic->p[i_index].p_pixels;
314 u8 *p_out = p_outpic->p[i_index].p_pixels;
315 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
316 * p_outpic->p[i_index].i_pitch;
318 for( ; p_out < p_out_end ; )
322 p_in_end = p_in + p_pic->p[i_index].i_lines
323 * p_pic->p[i_index].i_pitch;
325 for( ; p_in < p_in_end ; )
328 *p_out++ = *p_in_end;
336 case TRANSFORM_MODE_VFLIP:
337 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
339 u8 *p_in = p_pic->p[i_index].p_pixels;
340 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
341 * p_pic->p[i_index].i_pitch;
343 u8 *p_out = p_outpic->p[i_index].p_pixels;
345 for( ; p_in < p_in_end ; )
347 p_in_end -= p_pic->p[i_index].i_pitch;
348 p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
349 p_pic->p[i_index].i_pitch );
350 p_out += p_pic->p[i_index].i_pitch;
355 case TRANSFORM_MODE_HFLIP:
356 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
358 u8 *p_in = p_pic->p[i_index].p_pixels;
359 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
360 * p_pic->p[i_index].i_pitch;
362 u8 *p_out = p_outpic->p[i_index].p_pixels;
364 for( ; p_in < p_in_end ; )
366 u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
368 for( ; p_in < p_line_end ; )
370 *p_out++ = *(--p_line_end);
373 p_in += p_pic->p[i_index].i_pitch;
382 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
384 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );