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