rawaud.c 9.19 KB
Newer Older
1 2 3
/*****************************************************************************
 * rawaud.c : raw audio input module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2009 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Jarmo Torvinen <jarmo.torvinen@jutel.fi>
 *
Jean-Baptiste Kempf's avatar
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
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
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
 *****************************************************************************/

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

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_demux.h>

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


#define SAMPLERATE_TEXT N_("Audio samplerate (Hz)")
#define SAMPLERATE_LONGTEXT N_("Audio sample rate in Hertz. Default is 48000 Hz.")

#define CHANNELS_TEXT N_("Audio channels")
#define CHANNELS_LONGTEXT N_("Audio channels in input stream. Numeric value >0. Default is 2.")

#define FOURCC_TEXT N_("FOURCC code of raw input format")
#define FOURCC_LONGTEXT N_( \
51
    "FOURCC code of the raw input format. This is a four character string." )
52

53
#define LANG_TEXT N_("Forces the audio language")
54
#define LANG_LONGTEXT N_("Forces the audio language for the output mux. Three letter ISO639 code. Default is 'eng'.")
55

56 57 58 59 60 61
#ifdef WORDS_BIGENDIAN
# define FOURCC_DEFAULT "s16b"
#else
# define FOURCC_DEFAULT "s16l"
#endif

62 63 64 65 66 67 68 69
vlc_module_begin();
    set_shortname( "Raw Audio" );
    set_description( N_("Raw audio demuxer") );
    set_capability( "demux", 0 );
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
    set_callbacks( Open, Close );
    add_shortcut( "rawaud" );
70
    add_integer( "rawaud-channels", 2, CHANNELS_TEXT, CHANNELS_LONGTEXT, false );
71
        change_safe()
72
    add_integer( "rawaud-samplerate", 48000, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
73
        change_safe()
74
    add_string( "rawaud-fourcc", FOURCC_DEFAULT,
75
                FOURCC_TEXT, FOURCC_LONGTEXT, false );
76
        change_safe()
77
    add_string( "rawaud-lang", "eng", LANG_TEXT, LANG_LONGTEXT, false);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
vlc_module_end();

/*****************************************************************************
 * Definitions of structures used by this plugin
 *****************************************************************************/
struct demux_sys_t
{
    es_out_id_t *p_es;
    es_format_t  fmt;
    unsigned int i_frame_size;
    unsigned int i_frame_samples;
    unsigned int i_seek_step;
    date_t       pts;
};


/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t * );
static int Control( demux_t *, int i_query, va_list args );


/*****************************************************************************
 * Open: initializes raw audio demuxer
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

    p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
    if( !p_sys )
        return VLC_ENOMEM;

    char *psz_fourcc = var_CreateGetString( p_demux, "rawaud-fourcc" );
114 115
    es_format_Init( &p_sys->fmt, AUDIO_ES,
                    vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_fourcc ) );
116
    free( psz_fourcc );
117

118
    if( !p_sys->fmt.i_codec )
119 120 121 122 123 124 125 126 127 128 129
    {
        msg_Err( p_demux, "rawaud-fourcc must be a 4 character string");
        es_format_Clean( &p_sys->fmt );
        free( p_sys );
        return VLC_EGENERIC;
    }

    // get the bits per sample ratio based on codec
    switch( p_sys->fmt.i_codec )
    {

130
        case VLC_CODEC_FL64:
131 132 133
            p_sys->fmt.audio.i_bitspersample = 64;
            break;

134 135 136
        case VLC_CODEC_FL32:
        case VLC_CODEC_S32L:
        case VLC_CODEC_S32B:
137 138 139
            p_sys->fmt.audio.i_bitspersample = 32;
            break;

140 141
        case VLC_CODEC_S24L:
        case VLC_CODEC_S24B:
142 143 144
            p_sys->fmt.audio.i_bitspersample = 24;
            break;

145 146
        case VLC_CODEC_S16L:
        case VLC_CODEC_S16B:
147 148 149
            p_sys->fmt.audio.i_bitspersample = 16;
            break;

150 151
        case VLC_CODEC_S8:
        case VLC_CODEC_U8:
152 153 154 155 156
            p_sys->fmt.audio.i_bitspersample = 8;
            break;

        default:
            msg_Err( p_demux, "unknown fourcc format %4.4s",
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
157
                    (char *)&p_sys->fmt.i_codec);
158 159 160 161 162 163 164 165 166 167 168
            es_format_Clean( &p_sys->fmt );
            free( p_sys );
            return VLC_EGENERIC;

    }


    p_sys->fmt.psz_language = var_CreateGetString( p_demux, "rawaud-lang" );
    p_sys->fmt.audio.i_channels = var_CreateGetInteger( p_demux, "rawaud-channels" );
    p_sys->fmt.audio.i_rate = var_CreateGetInteger( p_demux, "rawaud-samplerate" );

169
    if( p_sys->fmt.audio.i_rate == 0 || p_sys->fmt.audio.i_rate > 384000 )
170
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
171 172 173 174
        msg_Err( p_demux, "invalid sample rate");
        es_format_Clean( &p_sys->fmt );
        free( p_sys );
        return VLC_EGENERIC;
175 176
    }

177
    if( p_sys->fmt.audio.i_channels == 0 || p_sys->fmt.audio.i_channels > 32 )
178
    {
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
179 180 181 182
        msg_Err( p_demux, "invalid number of channels");
        es_format_Clean( &p_sys->fmt );
        free( p_sys );
        return VLC_EGENERIC;
183 184 185 186 187 188
    }

    p_sys->fmt.i_bitrate = p_sys->fmt.audio.i_rate *
                           p_sys->fmt.audio.i_channels *
                           p_sys->fmt.audio.i_bitspersample;

189 190 191 192 193 194 195 196
    if( p_sys->fmt.i_bitrate > 50000000)
    {
        msg_Err( p_demux, "invalid bitrate");
        es_format_Clean( &p_sys->fmt );
        free( p_sys );
        return VLC_EGENERIC;
    }

197
    msg_Dbg( p_demux,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
198 199 200 201 202 203
            "format initialized: channels=%d , samplerate=%d Hz, fourcc=%4.4s, bits per sample = %d, bitrate = %d bit/s",
            p_sys->fmt.audio.i_channels,
            p_sys->fmt.audio.i_rate,
            (char*)&p_sys->fmt.i_codec,
            p_sys->fmt.audio.i_bitspersample,
            p_sys->fmt.i_bitrate);
204 205 206 207 208 209 210

    /* add the es */
    p_sys->p_es = es_out_Add( p_demux->out, &p_sys->fmt );
    msg_Dbg( p_demux, "elementary stream added");

    /* initialize timing */
    date_Init( &p_sys->pts, p_sys->fmt.audio.i_rate, 1 );
211
    date_Set( &p_sys->pts, 0 );
212 213 214

    /* calculate 50ms frame size/time */
    p_sys->i_frame_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
215
    p_sys->i_seek_step  = p_sys->fmt.audio.i_channels *
216 217 218 219
                          ( (p_sys->fmt.audio.i_bitspersample + 7) / 8 );
    p_sys->i_frame_size = p_sys->i_frame_samples * p_sys->i_seek_step;
    msg_Dbg( p_demux, "frame size is %d bytes ", p_sys->i_frame_size);

220 221
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = Control;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys  = p_demux->p_sys;

    es_format_Clean( &p_sys->fmt );
    free( p_sys );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys  = p_demux->p_sys;
    block_t     *p_block;

247 248
    p_block = vlc_stream_Block( p_demux->s, p_sys->i_frame_size );
    if( p_block == NULL )
249 250 251 252 253
    {
        /* EOF */
        return 0;
    }

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
254
    p_block->i_dts = p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
255

256
    es_out_SetPCR( p_demux->out, p_block->i_pts );
257
    es_out_Send( p_demux->out, p_sys->p_es, p_block );
258 259 260

    date_Increment( &p_sys->pts, p_sys->i_frame_samples );

261 262 263 264 265 266 267 268 269 270 271
    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys  = p_demux->p_sys;

    return demux_vaControlHelper( p_demux->s, 0, -1,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
272
                                  p_sys->fmt.i_bitrate, p_sys->i_seek_step, i_query, args );
273
}