* ./modules/*: moved plugins to the new tree. Yet untested builds include
[vlc.git] / modules / video_filter / transform.c
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 $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33
34 #include "filter_common.h"
35
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
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47
48 static int  Init      ( vout_thread_t * );
49 static void End       ( vout_thread_t * );
50 static void Render    ( vout_thread_t *, picture_t * );
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 #define TYPE_TEXT N_("Transform type")
56 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
57
58 static char *type_list[] = { "90", "180", "270", "hflip", "vflip", NULL };
59
60 vlc_module_begin();
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 );
68 vlc_module_end();
69
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 struct vout_sys_t
77 {
78     int i_mode;
79     vlc_bool_t b_rotation;
80     vout_thread_t *p_vout;
81 };
82
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 )
89 {
90     vout_thread_t *p_vout = (vout_thread_t *)p_this;
91     char *psz_method;
92
93     /* Allocate structure */
94     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
95     if( p_vout->p_sys == NULL )
96     {
97         msg_Err( p_vout, "out of memory" );
98         return( 1 );
99     }
100
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;
106
107     /* Look what method was requested */
108     psz_method = config_GetPsz( p_vout, "transform-type" );
109
110     if( psz_method == NULL )
111     {
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;
116     }
117     else
118     {
119         if( !strcmp( psz_method, "hflip" ) )
120         {
121             p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
122             p_vout->p_sys->b_rotation = 0;
123         }
124         else if( !strcmp( psz_method, "vflip" ) )
125         {
126             p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
127             p_vout->p_sys->b_rotation = 0;
128         }
129         else if( !strcmp( psz_method, "90" ) )
130         {
131             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
132             p_vout->p_sys->b_rotation = 1;
133         }
134         else if( !strcmp( psz_method, "180" ) )
135         {
136             p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
137             p_vout->p_sys->b_rotation = 0;
138         }
139         else if( !strcmp( psz_method, "270" ) )
140         {
141             p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
142             p_vout->p_sys->b_rotation = 1;
143         }
144         else
145         {
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;
149         }
150
151         free( psz_method );
152     }
153     
154     return( 0 );
155 }
156
157 /*****************************************************************************
158  * Init: initialize Transform video thread output method
159  *****************************************************************************/
160 static int Init( vout_thread_t *p_vout )
161 {
162     int i_index;
163     picture_t *p_pic;
164     
165     I_OUTPUTPICTURES = 0;
166
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;
172
173     /* Try to open the real video output */
174     msg_Dbg( p_vout, "spawning the real video output" );
175
176     if( p_vout->p_sys->b_rotation )
177     {
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 );
184     }
185     else
186     {
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 );
191     }
192
193     /* Everything failed */
194     if( p_vout->p_sys->p_vout == NULL )
195     {
196         msg_Err( p_vout, "cannot open vout, aborting" );
197         return( 0 );
198     }
199  
200     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
201
202     return( 0 );
203 }
204
205 /*****************************************************************************
206  * End: terminate Transform video thread output method
207  *****************************************************************************/
208 static void End( vout_thread_t *p_vout )
209 {
210     int i_index;
211
212     /* Free the fake output buffers we allocated */
213     for( i_index = I_OUTPUTPICTURES ; i_index ; )
214     {
215         i_index--;
216         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
217     }
218 }
219
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 )
226 {
227     vout_thread_t *p_vout = (vout_thread_t *)p_this;
228
229     vout_DestroyThread( p_vout->p_sys->p_vout );
230
231     free( p_vout->p_sys );
232 }
233
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
239  * frame.
240  *****************************************************************************/
241 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
242 {
243     picture_t *p_outpic;
244     int i_index;
245
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 ) )
248               == NULL )
249     {
250         if( p_vout->b_die || p_vout->b_error )
251         {
252             return;
253         }
254         msleep( VOUT_OUTMEM_SLEEP );
255     }   
256
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 );
259
260     switch( p_vout->p_sys->i_mode )
261     {
262         case TRANSFORM_MODE_90:
263             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
264             {
265                 int i_pitch = p_pic->p[i_index].i_pitch;
266
267                 u8 *p_in = p_pic->p[i_index].p_pixels;
268
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;
272
273                 for( ; p_out < p_out_end ; )
274                 {
275                     u8 *p_line_end;
276
277                     p_line_end = p_in + p_pic->p[i_index].i_lines
278                                          * p_pic->p[i_index].i_pitch;
279
280                     for( ; p_in < p_line_end ; )
281                     {
282                         p_line_end -= i_pitch;
283                         *(--p_out_end) = *p_line_end;
284                     }
285
286                     p_in++;
287                 }
288             }
289             break;
290
291         case TRANSFORM_MODE_180:
292             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
293             {
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;
297
298                 u8 *p_out = p_outpic->p[i_index].p_pixels;
299
300                 for( ; p_in < p_in_end ; )
301                 {
302                     *p_out++ = *(--p_in_end);
303                 }
304             }
305             break;
306
307         case TRANSFORM_MODE_270:
308             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
309             {
310                 int i_pitch = p_pic->p[i_index].i_pitch;
311
312                 u8 *p_in = p_pic->p[i_index].p_pixels;
313
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;
317
318                 for( ; p_out < p_out_end ; )
319                 {
320                     u8 *p_in_end;
321
322                     p_in_end = p_in + p_pic->p[i_index].i_lines
323                                        * p_pic->p[i_index].i_pitch;
324
325                     for( ; p_in < p_in_end ; )
326                     {
327                         p_in_end -= i_pitch;
328                         *p_out++ = *p_in_end;
329                     }
330
331                     p_in++;
332                 }
333             }
334             break;
335
336         case TRANSFORM_MODE_VFLIP:
337             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
338             {
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;
342
343                 u8 *p_out = p_outpic->p[i_index].p_pixels;
344
345                 for( ; p_in < p_in_end ; )
346                 {
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;
351                 }
352             }
353             break;
354
355         case TRANSFORM_MODE_HFLIP:
356             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
357             {
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;
361
362                 u8 *p_out = p_outpic->p[i_index].p_pixels;
363
364                 for( ; p_in < p_in_end ; )
365                 {
366                     u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
367
368                     for( ; p_in < p_line_end ; )
369                     {
370                         *p_out++ = *(--p_line_end);
371                     }
372
373                     p_in += p_pic->p[i_index].i_pitch;
374                 }
375             }
376             break;
377
378         default:
379             break;
380     }
381
382     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
383
384     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
385 }
386