]> git.sesse.net Git - vlc/blob - modules/video_filter/transform.c
1b49e2de725af7655b83d18c2de38d033600ff5d
[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.5 2003/01/09 14:00:00 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 = vout_Create( p_vout,
178                            p_vout->render.i_height, p_vout->render.i_width,
179                            p_vout->render.i_chroma,
180                            (uint64_t)VOUT_ASPECT_FACTOR
181                             * (uint64_t)VOUT_ASPECT_FACTOR
182                             / (uint64_t)p_vout->render.i_aspect );
183     }
184     else
185     {
186         p_vout->p_sys->p_vout = vout_Create( p_vout,
187                            p_vout->render.i_width, p_vout->render.i_height,
188                            p_vout->render.i_chroma, p_vout->render.i_aspect );
189     }
190
191     /* Everything failed */
192     if( p_vout->p_sys->p_vout == NULL )
193     {
194         msg_Err( p_vout, "cannot open vout, aborting" );
195         return( 0 );
196     }
197
198     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
199
200     return( 0 );
201 }
202
203 /*****************************************************************************
204  * End: terminate Transform video thread output method
205  *****************************************************************************/
206 static void End( vout_thread_t *p_vout )
207 {
208     int i_index;
209
210     /* Free the fake output buffers we allocated */
211     for( i_index = I_OUTPUTPICTURES ; i_index ; )
212     {
213         i_index--;
214         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
215     }
216 }
217
218 /*****************************************************************************
219  * Destroy: destroy Transform video thread output method
220  *****************************************************************************
221  * Terminate an output method created by TransformCreateOutputMethod
222  *****************************************************************************/
223 static void Destroy( vlc_object_t *p_this )
224 {
225     vout_thread_t *p_vout = (vout_thread_t *)p_this;
226
227     vout_Destroy( p_vout->p_sys->p_vout );
228
229     free( p_vout->p_sys );
230 }
231
232 /*****************************************************************************
233  * Render: displays previously rendered output
234  *****************************************************************************
235  * This function send the currently rendered image to Transform image, waits
236  * until it is displayed and switch the two rendering buffers, preparing next
237  * frame.
238  *****************************************************************************/
239 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
240 {
241     picture_t *p_outpic;
242     int i_index;
243
244     /* This is a new frame. Get a structure from the video_output. */
245     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
246               == NULL )
247     {
248         if( p_vout->b_die || p_vout->b_error )
249         {
250             return;
251         }
252         msleep( VOUT_OUTMEM_SLEEP );
253     }
254
255     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
256     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
257
258     switch( p_vout->p_sys->i_mode )
259     {
260         case TRANSFORM_MODE_90:
261             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
262             {
263                 int i_pitch = p_pic->p[i_index].i_pitch;
264
265                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
266
267                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
268                 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
269                                               * p_outpic->p[i_index].i_pitch;
270
271                 for( ; p_out < p_out_end ; )
272                 {
273                     uint8_t *p_line_end;
274
275                     p_out_end -= p_outpic->p[i_index].i_pitch
276                                   - p_outpic->p[i_index].i_visible_pitch;
277                     p_line_end = p_in + p_pic->p[i_index].i_lines * 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             /* FIXME: we should use i_visible_pitch here */
292             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
293             {
294                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
295                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
296                                             * p_pic->p[i_index].i_pitch;
297
298                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
299
300                 for( ; p_in < p_in_end ; )
301                 {
302                     *p_out++ = *(--p_in_end);
303                 }
304             }
305             break;
306
307         case TRANSFORM_MODE_270:
308             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
309             {
310                 int i_pitch = p_pic->p[i_index].i_pitch;
311
312                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
313
314                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
315                 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
316                                               * p_outpic->p[i_index].i_pitch;
317
318                 for( ; p_out < p_out_end ; )
319                 {
320                     uint8_t *p_in_end;
321
322                     p_in_end = p_in + p_pic->p[i_index].i_lines * 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_out += p_outpic->p[i_index].i_pitch
331                               - p_outpic->p[i_index].i_visible_pitch;
332                     p_in++;
333                 }
334             }
335             break;
336
337         case TRANSFORM_MODE_VFLIP:
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_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                     p_in_end -= p_pic->p[i_index].i_pitch;
349                     p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
350                                            p_pic->p[i_index].i_visible_pitch );
351                     p_out += p_pic->p[i_index].i_pitch;
352                 }
353             }
354             break;
355
356         case TRANSFORM_MODE_HFLIP:
357             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
358             {
359                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
360                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
361                                             * p_pic->p[i_index].i_pitch;
362
363                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
364
365                 for( ; p_in < p_in_end ; )
366                 {
367                     uint8_t *p_line_end = p_in
368                                            + p_pic->p[i_index].i_visible_pitch;
369
370                     for( ; p_in < p_line_end ; )
371                     {
372                         *p_out++ = *(--p_line_end);
373                     }
374
375                     p_in += p_pic->p[i_index].i_pitch;
376                 }
377             }
378             break;
379
380         default:
381             break;
382     }
383
384     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
385
386     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
387 }
388