au.c 12.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * au.c : au file input module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001-2007 VLC authors and VideoLAN
5
 * $Id$
6
 *
7 8
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <limits.h>
33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
35
#include <vlc_demux.h>
36

37 38 39 40
/* TODO:
 *  - all adpcm things (I _NEED_ samples)
 *  - ...
 */
41 42 43 44

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
45 46
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
47

48 49 50 51 52 53 54 55
vlc_module_begin ()
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_description( N_("AU demuxer") )
    set_capability( "demux", 10 )
    set_callbacks( Open, Close )
    add_shortcut( "au" )
vlc_module_end ()
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
enum AuType_e
{
    AU_UNKNOWN      =  0,
    AU_MULAW_8      =  1,  /* 8-bit ISDN u-law */
    AU_LINEAR_8     =  2,  /* 8-bit linear PCM */
    AU_LINEAR_16    =  3,  /* 16-bit linear PCM */
    AU_LINEAR_24    =  4,  /* 24-bit linear PCM */
    AU_LINEAR_32    =  5,  /* 32-bit linear PCM */
    AU_FLOAT        =  6,  /* 32-bit IEEE floating point */
    AU_DOUBLE       =  7,  /* 64-bit IEEE floating point */
    AU_ADPCM_G721   =  23, /* 4-bit CCITT g.721 ADPCM */
    AU_ADPCM_G722   =  24, /* CCITT g.722 ADPCM */
    AU_ADPCM_G723_3 =  25, /* CCITT g.723 3-bit ADPCM */
    AU_ADPCM_G723_5 =  26, /* CCITT g.723 5-bit ADPCM */
    AU_ALAW_8       =  27  /* 8-bit ISDN A-law */
};
76

77 78 79 80 81 82
enum AuCat_e
{
    AU_CAT_UNKNOWN  = 0,
    AU_CAT_PCM      = 1,
    AU_CAT_ADPCM    = 2
};
83 84 85

struct demux_sys_t
{
86 87
    es_format_t     fmt;
    es_out_id_t     *es;
88 89 90

    mtime_t         i_time;

91 92
    int             i_frame_size;
    mtime_t         i_frame_length;
93

94
    uint32_t        i_header_size;
95 96
};

97
static int Demux( demux_t * );
98
static int Control ( demux_t *, int i_query, va_list args );
99

100
/*****************************************************************************
101
 * Open: check file and initializes structures
102
 *****************************************************************************/
103
static int Open( vlc_object_t *p_this )
104
{
105 106
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
107

108
    uint8_t      hdr[20];
109
    const uint8_t *p_peek;
110
    int          i_cat;
111

112
    if( vlc_stream_Peek( p_demux->s , &p_peek, 4 ) < 4 )
Laurent Aimar's avatar
Laurent Aimar committed
113
        return VLC_EGENERIC;
114

Clément Stenac's avatar
Clément Stenac committed
115
    if( memcmp( p_peek, ".snd", 4 ) )
116
        return VLC_EGENERIC;
117

118
    /* skip signature */
119 120
    if( vlc_stream_Read( p_demux->s, NULL, 4 ) < 4 )
        return VLC_EGENERIC;
121

122
    /* read header */
123
    if( vlc_stream_Read( p_demux->s, hdr, 20 ) < 20 )
124
    {
125 126
        msg_Err( p_demux, "cannot read" );
        return VLC_EGENERIC;
127 128
    }

129
    if( GetDWBE( &hdr[0]  ) < 24 )
130
    {
131 132
        msg_Err( p_demux, "invalid file" );
        return VLC_EGENERIC;
133
    }
134

135 136 137 138
    p_sys = malloc( sizeof (*p_sys) );
    if( unlikely(p_sys == NULL) )
        return VLC_ENOMEM;

Laurent Aimar's avatar
Laurent Aimar committed
139
    p_sys->i_time = 0;
140 141
    p_sys->i_header_size = GetDWBE( &hdr[0] );

142
    /* skip extra header data */
143
    if( p_sys->i_header_size > 24 )
144
    {
145 146
#if (SSIZE_MAX <= INT32_MAX)
        if( p_sys->i_header_size > SSIZE_MAX )
147
            goto error;
148 149 150
#endif
        size_t skip = p_sys->i_header_size - 24;
        if( vlc_stream_Read( p_demux->s, NULL, skip ) < (ssize_t)skip )
151
            goto error;
152 153
    }

154
    /* init fmt */
155
    es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );
156 157 158 159 160 161 162 163 164 165 166
    p_sys->fmt.audio.i_rate     = GetDWBE( &hdr[12] );
    p_sys->fmt.audio.i_channels = GetDWBE( &hdr[16] );

#if 0
    p_sys->au.i_header_size   = GetDWBE( &p_sys->au.i_header_size );
    p_sys->au.i_data_size     = GetDWBE( &p_sys->au.i_data_size );
    p_sys->au.i_encoding      = GetDWBE( &p_sys->au.i_encoding );
    p_sys->au.i_sample_rate   = GetDWBE( &p_sys->au.i_sample_rate );
    p_sys->au.i_channels      = GetDWBE( &p_sys->au.i_channels );
#endif
    switch( GetDWBE( &hdr[8] ) )
167
    {
168
        case AU_ALAW_8:        /* 8-bit ISDN A-law */
169
            p_sys->fmt.i_codec               = VLC_CODEC_ALAW;
170 171
            p_sys->fmt.audio.i_bitspersample = 8;
            p_sys->fmt.audio.i_blockalign    = 1 * p_sys->fmt.audio.i_channels;
172
            i_cat                    = AU_CAT_PCM;
173
            break;
174 175

        case AU_MULAW_8:       /* 8-bit ISDN u-law */
176
            p_sys->fmt.i_codec               = VLC_CODEC_MULAW;
177 178
            p_sys->fmt.audio.i_bitspersample = 8;
            p_sys->fmt.audio.i_blockalign    = 1 * p_sys->fmt.audio.i_channels;
179
            i_cat                    = AU_CAT_PCM;
180 181
            break;

182
        case AU_LINEAR_8:      /* 8-bit linear PCM */
183
            p_sys->fmt.i_codec               = VLC_CODEC_S8;
184 185
            p_sys->fmt.audio.i_bitspersample = 8;
            p_sys->fmt.audio.i_blockalign    = 1 * p_sys->fmt.audio.i_channels;
186
            i_cat                    = AU_CAT_PCM;
187 188
            break;

189
        case AU_LINEAR_16:     /* 16-bit linear PCM */
190
            p_sys->fmt.i_codec               = VLC_CODEC_S16B;
191 192
            p_sys->fmt.audio.i_bitspersample = 16;
            p_sys->fmt.audio.i_blockalign    = 2 * p_sys->fmt.audio.i_channels;
193
            i_cat                    = AU_CAT_PCM;
194 195
            break;

196
        case AU_LINEAR_24:     /* 24-bit linear PCM */
197
            p_sys->fmt.i_codec               = VLC_CODEC_S24B;
198 199
            p_sys->fmt.audio.i_bitspersample = 24;
            p_sys->fmt.audio.i_blockalign    = 3 * p_sys->fmt.audio.i_channels;
200
            i_cat                    = AU_CAT_PCM;
201 202
            break;

203
        case AU_LINEAR_32:     /* 32-bit linear PCM */
204
            p_sys->fmt.i_codec               = VLC_CODEC_S32B;
205 206
            p_sys->fmt.audio.i_bitspersample = 32;
            p_sys->fmt.audio.i_blockalign    = 4 * p_sys->fmt.audio.i_channels;
207
            i_cat                    = AU_CAT_PCM;
208 209
            break;

210
        case AU_FLOAT:         /* 32-bit IEEE floating point */
211 212 213
            p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_FLOAT );
            p_sys->fmt.audio.i_bitspersample = 32;
            p_sys->fmt.audio.i_blockalign    = 4 * p_sys->fmt.audio.i_channels;
214
            i_cat                    = AU_CAT_PCM;
215 216
            break;

217
        case AU_DOUBLE:        /* 64-bit IEEE floating point */
218 219 220
            p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_DOUBLE );
            p_sys->fmt.audio.i_bitspersample = 64;
            p_sys->fmt.audio.i_blockalign    = 8 * p_sys->fmt.audio.i_channels;
221
            i_cat                    = AU_CAT_PCM;
222 223
            break;

224
        case AU_ADPCM_G721:    /* 4-bit CCITT g.721 ADPCM */
225 226 227
            p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G721 );
            p_sys->fmt.audio.i_bitspersample = 0;
            p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
228
            i_cat                    = AU_CAT_ADPCM;
229 230
            break;

231
        case AU_ADPCM_G722:    /* CCITT g.722 ADPCM */
232 233 234
            p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G722 );
            p_sys->fmt.audio.i_bitspersample = 0;
            p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
235
            i_cat                    = AU_CAT_ADPCM;
236 237
            break;

238
        case AU_ADPCM_G723_3:  /* CCITT g.723 3-bit ADPCM */
239 240 241
            p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G723_3 );
            p_sys->fmt.audio.i_bitspersample = 0;
            p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
242
            i_cat                    = AU_CAT_ADPCM;
243 244
            break;

245
        case AU_ADPCM_G723_5:  /* CCITT g.723 5-bit ADPCM */
246 247 248
            p_sys->fmt.i_codec               = VLC_FOURCC( 'a', 'u', 0, AU_ADPCM_G723_5 );
            p_sys->fmt.audio.i_bitspersample = 0;
            p_sys->fmt.audio.i_blockalign    = 0 * p_sys->fmt.audio.i_channels;
249
            i_cat                    = AU_CAT_ADPCM;
250 251 252
            break;

        default:
253
            msg_Warn( p_demux, "unknown encoding=0x%x", GetDWBE( &hdr[8] ) );
254 255
            p_sys->fmt.audio.i_bitspersample = 0;
            p_sys->fmt.audio.i_blockalign    = 0;
256
            i_cat                    = AU_CAT_UNKNOWN;
257
            break;
258
    }
259

260 261 262
    p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
                           p_sys->fmt.audio.i_channels *
                           p_sys->fmt.audio.i_bitspersample;
263 264 265

    if( i_cat == AU_CAT_UNKNOWN || i_cat == AU_CAT_ADPCM )
    {
266
        msg_Err( p_demux, "unsupported codec/type (Please report it)" );
267
        goto error;
268 269
    }

270 271 272
    if( p_sys->fmt.audio.i_rate == 0 )
    {
        msg_Err( p_demux, "invalid samplerate: 0" );
273
        goto error;
274 275
    }

276 277
    /* add the es */
    p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
278

279
    /* calculate 50ms frame size/time */
280
    unsigned i_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
281 282 283 284
    p_sys->i_frame_size = i_samples * p_sys->fmt.audio.i_channels *
                          ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
    if( p_sys->fmt.audio.i_blockalign > 0 )
    {
285 286
        unsigned mod = p_sys->i_frame_size % p_sys->fmt.audio.i_blockalign;
        if( mod != 0 )
287
        {
288
            p_sys->i_frame_size += p_sys->fmt.audio.i_blockalign - mod;
289
        }
290
    }
291 292 293
    p_sys->i_frame_length = (mtime_t)1000000 *
                            (mtime_t)i_samples /
                            (mtime_t)p_sys->fmt.audio.i_rate;
294

295 296 297
    p_demux->p_sys = p_sys;
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
298
    return VLC_SUCCESS;
299 300 301
error:
    free( p_sys );
    return VLC_EGENERIC;
302 303 304
}

/*****************************************************************************
305
 * Demux: read packet and send them to decoders
306 307 308
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
309
static int Demux( demux_t *p_demux )
310
{
311 312
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t     *p_block;
313

314
    /* set PCR */
315
    es_out_SetPCR( p_demux->out, VLC_TS_0 + p_sys->i_time );
316

317 318
    p_block = vlc_stream_Block( p_demux->s, p_sys->i_frame_size );
    if( p_block == NULL )
319
    {
320
        msg_Warn( p_demux, "cannot read data" );
321
        return 0;
322
    }
323

324
    p_block->i_dts =
Laurent Aimar's avatar
Laurent Aimar committed
325
    p_block->i_pts = VLC_TS_0 + p_sys->i_time;
326

327
    es_out_Send( p_demux->out, p_sys->es, p_block );
328

329
    p_sys->i_time += p_sys->i_frame_length;
330

331
    return 1;
332 333 334
}

/*****************************************************************************
335
 * Close: frees unused data
336
 *****************************************************************************/
337
static void Close( vlc_object_t * p_this )
338
{
339 340
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
341

342
    free( p_sys );
343 344
}

345 346 347 348 349 350 351
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys = p_demux->p_sys;

352
    return demux_vaControlHelper( p_demux->s, p_sys->i_header_size, -1,
353 354
                                   p_sys->fmt.i_bitrate, p_sys->fmt.audio.i_blockalign,
                                   i_query, args );
355 356
}