]> git.sesse.net Git - vlc/blob - plugins/aa/aa.c
* ./plugins/aa: aalib output plugin courtesy of Sigmund Augdal.
[vlc] / plugins / aa / 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.1 2002/03/19 14:00:50 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 <videolan/vlc.h>
34
35 #include "video.h"
36 #include "video_output.h"
37
38 #define AA_MAX_DIRECTBUFFERS 1
39
40 /*****************************************************************************
41  * Capabilities defined in the other files.
42  *****************************************************************************/
43 static void vout_getfunctions  ( function_list_t * p_function_list );
44
45 /*****************************************************************************
46  * Build configuration tree.
47  *****************************************************************************/
48 MODULE_CONFIG_START
49 MODULE_CONFIG_STOP
50
51
52 MODULE_INIT_START
53     SET_DESCRIPTION( "ASCII-art video output module" )
54     ADD_CAPABILITY( VOUT, 10 )
55     ADD_SHORTCUT( "aa" )
56     ADD_SHORTCUT( "aalib" )
57 MODULE_INIT_STOP
58
59
60 MODULE_ACTIVATE_START
61     vout_getfunctions( &p_module->p_functions->vout );
62 MODULE_ACTIVATE_STOP
63
64
65 MODULE_DEACTIVATE_START
66 MODULE_DEACTIVATE_STOP
67
68 /*****************************************************************************
69  * vout_sys_t: aa video output method descriptor
70  *****************************************************************************
71  * This structure is part of the video output thread descriptor.
72  * It describes the aa specific properties of an output thread.
73  *****************************************************************************/
74 typedef struct vout_sys_s
75 {
76     struct aa_context*  aa_context;
77     aa_palette          palette;
78     int                 i_width;                     /* width of main window */
79     int                 i_height;                   /* height of main window */
80
81 } vout_sys_t;
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static int  vout_Create    ( struct vout_thread_s * );
87 static int  vout_Init      ( struct vout_thread_s * );
88 static void vout_End       ( struct vout_thread_s * );
89 static void vout_Destroy   ( struct vout_thread_s * );
90 static int  vout_Manage    ( struct vout_thread_s * );
91 static void vout_Render    ( struct vout_thread_s *, struct picture_s * );
92 static void vout_Display   ( struct vout_thread_s *, struct picture_s * );
93
94 static int  NewPicture     ( struct vout_thread_s *, struct picture_s * );
95 static void SetPalette     ( struct vout_thread_s *, u16 *, u16 *, u16 * );
96
97 /*****************************************************************************
98  * Functions exported as capabilities. They are declared as static so that
99  * we don't pollute the namespace too much.
100  *****************************************************************************/
101 static void vout_getfunctions( function_list_t * p_function_list )
102 {
103     p_function_list->functions.vout.pf_create     = vout_Create;
104     p_function_list->functions.vout.pf_init       = vout_Init;
105     p_function_list->functions.vout.pf_end        = vout_End;
106     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
107     p_function_list->functions.vout.pf_manage     = vout_Manage;
108     p_function_list->functions.vout.pf_render     = vout_Render;
109     p_function_list->functions.vout.pf_display    = vout_Display;
110 }
111
112 /*****************************************************************************
113  * vout_Create: allocates aa video thread output method
114  *****************************************************************************
115  * This function allocates and initializes a aa vout method.
116  *****************************************************************************/
117 static int vout_Create( vout_thread_t *p_vout )
118 {
119     /* Allocate structure */
120     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
121     if( p_vout->p_sys == NULL )
122     {
123         intf_ErrMsg("error: %s", strerror(ENOMEM) );
124         return( 1 );
125     }
126
127     if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams))) {
128         intf_ErrMsg("error: Cannot initialize AA-lib. Sorry");
129         return( 1 );
130     }
131     p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
132     p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
133
134     return( 0 );
135 }
136
137 /*****************************************************************************
138  * vout_Init: initialize aa video thread output method
139  *****************************************************************************/
140 static int vout_Init( vout_thread_t *p_vout )
141 {
142     int i_index;
143     picture_t *p_pic;
144     
145     I_OUTPUTPICTURES = 0;
146
147     p_vout->output.i_chroma = FOURCC_RGB2;
148     p_vout->output.i_width = p_vout->p_sys->i_width;
149     p_vout->output.i_height = p_vout->p_sys->i_height;
150     p_vout->output.i_aspect = p_vout->p_sys->i_width
151                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
152     p_vout->output.pf_setpalette = SetPalette;
153
154
155     /* Try to initialize AA_MAX_DIRECTBUFFERS direct buffers */
156     while( I_OUTPUTPICTURES < AA_MAX_DIRECTBUFFERS )
157     {
158         p_pic = NULL;
159
160         /* Find an empty picture slot */
161         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
162         {
163             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
164             {
165                 p_pic = p_vout->p_picture + i_index;
166                 break;
167             }
168         }
169
170         /* Allocate the picture */
171         if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
172         {
173             break;
174         }
175
176         p_pic->i_status = DESTROYED_PICTURE;
177         p_pic->i_type   = DIRECT_PICTURE;
178     
179         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
180
181         I_OUTPUTPICTURES++;
182     }
183
184     return( 0 );
185 }
186
187 /*****************************************************************************
188  * vout_End: terminate aa video thread output method
189  *****************************************************************************/
190 static void vout_End( vout_thread_t *p_vout )
191 {
192     ;
193 }
194
195 /*****************************************************************************
196  * vout_Destroy: destroy aa video thread output method
197  *****************************************************************************
198  * Terminate an output method created by AaCreateOutputMethod
199  *****************************************************************************/
200 static void vout_Destroy( vout_thread_t *p_vout )
201 {
202     aa_close(p_vout->p_sys->aa_context);
203     free( p_vout->p_sys );
204 }
205
206 /*****************************************************************************
207  * vout_Manage: handle aa events
208  *****************************************************************************
209  * This function should be called regularly by video output thread. It manages
210  * console events. It returns a non null value on error.
211  *****************************************************************************/
212 static int vout_Manage( vout_thread_t *p_vout )
213 {
214     return( 0 );
215 }
216
217 /*****************************************************************************
218  * vout_Render: render previously calculated output
219  *****************************************************************************/
220 static void vout_Render( vout_thread_t *p_vout, picture_t *p_pic )
221 {
222     ;
223 }
224
225 /*****************************************************************************
226  * vout_Display: displays previously rendered output
227  *****************************************************************************/
228 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
229 {
230     /* No need to do anything, the fake direct buffers stay as they are */
231     int i_width, i_height, i_x, i_y;
232
233     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
234                        &i_x, &i_y, &i_width, &i_height );
235     //    p_vout->p_sys->aa_context->imagebuffer = p_pic->p_data;
236     aa_fastrender(p_vout->p_sys->aa_context, 0, 0, aa_scrwidth(p_vout->p_sys->aa_context),
237                      aa_scrheight(p_vout->p_sys->aa_context));
238     aa_flush(p_vout->p_sys->aa_context);
239
240 }
241
242 /*****************************************************************************
243  * NewPicture: allocate a picture
244  *****************************************************************************
245  * Returns 0 on success, -1 otherwise
246  *****************************************************************************/
247 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
248 {
249     p_pic->p->p_pixels = aa_image(p_vout->p_sys->aa_context);    
250     p_pic->p->i_pixel_bytes = 1;
251     p_pic->p->i_lines = p_vout->p_sys->i_height;
252     p_pic->p->i_pitch = p_vout->p_sys->i_width;
253     p_pic->p->b_margin = 0;
254     p_pic->i_planes = 1;
255
256     return( 0 );
257 }
258
259 /*****************************************************************************
260  * SetPalette: set the 8bpp palette
261  *****************************************************************************/
262 static void SetPalette( vout_thread_t *p_vout, u16 *red, u16 *green, u16 *blue )
263 {
264     int i;
265   
266     /* Fill colors with color information */
267     for( i = 0; i < 256; i++ )
268     {
269         aa_setpalette( p_vout->p_sys->palette, 256 -i,
270                        red[ i ], green[ i ], blue[ i ] );
271     }
272 }
273