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