1 /*****************************************************************************
2 * transform.c : transform image plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: transform.c,v 1.6 2003/01/09 17:47:05 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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
33 #include "filter_common.h"
35 #define TRANSFORM_MODE_HFLIP 1
36 #define TRANSFORM_MODE_VFLIP 2
37 #define TRANSFORM_MODE_90 3
38 #define TRANSFORM_MODE_180 4
39 #define TRANSFORM_MODE_270 5
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static int Init ( vout_thread_t * );
48 static void End ( vout_thread_t * );
49 static void Render ( vout_thread_t *, picture_t * );
51 /*****************************************************************************
53 *****************************************************************************/
54 #define TYPE_TEXT N_("transform type")
55 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
57 static char *type_list[] = { "90", "180", "270", "hflip", "vflip", NULL };
60 add_category_hint( N_("Miscellaneous"), NULL );
61 add_string_from_list( "transform-type", "90", type_list, NULL,
62 TYPE_TEXT, TYPE_LONGTEXT);
63 set_description( _("image transformation module") );
64 set_capability( "video filter", 0 );
65 add_shortcut( "transform" );
66 set_callbacks( Create, Destroy );
69 /*****************************************************************************
70 * vout_sys_t: Transform video output method descriptor
71 *****************************************************************************
72 * This structure is part of the video output thread descriptor.
73 * It describes the Transform specific properties of an output thread.
74 *****************************************************************************/
78 vlc_bool_t b_rotation;
79 vout_thread_t *p_vout;
82 /*****************************************************************************
83 * Create: allocates Transform video thread output method
84 *****************************************************************************
85 * This function allocates and initializes a Transform vout method.
86 *****************************************************************************/
87 static int Create( vlc_object_t *p_this )
89 vout_thread_t *p_vout = (vout_thread_t *)p_this;
92 /* Allocate structure */
93 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
94 if( p_vout->p_sys == NULL )
96 msg_Err( p_vout, "out of memory" );
100 p_vout->pf_init = Init;
101 p_vout->pf_end = End;
102 p_vout->pf_manage = NULL;
103 p_vout->pf_render = Render;
104 p_vout->pf_display = NULL;
106 /* Look what method was requested */
107 psz_method = config_GetPsz( p_vout, "transform-type" );
109 if( psz_method == NULL )
111 msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
112 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
113 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
114 p_vout->p_sys->b_rotation = 1;
118 if( !strcmp( psz_method, "hflip" ) )
120 p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
121 p_vout->p_sys->b_rotation = 0;
123 else if( !strcmp( psz_method, "vflip" ) )
125 p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
126 p_vout->p_sys->b_rotation = 0;
128 else if( !strcmp( psz_method, "90" ) )
130 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
131 p_vout->p_sys->b_rotation = 1;
133 else if( !strcmp( psz_method, "180" ) )
135 p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
136 p_vout->p_sys->b_rotation = 0;
138 else if( !strcmp( psz_method, "270" ) )
140 p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
141 p_vout->p_sys->b_rotation = 1;
145 msg_Err( p_vout, "no valid transform mode provided, using '90'" );
146 p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
147 p_vout->p_sys->b_rotation = 1;
156 /*****************************************************************************
157 * Init: initialize Transform video thread output method
158 *****************************************************************************/
159 static int Init( vout_thread_t *p_vout )
164 I_OUTPUTPICTURES = 0;
166 /* Initialize the output structure */
167 p_vout->output.i_chroma = p_vout->render.i_chroma;
168 p_vout->output.i_width = p_vout->render.i_width;
169 p_vout->output.i_height = p_vout->render.i_height;
170 p_vout->output.i_aspect = p_vout->render.i_aspect;
172 /* Try to open the real video output */
173 msg_Dbg( p_vout, "spawning the real video output" );
175 if( p_vout->p_sys->b_rotation )
177 p_vout->p_sys->p_vout = vout_Create( p_vout,
178 p_vout->render.i_height, p_vout->render.i_width,
179 p_vout->render.i_chroma,
180 (uint64_t)VOUT_ASPECT_FACTOR
181 * (uint64_t)VOUT_ASPECT_FACTOR
182 / (uint64_t)p_vout->render.i_aspect );
186 p_vout->p_sys->p_vout = vout_Create( p_vout,
187 p_vout->render.i_width, p_vout->render.i_height,
188 p_vout->render.i_chroma, p_vout->render.i_aspect );
191 /* Everything failed */
192 if( p_vout->p_sys->p_vout == NULL )
194 msg_Err( p_vout, "cannot open vout, aborting" );
198 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
203 /*****************************************************************************
204 * End: terminate Transform video thread output method
205 *****************************************************************************/
206 static void End( vout_thread_t *p_vout )
210 /* Free the fake output buffers we allocated */
211 for( i_index = I_OUTPUTPICTURES ; i_index ; )
214 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
218 /*****************************************************************************
219 * Destroy: destroy Transform video thread output method
220 *****************************************************************************
221 * Terminate an output method created by TransformCreateOutputMethod
222 *****************************************************************************/
223 static void Destroy( vlc_object_t *p_this )
225 vout_thread_t *p_vout = (vout_thread_t *)p_this;
227 vout_Destroy( p_vout->p_sys->p_vout );
229 free( p_vout->p_sys );
232 /*****************************************************************************
233 * Render: displays previously rendered output
234 *****************************************************************************
235 * This function send the currently rendered image to Transform image, waits
236 * until it is displayed and switch the two rendering buffers, preparing next
238 *****************************************************************************/
239 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
244 /* This is a new frame. Get a structure from the video_output. */
245 while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
248 if( p_vout->b_die || p_vout->b_error )
252 msleep( VOUT_OUTMEM_SLEEP );
255 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
256 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
258 switch( p_vout->p_sys->i_mode )
260 case TRANSFORM_MODE_90:
261 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
263 int i_pitch = p_pic->p[i_index].i_pitch;
265 uint8_t *p_in = p_pic->p[i_index].p_pixels;
267 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
268 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
269 * p_outpic->p[i_index].i_pitch;
271 for( ; p_out < p_out_end ; )
275 p_out_end -= p_outpic->p[i_index].i_pitch
276 - p_outpic->p[i_index].i_visible_pitch;
277 p_line_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
279 for( ; p_in < p_line_end ; )
281 p_line_end -= i_pitch;
282 *(--p_out_end) = *p_line_end;
290 case TRANSFORM_MODE_180:
291 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
293 uint8_t *p_in = p_pic->p[i_index].p_pixels;
294 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
295 * p_pic->p[i_index].i_pitch;
297 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
299 for( ; p_in < p_in_end ; )
301 uint8_t *p_line_start = p_in - p_pic->p[i_index].i_pitch;
302 p_in_end -= p_pic->p[i_index].i_pitch
303 - p_pic->p[i_index].i_visible_pitch;
305 for( ; p_line_start < p_in_end ; )
307 *p_out++ = *(--p_in_end);
310 p_out += p_outpic->p[i_index].i_pitch
311 - p_outpic->p[i_index].i_visible_pitch;
316 case TRANSFORM_MODE_270:
317 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
319 int i_pitch = p_pic->p[i_index].i_pitch;
321 uint8_t *p_in = p_pic->p[i_index].p_pixels;
323 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
324 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
325 * p_outpic->p[i_index].i_pitch;
327 for( ; p_out < p_out_end ; )
331 p_in_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
333 for( ; p_in < p_in_end ; )
336 *p_out++ = *p_in_end;
339 p_out += p_outpic->p[i_index].i_pitch
340 - p_outpic->p[i_index].i_visible_pitch;
346 case TRANSFORM_MODE_VFLIP:
347 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
349 uint8_t *p_in = p_pic->p[i_index].p_pixels;
350 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
351 * p_pic->p[i_index].i_pitch;
353 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
355 for( ; p_in < p_in_end ; )
357 p_in_end -= p_pic->p[i_index].i_pitch;
358 p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
359 p_pic->p[i_index].i_visible_pitch );
360 p_out += p_pic->p[i_index].i_pitch;
365 case TRANSFORM_MODE_HFLIP:
366 for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
368 uint8_t *p_in = p_pic->p[i_index].p_pixels;
369 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
370 * p_pic->p[i_index].i_pitch;
372 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
374 for( ; p_in < p_in_end ; )
376 uint8_t *p_line_end = p_in
377 + p_pic->p[i_index].i_visible_pitch;
379 for( ; p_in < p_line_end ; )
381 *p_out++ = *(--p_line_end);
384 p_in += p_pic->p[i_index].i_pitch;
393 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
395 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );