caca.c 9.95 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * caca.c: Color ASCII Art video output plugin using libcaca
 *****************************************************************************
 * Copyright (C) 2003 VideoLAN
5
 * $Id: caca.c,v 1.4 2003/12/04 16:49:43 sam Exp $
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 *
 * Authors: Sam Hocevar <sam@zoy.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <string.h>                                            /* strerror() */

#include <caca.h>

#include <vlc/vlc.h>
#include <vlc/vout.h>
#include <vlc/intf.h>
#include <vlc_keys.h>

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

static int  Init      ( vout_thread_t * );
static void End       ( vout_thread_t * );
static int  Manage    ( vout_thread_t * );
static void Render    ( vout_thread_t *, picture_t * );
static void Display   ( vout_thread_t *, picture_t * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
53
54
55
#define MODE_TEXT N_("dithering mode")
#define MODE_LONGTEXT N_("Choose the libcaca dithering mode")

56
57
58
59
60
61
62
63
64
static char *mode_list[] = { "none",
                             "ordered2",
                             "ordered4",
                             "ordered8",
                             "random" };
static char *mode_list_text[] = { N_("No dithering"),
                                  N_("2x2 ordered dithering"),
                                  N_("4x4 ordered dithering"),
                                  N_("8x8 ordered dithering"),
Sam Hocevar's avatar
Sam Hocevar committed
65
66
                                  N_("Random dithering") };

67
vlc_module_begin();
Sam Hocevar's avatar
Sam Hocevar committed
68
69
70
71
72
    add_category_hint( N_("Dithering"), NULL, VLC_FALSE );
    add_string( "caca-dithering", "ordered", NULL, MODE_TEXT,
                MODE_LONGTEXT, VLC_FALSE );
        change_string_list( mode_list, mode_list_text, 0 );
    set_description( _("colour ASCII art video output") );
73
74
75
76
    set_capability( "video output", 12 );
    set_callbacks( Create, Destroy );
vlc_module_end();

Sam Hocevar's avatar
Sam Hocevar committed
77
78
79
80
81
82
83
84
85
86
87
/*****************************************************************************
 * vout_sys_t: libcaca video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the libcaca specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    struct caca_bitmap *p_bitmap;
};

88
89
90
91
92
93
94
95
/*****************************************************************************
 * Create: allocates libcaca video output thread
 *****************************************************************************
 * This function initializes libcaca vout method.
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    vout_thread_t *p_vout = (vout_thread_t *)p_this;
96
    enum caca_dithering dither = CACA_DITHERING_ORDERED4;
Sam Hocevar's avatar
Sam Hocevar committed
97
98
99
100
101
102
103
104
105
    vlc_value_t val;

    /* Allocate structure */
    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
    if( p_vout->p_sys == NULL )
    {
        msg_Err( p_vout, "out of memory" );
        return VLC_ENOMEM;
    }
106
107
108
109

    if( caca_init() )
    {
        msg_Err( p_vout, "cannot initialize libcaca" );
Sam Hocevar's avatar
Sam Hocevar committed
110
        free( p_vout->p_sys );
111
112
113
        return VLC_EGENERIC;
    }

Sam Hocevar's avatar
Sam Hocevar committed
114
115
116
117
118
119
    var_Create( p_vout, "caca-dithering", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    var_Get( p_vout, "caca-dithering", &val );
    if( val.psz_string )
    {
        if( !strcmp( val.psz_string, "none" ) )
        {
120
121
122
123
124
125
126
127
128
            dither = CACA_DITHERING_NONE;
        }
        else if( !strcmp( val.psz_string, "ordered2" ) )
        {
            dither = CACA_DITHERING_ORDERED2;
        }
        else if( !strcmp( val.psz_string, "ordered4" ) )
        {
            dither = CACA_DITHERING_ORDERED4;
Sam Hocevar's avatar
Sam Hocevar committed
129
130
131
        }
        else if( !strcmp( val.psz_string, "random" ) )
        {
132
            dither = CACA_DITHERING_RANDOM;
Sam Hocevar's avatar
Sam Hocevar committed
133
134
135
136
        }
        free( val.psz_string );
    }
    caca_set_dithering( dither );
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    p_vout->pf_init = Init;
    p_vout->pf_end = End;
    p_vout->pf_manage = Manage;
    p_vout->pf_render = Render;
    p_vout->pf_display = Display;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Init: initialize libcaca video output thread
 *****************************************************************************/
static int Init( vout_thread_t *p_vout )
{
    int i_index;
    picture_t *p_pic = NULL;

    I_OUTPUTPICTURES = 0;

Sam Hocevar's avatar
Sam Hocevar committed
157
    p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
158
159
160
161
162
163
164
165
    p_vout->output.i_width = p_vout->render.i_width;
    p_vout->output.i_height = p_vout->render.i_height;
    p_vout->output.i_aspect = p_vout->render.i_aspect;

    p_vout->output.i_rmask = 0x00ff0000;
    p_vout->output.i_gmask = 0x0000ff00;
    p_vout->output.i_bmask = 0x000000ff;

Sam Hocevar's avatar
Sam Hocevar committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    /* Create the libcaca bitmap */
    p_vout->p_sys->p_bitmap =
        caca_create_bitmap( 32,
                            p_vout->output.i_width,
                            p_vout->output.i_height,
                            4 * ((p_vout->output.i_width + 15) & ~15),
                            p_vout->output.i_rmask,
                            p_vout->output.i_gmask,
                            p_vout->output.i_bmask );
    if( !p_vout->p_sys->p_bitmap )
    {
        msg_Err( p_vout, "could not create libcaca bitmap" );
        return VLC_EGENERIC;
    }

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
    /* Find an empty picture slot */
    for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
    {
        if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
        {
            p_pic = p_vout->p_picture + i_index;
            break;
        }
    }

    if( p_pic == NULL )
    {
        return VLC_EGENERIC;
    }

    /* Allocate the picture */
    p_pic->p->i_lines = p_vout->output.i_height;
    p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
    p_pic->p->i_pixel_pitch = 4;
    p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
    p_pic->i_planes = 1;
    p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );

    p_pic->i_status = DESTROYED_PICTURE;
    p_pic->i_type   = DIRECT_PICTURE;

    PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
    I_OUTPUTPICTURES++;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * End: terminate libcaca video output thread
 *****************************************************************************/
static void End( vout_thread_t *p_vout )
{
Sam Hocevar's avatar
Sam Hocevar committed
218
    caca_free_bitmap( p_vout->p_sys->p_bitmap );
219
220
221
222
223
224
225
226
227
}

/*****************************************************************************
 * Destroy: destroy libcaca video output thread
 *****************************************************************************
 * Terminate an output method created by AaCreateOutputMethod
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
Sam Hocevar's avatar
Sam Hocevar committed
228
229
    vout_thread_t *p_vout = (vout_thread_t *)p_this;

230
    caca_end();
Sam Hocevar's avatar
Sam Hocevar committed
231
    free( p_vout->p_sys );
232
233
234
235
236
237
238
239
240
241
}

/*****************************************************************************
 * Manage: handle libcaca events
 *****************************************************************************
 * This function should be called regularly by video output thread. It manages
 * console events. It returns a non null value on error.
 *****************************************************************************/
static int Manage( vout_thread_t *p_vout )
{
Sam Hocevar's avatar
Sam Hocevar committed
242
    int event;
243
244
    vlc_value_t val;

Sam Hocevar's avatar
Sam Hocevar committed
245
    while(( event = caca_get_event() ))
246
    {
Sam Hocevar's avatar
Sam Hocevar committed
247
        if( event & CACA_EVENT_KEY_PRESS )
248
        {
Sam Hocevar's avatar
Sam Hocevar committed
249
250
251
252
253
254
255
256
257
258
259
            switch( event & 0xffff )
            {
            case 'q':
                val.i_int = KEY_MODIFIER_CTRL | 'q';
                break;
            case ' ':
                val.i_int = KEY_SPACE;
                break;
            default:
                continue;
            }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
        }

        var_Set( p_vout->p_vlc, "key-pressed", val );
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Render: render previously calculated output
 *****************************************************************************/
static void Render( vout_thread_t *p_vout, picture_t *p_pic )
{
    caca_clear();
274
275
    caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
                      p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
276
277
278
279
280
281
282
283
284
285
}

/*****************************************************************************
 * Display: displays previously rendered output
 *****************************************************************************/
static void Display( vout_thread_t *p_vout, picture_t *p_pic )
{
    caca_refresh();
}