]> git.sesse.net Git - vlc/blob - modules/video_filter/transform.c
76d1e1eafc4d03c1f58bccf6547887143cedea8d
[vlc] / modules / video_filter / transform.c
1 /*****************************************************************************
2  * transform.c : transform image plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: transform.c,v 1.3 2002/11/23 02:40:30 sam 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 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define TYPE_TEXT N_("transform type")
55 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
56
57 static char *type_list[] = { "90", "180", "270", "hflip", "vflip", NULL };
58
59 vlc_module_begin();
60     add_category_hint( N_("Miscellaneous"), NULL );
61     add_string_from_list( "transform-type", "90", type_list, NULL,
62                           TYPE_TEXT, TYPE_LONGTEXT);
63     set_description( _("image transformation module") );
64     set_capability( "video filter", 0 );
65     add_shortcut( "transform" );
66     set_callbacks( Create, Destroy );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * vout_sys_t: Transform video output method descriptor
71  *****************************************************************************
72  * This structure is part of the video output thread descriptor.
73  * It describes the Transform specific properties of an output thread.
74  *****************************************************************************/
75 struct vout_sys_t
76 {
77     int i_mode;
78     vlc_bool_t b_rotation;
79     vout_thread_t *p_vout;
80 };
81
82 /*****************************************************************************
83  * Create: allocates Transform video thread output method
84  *****************************************************************************
85  * This function allocates and initializes a Transform vout method.
86  *****************************************************************************/
87 static int Create( vlc_object_t *p_this )
88 {
89     vout_thread_t *p_vout = (vout_thread_t *)p_this;
90     char *psz_method;
91
92     /* Allocate structure */
93     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
94     if( p_vout->p_sys == NULL )
95     {
96         msg_Err( p_vout, "out of memory" );
97         return( 1 );
98     }
99
100     p_vout->pf_init = Init;
101     p_vout->pf_end = End;
102     p_vout->pf_manage = NULL;
103     p_vout->pf_render = Render;
104     p_vout->pf_display = NULL;
105
106     /* Look what method was requested */
107     psz_method = config_GetPsz( p_vout, "transform-type" );
108
109     if( psz_method == NULL )
110     {
111         msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
112         msg_Err( p_vout, "no valid transform mode provided, using '90'" );
113         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
114         p_vout->p_sys->b_rotation = 1;
115     }
116     else
117     {
118         if( !strcmp( psz_method, "hflip" ) )
119         {
120             p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
121             p_vout->p_sys->b_rotation = 0;
122         }
123         else if( !strcmp( psz_method, "vflip" ) )
124         {
125             p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
126             p_vout->p_sys->b_rotation = 0;
127         }
128         else if( !strcmp( psz_method, "90" ) )
129         {
130             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
131             p_vout->p_sys->b_rotation = 1;
132         }
133         else if( !strcmp( psz_method, "180" ) )
134         {
135             p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
136             p_vout->p_sys->b_rotation = 0;
137         }
138         else if( !strcmp( psz_method, "270" ) )
139         {
140             p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
141             p_vout->p_sys->b_rotation = 1;
142         }
143         else
144         {
145             msg_Err( p_vout, "no valid transform mode provided, using '90'" );
146             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
147             p_vout->p_sys->b_rotation = 1;
148         }
149
150         free( psz_method );
151     }
152     
153     return( 0 );
154 }
155
156 /*****************************************************************************
157  * Init: initialize Transform video thread output method
158  *****************************************************************************/
159 static int Init( vout_thread_t *p_vout )
160 {
161     int i_index;
162     picture_t *p_pic;
163     
164     I_OUTPUTPICTURES = 0;
165
166     /* Initialize the output structure */
167     p_vout->output.i_chroma = p_vout->render.i_chroma;
168     p_vout->output.i_width  = p_vout->render.i_width;
169     p_vout->output.i_height = p_vout->render.i_height;
170     p_vout->output.i_aspect = p_vout->render.i_aspect;
171
172     /* Try to open the real video output */
173     msg_Dbg( p_vout, "spawning the real video output" );
174
175     if( p_vout->p_sys->b_rotation )
176     {
177         p_vout->p_sys->p_vout =
178             vout_CreateThread( p_vout,
179                            p_vout->render.i_height, p_vout->render.i_width,
180                            p_vout->render.i_chroma,
181                            (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
182                                / (u64)p_vout->render.i_aspect );
183     }
184     else
185     {
186         p_vout->p_sys->p_vout =
187             vout_CreateThread( p_vout,
188                            p_vout->render.i_width, p_vout->render.i_height,
189                            p_vout->render.i_chroma, p_vout->render.i_aspect );
190     }
191
192     /* Everything failed */
193     if( p_vout->p_sys->p_vout == NULL )
194     {
195         msg_Err( p_vout, "cannot open vout, aborting" );
196         return( 0 );
197     }
198  
199     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
200
201     return( 0 );
202 }
203
204 /*****************************************************************************
205  * End: terminate Transform video thread output method
206  *****************************************************************************/
207 static void End( vout_thread_t *p_vout )
208 {
209     int i_index;
210
211     /* Free the fake output buffers we allocated */
212     for( i_index = I_OUTPUTPICTURES ; i_index ; )
213     {
214         i_index--;
215         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
216     }
217 }
218
219 /*****************************************************************************
220  * Destroy: destroy Transform video thread output method
221  *****************************************************************************
222  * Terminate an output method created by TransformCreateOutputMethod
223  *****************************************************************************/
224 static void Destroy( vlc_object_t *p_this )
225 {
226     vout_thread_t *p_vout = (vout_thread_t *)p_this;
227
228     vout_DestroyThread( p_vout->p_sys->p_vout );
229
230     free( p_vout->p_sys );
231 }
232
233 /*****************************************************************************
234  * Render: displays previously rendered output
235  *****************************************************************************
236  * This function send the currently rendered image to Transform image, waits
237  * until it is displayed and switch the two rendering buffers, preparing next
238  * frame.
239  *****************************************************************************/
240 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
241 {
242     picture_t *p_outpic;
243     int i_index;
244
245     /* This is a new frame. Get a structure from the video_output. */
246     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
247               == NULL )
248     {
249         if( p_vout->b_die || p_vout->b_error )
250         {
251             return;
252         }
253         msleep( VOUT_OUTMEM_SLEEP );
254     }   
255
256     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
257     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
258
259     switch( p_vout->p_sys->i_mode )
260     {
261         case TRANSFORM_MODE_90:
262             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
263             {
264                 int i_pitch = p_pic->p[i_index].i_pitch;
265
266                 u8 *p_in = p_pic->p[i_index].p_pixels;
267
268                 u8 *p_out = p_outpic->p[i_index].p_pixels;
269                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
270                                          * p_outpic->p[i_index].i_pitch;
271
272                 for( ; p_out < p_out_end ; )
273                 {
274                     u8 *p_line_end;
275
276                     p_line_end = p_in + p_pic->p[i_index].i_lines
277                                          * p_pic->p[i_index].i_pitch;
278
279                     for( ; p_in < p_line_end ; )
280                     {
281                         p_line_end -= i_pitch;
282                         *(--p_out_end) = *p_line_end;
283                     }
284
285                     p_in++;
286                 }
287             }
288             break;
289
290         case TRANSFORM_MODE_180:
291             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
292             {
293                 u8 *p_in = p_pic->p[i_index].p_pixels;
294                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
295                                        * p_pic->p[i_index].i_pitch;
296
297                 u8 *p_out = p_outpic->p[i_index].p_pixels;
298
299                 for( ; p_in < p_in_end ; )
300                 {
301                     *p_out++ = *(--p_in_end);
302                 }
303             }
304             break;
305
306         case TRANSFORM_MODE_270:
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                 u8 *p_in = p_pic->p[i_index].p_pixels;
312
313                 u8 *p_out = p_outpic->p[i_index].p_pixels;
314                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
315                                          * p_outpic->p[i_index].i_pitch;
316
317                 for( ; p_out < p_out_end ; )
318                 {
319                     u8 *p_in_end;
320
321                     p_in_end = p_in + p_pic->p[i_index].i_lines
322                                        * p_pic->p[i_index].i_pitch;
323
324                     for( ; p_in < p_in_end ; )
325                     {
326                         p_in_end -= i_pitch;
327                         *p_out++ = *p_in_end;
328                     }
329
330                     p_in++;
331                 }
332             }
333             break;
334
335         case TRANSFORM_MODE_VFLIP:
336             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
337             {
338                 u8 *p_in = p_pic->p[i_index].p_pixels;
339                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
340                                        * p_pic->p[i_index].i_pitch;
341
342                 u8 *p_out = p_outpic->p[i_index].p_pixels;
343
344                 for( ; p_in < p_in_end ; )
345                 {
346                     p_in_end -= p_pic->p[i_index].i_pitch;
347                     p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
348                                               p_pic->p[i_index].i_pitch );
349                     p_out += p_pic->p[i_index].i_pitch;
350                 }
351             }
352             break;
353
354         case TRANSFORM_MODE_HFLIP:
355             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
356             {
357                 u8 *p_in = p_pic->p[i_index].p_pixels;
358                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
359                                        * p_pic->p[i_index].i_pitch;
360
361                 u8 *p_out = p_outpic->p[i_index].p_pixels;
362
363                 for( ; p_in < p_in_end ; )
364                 {
365                     u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
366
367                     for( ; p_in < p_line_end ; )
368                     {
369                         *p_out++ = *(--p_line_end);
370                     }
371
372                     p_in += p_pic->p[i_index].i_pitch;
373                 }
374             }
375             break;
376
377         default:
378             break;
379     }
380
381     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
382
383     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
384 }
385