rawaud.c 8.83 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 55
#define LANG_LONGTEXT N_("Forces the audio language for the output mux. Three letter ISO639 code. Default is 'eng'. ")

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 71
    add_integer( "rawaud-channels", 2, CHANNELS_TEXT, CHANNELS_LONGTEXT, false );
    add_integer( "rawaud-samplerate", 48000, SAMPLERATE_TEXT, SAMPLERATE_LONGTEXT, false );
72
    add_string( "rawaud-fourcc", FOURCC_DEFAULT,
73
                FOURCC_TEXT, FOURCC_LONGTEXT, false );
74
    add_string( "rawaud-lang", "eng", LANG_TEXT, LANG_LONGTEXT, false);
75 76 77 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
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;

    es_format_Init( &p_sys->fmt, AUDIO_ES, 0 );

    char *psz_fourcc = var_CreateGetString( p_demux, "rawaud-fourcc" );
113 114
    p_sys->fmt.i_codec = vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_fourcc );
    free( psz_fourcc );
115

116
    if( !p_sys->fmt.i_codec )
117 118 119 120 121 122 123 124 125 126 127
    {
        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 )
    {

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

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

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

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

148 149
        case VLC_CODEC_S8:
        case VLC_CODEC_U8:
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            p_sys->fmt.audio.i_bitspersample = 8;
            break;

        default:
            msg_Err( p_demux, "unknown fourcc format %4.4s",
                     (char *)&p_sys->fmt.i_codec);
            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" );

    if( p_sys->fmt.audio.i_rate <= 0 )
    {
      msg_Err( p_demux, "invalid sample rate");
      es_format_Clean( &p_sys->fmt );
      free( p_sys );
      return VLC_EGENERIC;
    }

    if( p_sys->fmt.audio.i_channels <= 0 )
    {
      msg_Err( p_demux, "invalid number of channels");
      es_format_Clean( &p_sys->fmt );
      free( p_sys );
      return VLC_EGENERIC;
    }

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

    msg_Dbg( p_demux,
     "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);

    /* 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 );
201
    date_Set( &p_sys->pts, 0 );
202 203 204 205 206 207 208 209

    /* calculate 50ms frame size/time */
    p_sys->i_frame_samples = __MAX( p_sys->fmt.audio.i_rate / 20, 1 );
    p_sys->i_seek_step = p_sys->fmt.audio.i_channels *
                          ( (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);

210 211
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = Control;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    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;

    if( ( p_block = stream_Block( p_demux->s, p_sys->i_frame_size ) ) == NULL )
    {
        /* EOF */
        return 0;
    }

    p_block->i_dts =
244
    p_block->i_pts = VLC_TS_0 + date_Get( &p_sys->pts );
245 246 247

    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
    es_out_Send( p_demux->out, p_sys->p_es, p_block );
248 249 250

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

251 252 253 254 255 256 257 258 259 260 261 262 263
    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,
                                   p_sys->fmt.i_bitrate, p_sys->i_seek_step, i_query, args );
}