]> git.sesse.net Git - vlc/blob - plugins/filter/transform.c
* ./plugins/filter/*: filters are now configurable through the configuration
[vlc] / plugins / 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.11 2002/05/27 19:35:41 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 <videolan/vlc.h>
32
33 #include "video.h"
34 #include "video_output.h"
35
36 #include "filter_common.h"
37
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
43
44 /*****************************************************************************
45  * Capabilities defined in the other files.
46  *****************************************************************************/
47 static void vout_getfunctions( function_list_t * p_function_list );
48
49 /*****************************************************************************
50  * Build configuration tree.
51  *****************************************************************************/
52 MODULE_CONFIG_START
53 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
54 ADD_STRING("transform_type", "90", NULL, N_("Transform type"),
55            N_("One of '90', '180', '270', 'hflip' and 'vflip'"))
56 MODULE_CONFIG_STOP
57
58 MODULE_INIT_START
59     SET_DESCRIPTION( _("image transformation module") )
60     /* Capability score set to 0 because we don't want to be spawned
61      * as a video output unless explicitly requested to */
62     ADD_CAPABILITY( VOUT, 0 )
63     ADD_SHORTCUT( "transform" )
64 MODULE_INIT_STOP
65
66 MODULE_ACTIVATE_START
67     vout_getfunctions( &p_module->p_functions->vout );
68 MODULE_ACTIVATE_STOP
69
70 MODULE_DEACTIVATE_START
71 MODULE_DEACTIVATE_STOP
72
73 /*****************************************************************************
74  * vout_sys_t: Transform video output method descriptor
75  *****************************************************************************
76  * This structure is part of the video output thread descriptor.
77  * It describes the Transform specific properties of an output thread.
78  *****************************************************************************/
79 typedef struct vout_sys_s
80 {
81     int i_mode;
82     boolean_t b_rotation;
83     struct vout_thread_s *p_vout;
84
85 } vout_sys_t;
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int  vout_Create    ( struct vout_thread_s * );
91 static int  vout_Init      ( struct vout_thread_s * );
92 static void vout_End       ( struct vout_thread_s * );
93 static void vout_Destroy   ( struct vout_thread_s * );
94 static int  vout_Manage    ( struct vout_thread_s * );
95 static void vout_Render    ( struct vout_thread_s *, struct picture_s * );
96 static void vout_Display   ( struct vout_thread_s *, struct picture_s * );
97
98 /*****************************************************************************
99  * Functions exported as capabilities. They are declared as static so that
100  * we don't pollute the namespace too much.
101  *****************************************************************************/
102 static void vout_getfunctions( function_list_t * p_function_list )
103 {
104     p_function_list->functions.vout.pf_create     = vout_Create;
105     p_function_list->functions.vout.pf_init       = vout_Init;
106     p_function_list->functions.vout.pf_end        = vout_End;
107     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
108     p_function_list->functions.vout.pf_manage     = vout_Manage;
109     p_function_list->functions.vout.pf_render     = vout_Render;
110     p_function_list->functions.vout.pf_display    = vout_Display;
111 }
112
113 /*****************************************************************************
114  * vout_Create: allocates Transform video thread output method
115  *****************************************************************************
116  * This function allocates and initializes a Transform vout method.
117  *****************************************************************************/
118 static int vout_Create( vout_thread_t *p_vout )
119 {
120     char *psz_method, *psz_method_tmp;
121
122     /* Allocate structure */
123     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
124     if( p_vout->p_sys == NULL )
125     {
126         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
127         return( 1 );
128     }
129
130     /* Look what method was requested */
131     if( !(psz_method = psz_method_tmp
132           = config_GetPszVariable( "transform_type" )) )
133     {
134         intf_ErrMsg( "vout error: configuration variable %s empty",
135                      "transform_type" );
136         intf_ErrMsg( "filter error: no valid transform mode provided, "
137                      "using '90'" );
138         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
139         p_vout->p_sys->b_rotation = 1;
140     }
141     else
142     {
143         if( !strcmp( psz_method, "hflip" ) )
144         {
145             p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
146             p_vout->p_sys->b_rotation = 0;
147         }
148         else if( !strcmp( psz_method, "vflip" ) )
149         {
150             p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
151             p_vout->p_sys->b_rotation = 0;
152         }
153         else if( !strcmp( psz_method, "90" ) )
154         {
155             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
156             p_vout->p_sys->b_rotation = 1;
157         }
158         else if( !strcmp( psz_method, "180" ) )
159         {
160             p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
161             p_vout->p_sys->b_rotation = 0;
162         }
163         else if( !strcmp( psz_method, "270" ) )
164         {
165             p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
166             p_vout->p_sys->b_rotation = 1;
167         }
168         else
169         {
170             intf_ErrMsg( "filter error: no valid transform mode provided, "
171                          "using '90'" );
172             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
173             p_vout->p_sys->b_rotation = 1;
174         }
175     }
176     
177     free( psz_method_tmp );
178
179     return( 0 );
180 }
181
182 /*****************************************************************************
183  * vout_Init: initialize Transform video thread output method
184  *****************************************************************************/
185 static int vout_Init( vout_thread_t *p_vout )
186 {
187     int i_index;
188     char *psz_filter;
189     picture_t *p_pic;
190     
191     I_OUTPUTPICTURES = 0;
192
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;
198
199     /* Try to open the real video output */
200     psz_filter = config_GetPszVariable( "filter" );
201     config_PutPszVariable( "filter", NULL );
202
203     intf_WarnMsg( 1, "filter: spawning the real video output" );
204
205     if( p_vout->p_sys->b_rotation )
206     {
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 );
213     }
214     else
215     {
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 );
220     }
221
222     config_PutPszVariable( "filter", psz_filter );
223     if( psz_filter ) free( psz_filter );
224
225     /* Everything failed */
226     if( p_vout->p_sys->p_vout == NULL )
227     {
228         intf_ErrMsg( "filter error: can't open vout, aborting" );
229         return( 0 );
230     }
231  
232     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
233
234     return( 0 );
235 }
236
237 /*****************************************************************************
238  * vout_End: terminate Transform video thread output method
239  *****************************************************************************/
240 static void vout_End( vout_thread_t *p_vout )
241 {
242     int i_index;
243
244     /* Free the fake output buffers we allocated */
245     for( i_index = I_OUTPUTPICTURES ; i_index ; )
246     {
247         i_index--;
248         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
249     }
250 }
251
252 /*****************************************************************************
253  * vout_Destroy: destroy Transform video thread output method
254  *****************************************************************************
255  * Terminate an output method created by TransformCreateOutputMethod
256  *****************************************************************************/
257 static void vout_Destroy( vout_thread_t *p_vout )
258 {
259     vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
260
261     free( p_vout->p_sys );
262 }
263
264 /*****************************************************************************
265  * vout_Manage: handle Transform events
266  *****************************************************************************
267  * This function should be called regularly by video output thread. It manages
268  * console events. It returns a non null value on error.
269  *****************************************************************************/
270 static int vout_Manage( vout_thread_t *p_vout )
271 {
272     return( 0 );
273 }
274
275 /*****************************************************************************
276  * vout_Render: displays previously rendered output
277  *****************************************************************************
278  * This function send the currently rendered image to Transform image, waits
279  * until it is displayed and switch the two rendering buffers, preparing next
280  * frame.
281  *****************************************************************************/
282 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
283 {
284     picture_t *p_outpic;
285     int i_index;
286
287     /* This is a new frame. Get a structure from the video_output. */
288     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
289               == NULL )
290     {
291         if( p_vout->b_die || p_vout->b_error )
292         {
293             return;
294         }
295         msleep( VOUT_OUTMEM_SLEEP );
296     }   
297
298     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
299     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
300
301     switch( p_vout->p_sys->i_mode )
302     {
303         case TRANSFORM_MODE_90:
304             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
305             {
306                 int i_pitch = p_pic->p[i_index].i_pitch;
307
308                 u8 *p_in = p_pic->p[i_index].p_pixels;
309
310                 u8 *p_out = p_outpic->p[i_index].p_pixels;
311                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
312                                          * p_outpic->p[i_index].i_pitch;
313
314                 for( ; p_out < p_out_end ; )
315                 {
316                     u8 *p_line_end;
317
318                     p_line_end = p_in + p_pic->p[i_index].i_lines
319                                          * p_pic->p[i_index].i_pitch;
320
321                     for( ; p_in < p_line_end ; )
322                     {
323                         p_line_end -= i_pitch;
324                         *(--p_out_end) = *p_line_end;
325                     }
326
327                     p_in++;
328                 }
329             }
330             break;
331
332         case TRANSFORM_MODE_180:
333             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
334             {
335                 u8 *p_in = p_pic->p[i_index].p_pixels;
336                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
337                                        * p_pic->p[i_index].i_pitch;
338
339                 u8 *p_out = p_outpic->p[i_index].p_pixels;
340
341                 for( ; p_in < p_in_end ; )
342                 {
343                     *p_out++ = *(--p_in_end);
344                 }
345             }
346             break;
347
348         case TRANSFORM_MODE_270:
349             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
350             {
351                 int i_pitch = p_pic->p[i_index].i_pitch;
352
353                 u8 *p_in = p_pic->p[i_index].p_pixels;
354
355                 u8 *p_out = p_outpic->p[i_index].p_pixels;
356                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
357                                          * p_outpic->p[i_index].i_pitch;
358
359                 for( ; p_out < p_out_end ; )
360                 {
361                     u8 *p_in_end;
362
363                     p_in_end = p_in + p_pic->p[i_index].i_lines
364                                        * p_pic->p[i_index].i_pitch;
365
366                     for( ; p_in < p_in_end ; )
367                     {
368                         p_in_end -= i_pitch;
369                         *p_out++ = *p_in_end;
370                     }
371
372                     p_in++;
373                 }
374             }
375             break;
376
377         case TRANSFORM_MODE_VFLIP:
378             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
379             {
380                 u8 *p_in = p_pic->p[i_index].p_pixels;
381                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
382                                        * p_pic->p[i_index].i_pitch;
383
384                 u8 *p_out = p_outpic->p[i_index].p_pixels;
385
386                 for( ; p_in < p_in_end ; )
387                 {
388                     p_in_end -= p_pic->p[i_index].i_pitch;
389                     FAST_MEMCPY( p_out, p_in_end, p_pic->p[i_index].i_pitch );
390                     p_out += p_pic->p[i_index].i_pitch;
391                 }
392             }
393             break;
394
395         case TRANSFORM_MODE_HFLIP:
396             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
397             {
398                 u8 *p_in = p_pic->p[i_index].p_pixels;
399                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
400                                        * p_pic->p[i_index].i_pitch;
401
402                 u8 *p_out = p_outpic->p[i_index].p_pixels;
403
404                 for( ; p_in < p_in_end ; )
405                 {
406                     u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
407
408                     for( ; p_in < p_line_end ; )
409                     {
410                         *p_out++ = *(--p_line_end);
411                     }
412
413                     p_in += p_pic->p[i_index].i_pitch;
414                 }
415             }
416             break;
417
418         default:
419             break;
420     }
421
422     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
423
424     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
425 }
426
427 /*****************************************************************************
428  * vout_Display: displays previously rendered output
429  *****************************************************************************
430  * This function send the currently rendered image to Invert image, waits
431  * until it is displayed and switch the two rendering buffers, preparing next
432  * frame.
433  *****************************************************************************/
434 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
435 {
436     ;
437 }
438