* modules/video_filter/*: forward fullscreen event between children and parent.
[vlc.git] / modules / video_filter / transform.c
1 /*****************************************************************************
2  * transform.c : transform image plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001, 2002, 2003 VideoLAN
5  * $Id: transform.c,v 1.14 2003/10/15 22:49:48 gbazin 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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "filter_common.h"
34
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
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
47 static int  Init      ( vout_thread_t * );
48 static void End       ( vout_thread_t * );
49 static void Render    ( vout_thread_t *, picture_t * );
50
51 static int  SendEvents( vlc_object_t *, char const *,
52                         vlc_value_t, vlc_value_t, void * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define TYPE_TEXT N_("Transform type")
58 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
59
60 static char *type_list[] = { "90", "180", "270", "hflip", "vflip", NULL };
61
62 vlc_module_begin();
63     add_category_hint( N_("Miscellaneous"), NULL, VLC_FALSE );
64     add_string_from_list( "transform-type", "90", type_list, NULL,
65                           TYPE_TEXT, TYPE_LONGTEXT, VLC_FALSE);
66     set_description( _("video transformation filter") );
67     set_capability( "video filter", 0 );
68     add_shortcut( "transform" );
69     set_callbacks( Create, Destroy );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * vout_sys_t: Transform video output method descriptor
74  *****************************************************************************
75  * This structure is part of the video output thread descriptor.
76  * It describes the Transform specific properties of an output thread.
77  *****************************************************************************/
78 struct vout_sys_t
79 {
80     int i_mode;
81     vlc_bool_t b_rotation;
82     vout_thread_t *p_vout;
83 };
84
85 /*****************************************************************************
86  * Create: allocates Transform video thread output method
87  *****************************************************************************
88  * This function allocates and initializes a Transform vout method.
89  *****************************************************************************/
90 static int Create( vlc_object_t *p_this )
91 {
92     vout_thread_t *p_vout = (vout_thread_t *)p_this;
93     char *psz_method;
94
95     /* Allocate structure */
96     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
97     if( p_vout->p_sys == NULL )
98     {
99         msg_Err( p_vout, "out of memory" );
100         return VLC_ENOMEM;
101     }
102
103     p_vout->pf_init = Init;
104     p_vout->pf_end = End;
105     p_vout->pf_manage = NULL;
106     p_vout->pf_render = Render;
107     p_vout->pf_display = NULL;
108
109     /* Look what method was requested */
110     psz_method = config_GetPsz( p_vout, "transform-type" );
111
112     if( psz_method == NULL )
113     {
114         msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
115         msg_Err( p_vout, "no valid transform mode provided, using '90'" );
116         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
117         p_vout->p_sys->b_rotation = 1;
118     }
119     else
120     {
121         if( !strcmp( psz_method, "hflip" ) )
122         {
123             p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
124             p_vout->p_sys->b_rotation = 0;
125         }
126         else if( !strcmp( psz_method, "vflip" ) )
127         {
128             p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
129             p_vout->p_sys->b_rotation = 0;
130         }
131         else if( !strcmp( psz_method, "90" ) )
132         {
133             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
134             p_vout->p_sys->b_rotation = 1;
135         }
136         else if( !strcmp( psz_method, "180" ) )
137         {
138             p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
139             p_vout->p_sys->b_rotation = 0;
140         }
141         else if( !strcmp( psz_method, "270" ) )
142         {
143             p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
144             p_vout->p_sys->b_rotation = 1;
145         }
146         else
147         {
148             msg_Err( p_vout, "no valid transform mode provided, using '90'" );
149             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
150             p_vout->p_sys->b_rotation = 1;
151         }
152
153         free( psz_method );
154     }
155
156     return VLC_SUCCESS;
157 }
158
159 /*****************************************************************************
160  * Init: initialize Transform video thread output method
161  *****************************************************************************/
162 static int Init( vout_thread_t *p_vout )
163 {
164     int i_index;
165     picture_t *p_pic;
166
167     I_OUTPUTPICTURES = 0;
168
169     /* Initialize the output structure */
170     p_vout->output.i_chroma = p_vout->render.i_chroma;
171     p_vout->output.i_width  = p_vout->render.i_width;
172     p_vout->output.i_height = p_vout->render.i_height;
173     p_vout->output.i_aspect = p_vout->render.i_aspect;
174
175     /* Try to open the real video output */
176     msg_Dbg( p_vout, "spawning the real video output" );
177
178     if( p_vout->p_sys->b_rotation )
179     {
180         p_vout->p_sys->p_vout = vout_Create( p_vout,
181                            p_vout->render.i_height, p_vout->render.i_width,
182                            p_vout->render.i_chroma,
183                            (uint64_t)VOUT_ASPECT_FACTOR
184                             * (uint64_t)VOUT_ASPECT_FACTOR
185                             / (uint64_t)p_vout->render.i_aspect );
186     }
187     else
188     {
189         p_vout->p_sys->p_vout = vout_Create( p_vout,
190                            p_vout->render.i_width, p_vout->render.i_height,
191                            p_vout->render.i_chroma, p_vout->render.i_aspect );
192     }
193
194     /* Everything failed */
195     if( p_vout->p_sys->p_vout == NULL )
196     {
197         msg_Err( p_vout, "cannot open vout, aborting" );
198         return VLC_EGENERIC;
199     }
200
201     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
202
203     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
204
205     ADD_PARENT_CALLBACKS( SendEventsToChild );
206
207     return VLC_SUCCESS;
208 }
209
210 /*****************************************************************************
211  * End: terminate Transform video thread output method
212  *****************************************************************************/
213 static void End( vout_thread_t *p_vout )
214 {
215     int i_index;
216
217     /* Free the fake output buffers we allocated */
218     for( i_index = I_OUTPUTPICTURES ; i_index ; )
219     {
220         i_index--;
221         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
222     }
223 }
224
225 /*****************************************************************************
226  * Destroy: destroy Transform video thread output method
227  *****************************************************************************
228  * Terminate an output method created by TransformCreateOutputMethod
229  *****************************************************************************/
230 static void Destroy( vlc_object_t *p_this )
231 {
232     vout_thread_t *p_vout = (vout_thread_t *)p_this;
233
234     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
235     vlc_object_detach( p_vout->p_sys->p_vout );
236     vout_Destroy( p_vout->p_sys->p_vout );
237
238     DEL_PARENT_CALLBACKS( SendEventsToChild );
239
240     free( p_vout->p_sys );
241 }
242
243 /*****************************************************************************
244  * Render: displays previously rendered output
245  *****************************************************************************
246  * This function send the currently rendered image to Transform image, waits
247  * until it is displayed and switch the two rendering buffers, preparing next
248  * frame.
249  *****************************************************************************/
250 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
251 {
252     picture_t *p_outpic;
253     int i_index;
254
255     /* This is a new frame. Get a structure from the video_output. */
256     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
257               == NULL )
258     {
259         if( p_vout->b_die || p_vout->b_error )
260         {
261             return;
262         }
263         msleep( VOUT_OUTMEM_SLEEP );
264     }
265
266     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
267     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
268
269     switch( p_vout->p_sys->i_mode )
270     {
271         case TRANSFORM_MODE_90:
272             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
273             {
274                 int i_pitch = p_pic->p[i_index].i_pitch;
275
276                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
277
278                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
279                 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
280                                               * p_outpic->p[i_index].i_pitch;
281
282                 for( ; p_out < p_out_end ; )
283                 {
284                     uint8_t *p_line_end;
285
286                     p_out_end -= p_outpic->p[i_index].i_pitch
287                                   - p_outpic->p[i_index].i_visible_pitch;
288                     p_line_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
289
290                     for( ; p_in < p_line_end ; )
291                     {
292                         p_line_end -= i_pitch;
293                         *(--p_out_end) = *p_line_end;
294                     }
295
296                     p_in++;
297                 }
298             }
299             break;
300
301         case TRANSFORM_MODE_180:
302             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
303             {
304                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
305                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
306                                             * p_pic->p[i_index].i_pitch;
307
308                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
309
310                 for( ; p_in < p_in_end ; )
311                 {
312                     uint8_t *p_line_start = p_in_end
313                                              - p_pic->p[i_index].i_pitch;
314                     p_in_end -= p_pic->p[i_index].i_pitch
315                                  - p_pic->p[i_index].i_visible_pitch;
316
317                     for( ; p_line_start < p_in_end ; )
318                     {
319                         *p_out++ = *(--p_in_end);
320                     }
321
322                     p_out += p_outpic->p[i_index].i_pitch
323                               - p_outpic->p[i_index].i_visible_pitch;
324                 }
325             }
326             break;
327
328         case TRANSFORM_MODE_270:
329             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
330             {
331                 int i_pitch = p_pic->p[i_index].i_pitch;
332
333                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
334
335                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
336                 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
337                                               * p_outpic->p[i_index].i_pitch;
338
339                 for( ; p_out < p_out_end ; )
340                 {
341                     uint8_t *p_in_end;
342
343                     p_in_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
344
345                     for( ; p_in < p_in_end ; )
346                     {
347                         p_in_end -= i_pitch;
348                         *p_out++ = *p_in_end;
349                     }
350
351                     p_out += p_outpic->p[i_index].i_pitch
352                               - p_outpic->p[i_index].i_visible_pitch;
353                     p_in++;
354                 }
355             }
356             break;
357
358         case TRANSFORM_MODE_HFLIP:
359             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
360             {
361                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
362                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
363                                             * p_pic->p[i_index].i_pitch;
364
365                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
366
367                 for( ; p_in < p_in_end ; )
368                 {
369                     p_in_end -= p_pic->p[i_index].i_pitch;
370                     p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
371                                            p_pic->p[i_index].i_visible_pitch );
372                     p_out += p_pic->p[i_index].i_pitch;
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                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
381                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
382                                             * p_pic->p[i_index].i_pitch;
383
384                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
385
386                 for( ; p_in < p_in_end ; )
387                 {
388                     uint8_t *p_line_end = p_in
389                                            + p_pic->p[i_index].i_visible_pitch;
390
391                     for( ; p_in < p_line_end ; )
392                     {
393                         *p_out++ = *(--p_line_end);
394                     }
395
396                     p_in += p_pic->p[i_index].i_pitch;
397                 }
398             }
399             break;
400
401         default:
402             break;
403     }
404
405     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
406
407     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
408 }
409
410 /*****************************************************************************
411  * SendEvents: forward mouse and keyboard events to the parent p_vout
412  *****************************************************************************/
413 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
414                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
415 {
416     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
417     vlc_value_t sentval = newval;
418
419     /* Translate the mouse coordinates */
420     if( !strcmp( psz_var, "mouse-x" ) )
421     {
422         switch( p_vout->p_sys->i_mode )
423         {
424         case TRANSFORM_MODE_270:
425             sentval.i_int = p_vout->p_sys->p_vout->output.i_width
426                              - sentval.i_int;
427         case TRANSFORM_MODE_90:
428             var_Set( p_vout, "mouse-y", sentval );
429             return VLC_SUCCESS;
430
431         case TRANSFORM_MODE_180:
432         case TRANSFORM_MODE_HFLIP:
433             sentval.i_int = p_vout->p_sys->p_vout->output.i_width
434                              - sentval.i_int;
435             break;
436
437         case TRANSFORM_MODE_VFLIP:
438         default:
439             break;
440         }
441     }
442     else if( !strcmp( psz_var, "mouse-y" ) )
443     {
444         switch( p_vout->p_sys->i_mode )
445         {
446         case TRANSFORM_MODE_90:
447             sentval.i_int = p_vout->p_sys->p_vout->output.i_height
448                              - sentval.i_int;
449         case TRANSFORM_MODE_270:
450             var_Set( p_vout, "mouse-x", sentval );
451             return VLC_SUCCESS;
452
453         case TRANSFORM_MODE_180:
454         case TRANSFORM_MODE_VFLIP:
455             sentval.i_int = p_vout->p_sys->p_vout->output.i_height
456                              - sentval.i_int;
457             break;
458
459         case TRANSFORM_MODE_HFLIP:
460         default:
461             break;
462         }
463     }
464
465     var_Set( p_vout, psz_var, sentval );
466
467     return VLC_SUCCESS;
468 }
469
470 /*****************************************************************************
471  * SendEventsToChild: forward events to the child/children vout
472  *****************************************************************************/
473 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
474                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
475 {
476     vout_thread_t *p_vout = (vout_thread_t *)p_this;
477     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
478     return VLC_SUCCESS;
479 }