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