]> git.sesse.net Git - vlc/blob - plugins/filter/transform.c
f3367b9455beb02a1a8cc28e3c7ae717fe0e17f8
[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.9 2002/04/19 13:56:11 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_Create    ( struct vout_thread_s * );
88 static int  vout_Init      ( struct vout_thread_s * );
89 static void vout_End       ( struct vout_thread_s * );
90 static void vout_Destroy   ( struct vout_thread_s * );
91 static int  vout_Manage    ( struct vout_thread_s * );
92 static void vout_Render    ( struct vout_thread_s *, struct picture_s * );
93 static void vout_Display   ( struct vout_thread_s *, struct picture_s * );
94
95 /*****************************************************************************
96  * Functions exported as capabilities. They are declared as static so that
97  * we don't pollute the namespace too much.
98  *****************************************************************************/
99 static void vout_getfunctions( function_list_t * p_function_list )
100 {
101     p_function_list->functions.vout.pf_create     = vout_Create;
102     p_function_list->functions.vout.pf_init       = vout_Init;
103     p_function_list->functions.vout.pf_end        = vout_End;
104     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
105     p_function_list->functions.vout.pf_manage     = vout_Manage;
106     p_function_list->functions.vout.pf_render     = vout_Render;
107     p_function_list->functions.vout.pf_display    = vout_Display;
108 }
109
110 /*****************************************************************************
111  * vout_Create: allocates Transform video thread output method
112  *****************************************************************************
113  * This function allocates and initializes a Transform vout method.
114  *****************************************************************************/
115 static int vout_Create( vout_thread_t *p_vout )
116 {
117     char *psz_method, *psz_method_tmp;
118
119     /* Allocate structure */
120     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
121     if( p_vout->p_sys == NULL )
122     {
123         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
124         return( 1 );
125     }
126
127     /* Look what method was requested */
128     if( !(psz_method = psz_method_tmp
129           = config_GetPszVariable( "filter" )) )
130     {
131         intf_ErrMsg( "vout error: configuration variable %s empty",
132                      "filter" );
133         return( 1 );
134     }
135
136     while( *psz_method && *psz_method != ':' )
137     {
138         psz_method++;
139     }
140
141     if( !strcmp( psz_method, ":hflip" ) )
142     {
143         p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
144         p_vout->p_sys->b_rotation = 0;
145     }
146     else if( !strcmp( psz_method, ":vflip" ) )
147     {
148         p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
149         p_vout->p_sys->b_rotation = 0;
150     }
151     else if( !strcmp( psz_method, ":90" ) )
152     {
153         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
154         p_vout->p_sys->b_rotation = 1;
155     }
156     else if( !strcmp( psz_method, ":180" ) )
157     {
158         p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
159         p_vout->p_sys->b_rotation = 0;
160     }
161     else if( !strcmp( psz_method, ":270" ) )
162     {
163         p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
164         p_vout->p_sys->b_rotation = 1;
165     }
166     else
167     {
168         intf_ErrMsg( "filter error: no valid transform mode provided, "
169                      "using transform:90" );
170         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
171         p_vout->p_sys->b_rotation = 1;
172     }
173
174     free( psz_method_tmp );
175
176     return( 0 );
177 }
178
179 /*****************************************************************************
180  * vout_Init: initialize Transform video thread output method
181  *****************************************************************************/
182 static int vout_Init( vout_thread_t *p_vout )
183 {
184     int i_index;
185     char *psz_filter;
186     picture_t *p_pic;
187     
188     I_OUTPUTPICTURES = 0;
189
190     /* Initialize the output structure */
191     p_vout->output.i_chroma = p_vout->render.i_chroma;
192     p_vout->output.i_width  = p_vout->render.i_width;
193     p_vout->output.i_height = p_vout->render.i_height;
194     p_vout->output.i_aspect = p_vout->render.i_aspect;
195
196     /* Try to open the real video output */
197     psz_filter = config_GetPszVariable( "filter" );
198     config_PutPszVariable( "filter", NULL );
199
200     intf_WarnMsg( 1, "filter: spawning the real video output" );
201
202     if( p_vout->p_sys->b_rotation )
203     {
204         p_vout->p_sys->p_vout =
205             vout_CreateThread( NULL,
206                            p_vout->render.i_height, p_vout->render.i_width,
207                            p_vout->render.i_chroma,
208                            (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
209                                / (u64)p_vout->render.i_aspect );
210     }
211     else
212     {
213         p_vout->p_sys->p_vout =
214             vout_CreateThread( NULL,
215                            p_vout->render.i_width, p_vout->render.i_height,
216                            p_vout->render.i_chroma, p_vout->render.i_aspect );
217     }
218
219     config_PutPszVariable( "filter", psz_filter );
220     if( psz_filter ) free( psz_filter );
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     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
230
231     return( 0 );
232 }
233
234 /*****************************************************************************
235  * vout_End: terminate Transform video thread output method
236  *****************************************************************************/
237 static void vout_End( vout_thread_t *p_vout )
238 {
239     int i_index;
240
241     /* Free the fake output buffers we allocated */
242     for( i_index = I_OUTPUTPICTURES ; i_index ; )
243     {
244         i_index--;
245         free( PP_OUTPUTPICTURE[ i_index ]->p_data );
246     }
247 }
248
249 /*****************************************************************************
250  * vout_Destroy: destroy Transform video thread output method
251  *****************************************************************************
252  * Terminate an output method created by TransformCreateOutputMethod
253  *****************************************************************************/
254 static void vout_Destroy( vout_thread_t *p_vout )
255 {
256     vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
257
258     free( p_vout->p_sys );
259 }
260
261 /*****************************************************************************
262  * vout_Manage: handle Transform events
263  *****************************************************************************
264  * This function should be called regularly by video output thread. It manages
265  * console events. It returns a non null value on error.
266  *****************************************************************************/
267 static int vout_Manage( vout_thread_t *p_vout )
268 {
269     return( 0 );
270 }
271
272 /*****************************************************************************
273  * vout_Render: displays previously rendered output
274  *****************************************************************************
275  * This function send the currently rendered image to Transform image, waits
276  * until it is displayed and switch the two rendering buffers, preparing next
277  * frame.
278  *****************************************************************************/
279 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
280 {
281     picture_t *p_outpic;
282     int i_index;
283
284     /* This is a new frame. Get a structure from the video_output. */
285     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
286               == NULL )
287     {
288         if( p_vout->b_die || p_vout->b_error )
289         {
290             return;
291         }
292         msleep( VOUT_OUTMEM_SLEEP );
293     }   
294
295     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
296     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
297
298     switch( p_vout->p_sys->i_mode )
299     {
300         case TRANSFORM_MODE_90:
301             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
302             {
303                 int i_pitch = p_pic->p[i_index].i_pitch;
304
305                 u8 *p_in = p_pic->p[i_index].p_pixels;
306
307                 u8 *p_out = p_outpic->p[i_index].p_pixels;
308                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
309                                          * p_outpic->p[i_index].i_pitch;
310
311                 for( ; p_out < p_out_end ; )
312                 {
313                     u8 *p_line_end;
314
315                     p_line_end = p_in + p_pic->p[i_index].i_lines
316                                          * p_pic->p[i_index].i_pitch;
317
318                     for( ; p_in < p_line_end ; )
319                     {
320                         p_line_end -= i_pitch;
321                         *(--p_out_end) = *p_line_end;
322                     }
323
324                     p_in++;
325                 }
326             }
327             break;
328
329         case TRANSFORM_MODE_180:
330             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
331             {
332                 u8 *p_in = p_pic->p[i_index].p_pixels;
333                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
334                                        * p_pic->p[i_index].i_pitch;
335
336                 u8 *p_out = p_outpic->p[i_index].p_pixels;
337
338                 for( ; p_in < p_in_end ; )
339                 {
340                     *p_out++ = *(--p_in_end);
341                 }
342             }
343             break;
344
345         case TRANSFORM_MODE_270:
346             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
347             {
348                 int i_pitch = p_pic->p[i_index].i_pitch;
349
350                 u8 *p_in = p_pic->p[i_index].p_pixels;
351
352                 u8 *p_out = p_outpic->p[i_index].p_pixels;
353                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
354                                          * p_outpic->p[i_index].i_pitch;
355
356                 for( ; p_out < p_out_end ; )
357                 {
358                     u8 *p_in_end;
359
360                     p_in_end = p_in + p_pic->p[i_index].i_lines
361                                        * p_pic->p[i_index].i_pitch;
362
363                     for( ; p_in < p_in_end ; )
364                     {
365                         p_in_end -= i_pitch;
366                         *p_out++ = *p_in_end;
367                     }
368
369                     p_in++;
370                 }
371             }
372             break;
373
374         case TRANSFORM_MODE_VFLIP:
375             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
376             {
377                 u8 *p_in = p_pic->p[i_index].p_pixels;
378                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
379                                        * p_pic->p[i_index].i_pitch;
380
381                 u8 *p_out = p_outpic->p[i_index].p_pixels;
382
383                 for( ; p_in < p_in_end ; )
384                 {
385                     p_in_end -= p_pic->p[i_index].i_pitch;
386                     FAST_MEMCPY( p_out, p_in_end, p_pic->p[i_index].i_pitch );
387                     p_out += p_pic->p[i_index].i_pitch;
388                 }
389             }
390             break;
391
392         case TRANSFORM_MODE_HFLIP:
393             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
394             {
395                 u8 *p_in = p_pic->p[i_index].p_pixels;
396                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
397                                        * p_pic->p[i_index].i_pitch;
398
399                 u8 *p_out = p_outpic->p[i_index].p_pixels;
400
401                 for( ; p_in < p_in_end ; )
402                 {
403                     u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
404
405                     for( ; p_in < p_line_end ; )
406                     {
407                         *p_out++ = *(--p_line_end);
408                     }
409
410                     p_in += p_pic->p[i_index].i_pitch;
411                 }
412             }
413             break;
414
415         default:
416             break;
417     }
418
419     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
420
421     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
422 }
423
424 /*****************************************************************************
425  * vout_Display: displays previously rendered output
426  *****************************************************************************
427  * This function send the currently rendered image to Invert image, waits
428  * until it is displayed and switch the two rendering buffers, preparing next
429  * frame.
430  *****************************************************************************/
431 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
432 {
433     ;
434 }
435