]> git.sesse.net Git - vlc/blob - plugins/filter/transform.c
* ./BUGS: added a list of known bugs. Please add your findings!
[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.4 2002/01/04 14:01:34 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 MODULE_CONFIG_STOP
54
55 MODULE_INIT_START
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" )
61 MODULE_INIT_STOP
62
63 MODULE_ACTIVATE_START
64     vout_getfunctions( &p_module->p_functions->vout );
65 MODULE_ACTIVATE_STOP
66
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
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 typedef struct vout_sys_s
77 {
78     int i_mode;
79     boolean_t b_rotation;
80     struct vout_thread_s *p_vout;
81
82 } vout_sys_t;
83
84 /*****************************************************************************
85  * Local prototypes
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_Render    ( struct vout_thread_s *, struct picture_s * );
94 static void vout_Display   ( struct vout_thread_s *, struct picture_s * );
95
96 /*****************************************************************************
97  * Functions exported as capabilities. They are declared as static so that
98  * we don't pollute the namespace too much.
99  *****************************************************************************/
100 static void vout_getfunctions( function_list_t * p_function_list )
101 {
102     p_function_list->pf_probe = vout_Probe;
103     p_function_list->functions.vout.pf_create     = vout_Create;
104     p_function_list->functions.vout.pf_init       = vout_Init;
105     p_function_list->functions.vout.pf_end        = vout_End;
106     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
107     p_function_list->functions.vout.pf_manage     = vout_Manage;
108     p_function_list->functions.vout.pf_render     = vout_Render;
109     p_function_list->functions.vout.pf_display    = vout_Display;
110     p_function_list->functions.vout.pf_setpalette = NULL;
111 }
112
113 /*****************************************************************************
114  * intf_Probe: return a score
115  *****************************************************************************/
116 static int vout_Probe( probedata_t *p_data )
117 {
118     return( 0 );
119 }
120
121 /*****************************************************************************
122  * vout_Create: allocates Transform video thread output method
123  *****************************************************************************
124  * This function allocates and initializes a Transform vout method.
125  *****************************************************************************/
126 static int vout_Create( vout_thread_t *p_vout )
127 {
128     char *psz_method;
129
130     /* Allocate structure */
131     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
132     if( p_vout->p_sys == NULL )
133     {
134         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
135         return( 1 );
136     }
137
138     /* Look what method was requested */
139     psz_method = main_GetPszVariable( VOUT_FILTER_VAR, "" );
140
141     while( *psz_method && *psz_method != ':' )
142     {
143         psz_method++;
144     }
145
146     if( !strcmp( psz_method, ":hflip" ) )
147     {
148         p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
149         p_vout->p_sys->b_rotation = 0;
150     }
151     else if( !strcmp( psz_method, ":vflip" ) )
152     {
153         p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
154         p_vout->p_sys->b_rotation = 0;
155     }
156     else if( !strcmp( psz_method, ":90" ) )
157     {
158         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
159         p_vout->p_sys->b_rotation = 1;
160     }
161     else if( !strcmp( psz_method, ":180" ) )
162     {
163         p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
164         p_vout->p_sys->b_rotation = 0;
165     }
166     else if( !strcmp( psz_method, ":270" ) )
167     {
168         p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
169         p_vout->p_sys->b_rotation = 1;
170     }
171     else
172     {
173         intf_ErrMsg( "filter error: no valid transform mode provided, "
174                      "using transform:90" );
175         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
176         p_vout->p_sys->b_rotation = 1;
177     }
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 = main_GetPszVariable( VOUT_FILTER_VAR, "" );
201     main_PutPszVariable( VOUT_FILTER_VAR, "" );
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     /* Everything failed */
223     if( p_vout->p_sys->p_vout == NULL )
224     {
225         intf_ErrMsg( "filter error: can't open vout, aborting" );
226         return( 0 );
227     }
228  
229     main_PutPszVariable( VOUT_FILTER_VAR, psz_filter );
230
231     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
232
233     return( 0 );
234 }
235
236 /*****************************************************************************
237  * vout_End: terminate Transform video thread output method
238  *****************************************************************************/
239 static void vout_End( vout_thread_t *p_vout )
240 {
241     int i_index;
242
243     /* Free the fake output buffers we allocated */
244     for( i_index = I_OUTPUTPICTURES ; i_index ; )
245     {
246         i_index--;
247         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
248     }
249 }
250
251 /*****************************************************************************
252  * vout_Destroy: destroy Transform video thread output method
253  *****************************************************************************
254  * Terminate an output method created by TransformCreateOutputMethod
255  *****************************************************************************/
256 static void vout_Destroy( vout_thread_t *p_vout )
257 {
258     vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
259
260     free( p_vout->p_sys );
261 }
262
263 /*****************************************************************************
264  * vout_Manage: handle Transform events
265  *****************************************************************************
266  * This function should be called regularly by video output thread. It manages
267  * console events. It returns a non null value on error.
268  *****************************************************************************/
269 static int vout_Manage( vout_thread_t *p_vout )
270 {
271     return( 0 );
272 }
273
274 /*****************************************************************************
275  * vout_Render: displays previously rendered output
276  *****************************************************************************
277  * This function send the currently rendered image to Transform image, waits
278  * until it is displayed and switch the two rendering buffers, preparing next
279  * frame.
280  *****************************************************************************/
281 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
282 {
283     picture_t *p_outpic;
284     int i_index;
285
286     /* This is a new frame. Get a structure from the video_output. */
287     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
288               == NULL )
289     {
290         if( p_vout->b_die || p_vout->b_error )
291         {
292             return;
293         }
294         msleep( VOUT_OUTMEM_SLEEP );
295     }   
296
297     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
298     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
299
300     switch( p_vout->p_sys->i_mode )
301     {
302         case TRANSFORM_MODE_90:
303             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
304             {
305                 int i_pitch = p_pic->p[i_index].i_pitch;
306
307                 u8 *p_in = p_pic->p[i_index].p_pixels;
308
309                 u8 *p_out = p_outpic->p[i_index].p_pixels;
310                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
311                                          * p_outpic->p[i_index].i_pitch;
312
313                 for( ; p_out < p_out_end ; )
314                 {
315                     u8 *p_line_end;
316
317                     p_line_end = p_in + p_pic->p[i_index].i_lines
318                                          * p_pic->p[i_index].i_pitch;
319
320                     for( ; p_in < p_line_end ; )
321                     {
322                         p_line_end -= i_pitch;
323                         *(--p_out_end) = *p_line_end;
324                     }
325
326                     p_in++;
327                 }
328             }
329             break;
330
331         case TRANSFORM_MODE_180:
332             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
333             {
334                 u8 *p_in = p_pic->p[i_index].p_pixels;
335                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
336                                        * p_pic->p[i_index].i_pitch;
337
338                 u8 *p_out = p_outpic->p[i_index].p_pixels;
339
340                 for( ; p_in < p_in_end ; )
341                 {
342                     *p_out++ = *(--p_in_end);
343                 }
344             }
345             break;
346
347         case TRANSFORM_MODE_270:
348             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
349             {
350                 int i_pitch = p_pic->p[i_index].i_pitch;
351
352                 u8 *p_in = p_pic->p[i_index].p_pixels;
353
354                 u8 *p_out = p_outpic->p[i_index].p_pixels;
355                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
356                                          * p_outpic->p[i_index].i_pitch;
357
358                 for( ; p_out < p_out_end ; )
359                 {
360                     u8 *p_in_end;
361
362                     p_in_end = p_in + p_pic->p[i_index].i_lines
363                                        * p_pic->p[i_index].i_pitch;
364
365                     for( ; p_in < p_in_end ; )
366                     {
367                         p_in_end -= i_pitch;
368                         *p_out++ = *p_in_end;
369                     }
370
371                     p_in++;
372                 }
373             }
374             break;
375
376         case TRANSFORM_MODE_VFLIP:
377             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
378             {
379                 u8 *p_in = p_pic->p[i_index].p_pixels;
380                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
381                                        * p_pic->p[i_index].i_pitch;
382
383                 u8 *p_out = p_outpic->p[i_index].p_pixels;
384
385                 for( ; p_in < p_in_end ; )
386                 {
387                     p_in_end -= p_pic->p[i_index].i_pitch;
388                     FAST_MEMCPY( p_out, p_in_end, p_pic->p[i_index].i_pitch );
389                     p_out += p_pic->p[i_index].i_pitch;
390                 }
391             }
392             break;
393
394         case TRANSFORM_MODE_HFLIP:
395             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
396             {
397                 u8 *p_in = p_pic->p[i_index].p_pixels;
398                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
399                                        * p_pic->p[i_index].i_pitch;
400
401                 u8 *p_out = p_outpic->p[i_index].p_pixels;
402
403                 for( ; p_in < p_in_end ; )
404                 {
405                     u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
406
407                     for( ; p_in < p_line_end ; )
408                     {
409                         *p_out++ = *(--p_line_end);
410                     }
411
412                     p_in += p_pic->p[i_index].i_pitch;
413                 }
414             }
415             break;
416
417         default:
418             break;
419     }
420
421     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
422
423     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
424 }
425
426 /*****************************************************************************
427  * vout_Display: displays previously rendered output
428  *****************************************************************************
429  * This function send the currently rendered image to Invert image, waits
430  * until it is displayed and switch the two rendering buffers, preparing next
431  * frame.
432  *****************************************************************************/
433 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
434 {
435     ;
436 }
437