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