]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
A bit of headers cleanup
[vlc] / modules / video_output / aa.c
1 /*****************************************************************************
2  * vout_aa.c: Aa video output display method for testing purposes
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.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 <errno.h>                                                 /* ENOMEM */
28 #include <stdlib.h>                                                /* free() */
29 #include <string.h>                                            /* strerror() */
30
31 #include <aalib.h>
32
33 #include <vlc/vlc.h>
34 #include <vlc_vout.h>
35 #include <vlc_interface.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static int  Init      ( vout_thread_t * );
44 static void End       ( vout_thread_t * );
45 static int  Manage    ( vout_thread_t * );
46 static void Render    ( vout_thread_t *, picture_t * );
47 static void Display   ( vout_thread_t *, picture_t * );
48
49 static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 vlc_module_begin();
55     set_shortname( _("ASCII Art"));
56     set_category( CAT_VIDEO );
57     set_subcategory( SUBCAT_VIDEO_VOUT );
58     set_description( _("ASCII-art video output") );
59     set_capability( "video output", 10 );
60     add_shortcut( "aalib" );
61     set_callbacks( Create, Destroy );
62 vlc_module_end();
63
64 /*****************************************************************************
65  * vout_sys_t: aa video output method descriptor
66  *****************************************************************************
67  * This structure is part of the video output thread descriptor.
68  * It describes the aa specific properties of an output thread.
69  *****************************************************************************/
70 struct vout_sys_t
71 {
72     struct aa_context*  aa_context;
73     aa_palette          palette;
74     int                 i_width;                     /* width of main window */
75     int                 i_height;                   /* height of main window */
76 };
77
78 /*****************************************************************************
79  * Create: allocates aa video thread output method
80  *****************************************************************************
81  * This function allocates and initializes a aa vout method.
82  *****************************************************************************/
83 static int Create( vlc_object_t *p_this )
84 {
85     vout_thread_t *p_vout = (vout_thread_t *)p_this;
86
87     /* Allocate structure */
88     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
89     if( p_vout->p_sys == NULL )
90     {
91         msg_Err( p_vout, "out of memory" );
92         return( 1 );
93     }
94
95     /* Don't parse any options, but take $AAOPTS into account */
96     aa_parseoptions( NULL, NULL, NULL, NULL );
97
98     if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
99     {
100         msg_Err( p_vout, "cannot initialize aalib" );
101         return( 1 );
102     }
103
104     p_vout->pf_init = Init;
105     p_vout->pf_end = End;
106     p_vout->pf_manage = Manage;
107     p_vout->pf_render = Render;
108     p_vout->pf_display = Display;
109
110     p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
111     p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
112     aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
113     aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
114     aa_hidemouse( p_vout->p_sys->aa_context );
115     return( 0 );
116 }
117
118 /*****************************************************************************
119  * Init: initialize aa video thread output method
120  *****************************************************************************/
121 static int Init( vout_thread_t *p_vout )
122 {
123     int i_index;
124     picture_t *p_pic = NULL;
125
126     I_OUTPUTPICTURES = 0;
127
128     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
129     p_vout->output.i_width = p_vout->p_sys->i_width;
130     p_vout->output.i_height = p_vout->p_sys->i_height;
131     p_vout->output.i_aspect = p_vout->p_sys->i_width
132                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
133     p_vout->output.pf_setpalette = SetPalette;
134
135     /* Find an empty picture slot */
136     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
137     {
138         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
139         {
140             p_pic = p_vout->p_picture + i_index;
141             break;
142         }
143     }
144
145     if( p_pic == NULL )
146     {
147         return -1;
148     }
149
150     /* Allocate the picture */
151     p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
152     p_pic->p->i_lines = p_vout->p_sys->i_height;
153     p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
154     p_pic->p->i_pitch = p_vout->p_sys->i_width;
155     p_pic->p->i_pixel_pitch = 1;
156     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
157     p_pic->i_planes = 1;
158
159     p_pic->i_status = DESTROYED_PICTURE;
160     p_pic->i_type   = DIRECT_PICTURE;
161
162     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
163     I_OUTPUTPICTURES++;
164
165     return 0;
166 }
167
168 /*****************************************************************************
169  * End: terminate aa video thread output method
170  *****************************************************************************/
171 static void End( vout_thread_t *p_vout )
172 {
173     ;
174 }
175
176 /*****************************************************************************
177  * Destroy: destroy aa video thread output method
178  *****************************************************************************
179  * Terminate an output method created by AaCreateOutputMethod
180  *****************************************************************************/
181 static void Destroy( vlc_object_t *p_this )
182 {
183     vout_thread_t *p_vout = (vout_thread_t *)p_this;
184
185     aa_close( p_vout->p_sys->aa_context );
186     free( p_vout->p_sys );
187 }
188
189 /*****************************************************************************
190  * Manage: handle aa events
191  *****************************************************************************
192  * This function should be called regularly by video output thread. It manages
193  * console events. It returns a non null value on error.
194  *****************************************************************************/
195 static int Manage( vout_thread_t *p_vout )
196 {
197     int event, x, y, b;
198     event = aa_getevent( p_vout->p_sys->aa_context, 0 );
199     switch ( event )
200     {
201     case AA_MOUSE:
202         aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
203         if ( b & AA_BUTTON3 )
204         {
205             intf_thread_t *p_intf;
206             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
207             if( p_intf )
208             {
209                 p_intf->b_menu_change = 1;
210                 vlc_object_release( p_intf );
211             }
212         }
213         break;
214     case AA_RESIZE:
215         p_vout->i_changes |= VOUT_SIZE_CHANGE;
216         aa_resize( p_vout->p_sys->aa_context );
217         p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
218         p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
219         break;
220     default:
221         break;
222     }
223     return( 0 );
224 }
225
226 /*****************************************************************************
227  * Render: render previously calculated output
228  *****************************************************************************/
229 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
230 {
231   aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
232                  aa_imgwidth( p_vout->p_sys->aa_context ),
233                  aa_imgheight( p_vout->p_sys->aa_context ) );
234 }
235
236 /*****************************************************************************
237  * Display: displays previously rendered output
238  *****************************************************************************/
239 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
240 {
241     /* No need to do anything, the fake direct buffers stay as they are */
242     int i_width, i_height, i_x, i_y;
243
244     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
245                        &i_x, &i_y, &i_width, &i_height );
246
247     aa_flush(p_vout->p_sys->aa_context);
248 }
249
250 /*****************************************************************************
251  * SetPalette: set the 8bpp palette
252  *****************************************************************************/
253 static void SetPalette( vout_thread_t *p_vout,
254                         uint16_t *red, uint16_t *green, uint16_t *blue )
255 {
256     int i;
257
258     /* Fill colors with color information */
259     for( i = 0; i < 256; i++ )
260     {
261         aa_setpalette( p_vout->p_sys->palette, 256 -i,
262                        red[ i ], green[ i ], blue[ i ] );
263     }
264 }
265