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